Skip to content
AI Engineer Guide.

concept · data · intermediate · concept 13 of 17

Redis

0 of 17 complete

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

Mechanism, step by stepInteractive
  1. 1Classify each key by what losing it costs: recomputable cache, session state, or a queue entry that must not vanish.
  2. 2Choose persistence accordingly, with append-only logging for durability and periodic snapshots for faster restarts.
  3. 3Set a maxmemory limit and an eviction policy that matches the key classes, and keep durable keys out of the evictable set.
  4. 4Use key prefixes and TTLs so cached entries expire on their own rather than accumulating.
  5. 5Avoid long-running commands over large collections; scan incrementally instead of blocking the server.
  6. 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.

Step 1/6 · step 1

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.

  1. Classify each key by what losing it costs: recomputable cache, session state, or a queue entry that must not vanish.
  2. Choose persistence accordingly, with append-only logging for durability and periodic snapshots for faster restarts.
  3. Set a maxmemory limit and an eviction policy that matches the key classes, and keep durable keys out of the evictable set.
  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 blocking the server.
  6. Measure hit rate, memory fragmentation and evictions, not just average latency.

Trade-offs

ChoiceGood forCostFails when
Cache-asideSimple and resilient to cache lossA miss storm after evictionTraffic spikes when the cache is cold
Write-through cacheReads always warmWrite latency and partial failure handlingThe cache is a source of truth by accident
Append-only persistenceSmall durability windowDisk growth and slower restartsWrite volume exceeds disk throughput
No persistenceFastest and simplestEverything is lost on restartKeys hold session or queue state
Trade-off mapInteractive

Good for

Simple and resilient to cache loss

Cost

A miss storm after eviction

Fails when

Traffic spikes when the cache is cold

  1. Traffic spikes when the cache is cold
  2. The cache is a source of truth by accident
  3. Write volume exceeds disk throughput
  4. 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