Code Reference

Redis vs Postgres

Comparisons · Reference cheat sheet

Redis vs Postgres

Comparisons · Reference cheat sheet


📋 Overview

Postgres is a durable relational system of record. Redis is an in-memory data structure server optimized for speed, caching, ephemeral state, and specialized structures. Often used together, not as mutual substitutes.

🔧 Core concepts

DimensionRedisPostgres
Primary store?Usually no (cache/session)Yes (SoR)
DurabilityOptional RDB/AOFWAL + strong durability defaults
Query modelKeys + data structuresSQL + relational model
LatencySub-ms in-memoryLow ms; disk-backed
Relations / joinsDIYFirst-class
Memory costEntire working set in RAMBuffer cache + disk

When to use Redis: cache, rate limits, sessions, leaderboards, pub/sub, queues (or Streams).

When to use Postgres: authoritative business data, complex queries, transactions, constraints.

💡 Examples

Redis cache aside:

GET user:42
# miss → load from Postgres → SET user:42 … EX 300

Postgres source of truth:

SELECT id, email FROM users WHERE id = 42;

⚠️ Pitfalls

  • Using Redis as the only database without HA/persistence for critical data.
  • Caching without invalidation strategy serves stale Postgres data forever.
  • Storing huge blobs in Redis blows RAM — keep hot, small keys.

On this page