Why it matters
Many application paths need state that is faster than a database round trip and shared across processes: session data, rate-limit counters, cached query results, job queues. Redis provides those structures with predictable per-command latency. The engineering work is deciding what may be lost, how the instance behaves at the memory limit and which operations are safe to run against a single-threaded server.
Mental model
Redis is a whiteboard in a shared room: fast to read and write, visible to everyone, and erased when the room is cleared. The metaphor breaks because a whiteboard has no eviction policy; Redis will silently delete entries you still needed if memory fills.
How it works
- 1Classify each key by what losing it costs: recomputable cache, session state, or a queue entry that must not vanish.
- 2Choose persistence accordingly, with append-only logging for durability and periodic snapshots for faster restarts.
- 3Set a maxmemory limit and an eviction policy that matches the key classes, and keep durable keys out of the evictable set.
- 4Use key prefixes and TTLs so cached entries expire on their own rather than accumulating.
- 5Avoid long-running commands over large collections; scan incrementally instead of blocking the server.
- 6Measure hit rate, memory fragmentation and evictions, not just average latency.
Step 1 of 6
Classify each key by what losing it costs: recomputable cache, session state, or a queue entry that must not vanish.
Figure. The mechanism in this page's own steps. The figure adds sequence, not new claims.
What to notice. A step can be read alone, but the order is the mechanism. The control lets you stop on any one.
Text version
The page's steps: 1. Classify each key by what losing it costs: recomputable cache, session state, or a queue … 2. Choose persistence accordingly, with append-only logging for durability and periodic snap… 3. Set a maxmemory limit and an eviction policy that matches the key classes, and keep durab… 4. Use key prefixes and TTLs so cached entries expire on their own rather than accumulating. 5. Avoid long-running commands over large collections; scan incrementally instead of blockin… 6. Measure hit rate, memory fragmentation and evictions, not just average latency.
- Classify each key by what losing it costs: recomputable cache, session state, or a queue entry that must not vanish.
- Choose persistence accordingly, with append-only logging for durability and periodic snapshots for faster restarts.
- Set a maxmemory limit and an eviction policy that matches the key classes, and keep durable keys out of the evictable set.
- Use key prefixes and TTLs so cached entries expire on their own rather than accumulating.
- Avoid long-running commands over large collections; scan incrementally instead of blocking the server.
- Measure hit rate, memory fragmentation and evictions, not just average latency.
Trade-offs
| Choice | Good for | Cost | Fails when |
|---|---|---|---|
| Cache-aside | Simple and resilient to cache loss | A miss storm after eviction | Traffic spikes when the cache is cold |
| Write-through cache | Reads always warm | Write latency and partial failure handling | The cache is a source of truth by accident |
| Append-only persistence | Small durability window | Disk growth and slower restarts | Write volume exceeds disk throughput |
| No persistence | Fastest and simplest | Everything is lost on restart | Keys hold session or queue state |
Good for
Simple and resilient to cache loss
Cost
A miss storm after eviction
Fails when
Traffic spikes when the cache is cold
- Traffic spikes when the cache is cold
- The cache is a source of truth by accident
- Write volume exceeds disk throughput
- Keys hold session or queue state
Figure. Each choice with what it buys, what it costs, and the case where it loses.
What to notice. The fourth column is the decision: if the losing case describes your situation, the choice is wrong.
Text version
Four trade-offs: Cache-aside is good for Simple and resilient to cache loss, costs A miss storm after eviction, and fails when Traffic spikes when the cache is cold. Write-through cache is good for Reads always warm, costs Write latency and partial failure handling, and fails when The cache is a source of truth by accident. Append-only persistence is good for Small durability window, costs Disk growth and slower restarts, and fails when Write volume exceeds disk throughput. No persistence is good for Fastest and simplest, costs Everything is lost on restart, and fails when Keys hold session or queue state
Failure modes
- Eviction of durable keys. Session data disappears under memory pressure because all keys share one policy. Debug: inspect evicted key prefixes and compare against the classes marked durable.
- Cache stampede. A popular key expires and every request recomputes it at once. Debug: plot backend request rate against cache miss rate for the same minute.
- Slow command blocking. One large collection operation stalls every client. Debug: enable slow command logging and correlate latency spikes with command names.
In production
Alert on evictions, memory fragmentation and hit rate, not only on latency. Set maxmemory below the instance limit to leave room for replication buffers, and load-test the cold-cache path.
Common misconceptions
People think: Redis is just a cache.
Actually: It is a general data structure server; queues, counters and coordination all rely on structures a cache does not have.
People think: Persistence makes Redis a database.
Actually: Snapshots and append-only logs reduce loss but do not give you the transactional and query guarantees of a database.
Interview angle
Start from the access pattern and the cost of losing the data, then pick structures, persistence and an eviction policy. Mention single-threaded execution and cold-cache behaviour. Weak answers say they would put it in front of the database without saying what happens at the memory limit.
Sources
- Redis documentation — data types, commands and operational model.
- Redis persistence — snapshots, append-only files and durability trade-offs.
- Redis eviction policies — maxmemory behaviour and what gets removed.