← ALL LOGS

Postgres + Redis patterns for real-time systems

For real-time backends I reach for the same pairing again and again: Postgres as the durable source of truth, Redis as the hot path. The discipline is keeping the line between them sharp.

What goes where

  • Postgres — anything that must survive a restart: tenant config, records, audit.
  • Redis — ephemeral, high-churn, latency-critical: session state, dedup keys, counters, pub/sub fan-out.

Don’t let the cache become the truth

The failure mode is drift — treating Redis as authoritative and waking up to state you can’t reconstruct. Two guards: give hot keys a TTL so stale data self-heals, and make idempotency keys SET NX EX so a crash can’t wedge you.

await redis.set(`session:${id}`, JSON.stringify(state), { EX: 3600 });

Everything in Redis should be rebuildable from Postgres. If it isn’t, it belongs in Postgres.