Redis as Cache vs As Database: When Each Fits
Redis is the multi-tool of databases. Knowing when to use which mode prevents mismatched-workload pain.
Cache mode vs DB mode
Cache mode: data may be lost; performance is everything; refill from source-of-truth.
DB mode: data must persist; durability matters; performance still matters.
Four criteria
- 1. Acceptable data loss? Yes → cache; no → DB.
- 2. Re-fillable from source? Yes → cache; no → DB.
- 3. Sub-millisecond required? Yes → either; no → consider Postgres.
- 4. Durability + speed? DB mode + RDB+AOF.
Persistence options
RDB: snapshot at intervals; lose data between.
AOF: write log; near-zero loss; slower writes.
RDB+AOF: best of both at cost of disk.
Failure modes
Cache mode: cache miss on Redis-down is OK; app falls back to source.
DB mode: Redis-down = outage. Plan for HA, backups, failover.
Antipatterns
- DB mode without persistence. Data loss waiting.
- Cache mode treated as durable. Surprise data loss.
- One Redis cluster for both. Mixed concerns.
What to do this week
Three moves. (1) Apply this pattern to your most-loaded table. (2) Measure query latency / write throughput before/after. (3) Document the win and the constraint so the next refactor inherits the knowledge.