Technology deep dives · Chapter 25

Redis

How Redis works and when to use it: data structures, single-threaded atomicity, persistence, replication, Sentinel, Cluster hash slots, eviction and the pitfalls.

AdvancedInterview priority: High45 min
On this page
  1. The problem: the database is too slow for the hot path
  2. The data structures, and what each one is for
  3. Single-threaded, and why that makes it simple
  4. Persistence: what survives a restart
  5. Replication, Sentinel and Cluster
  6. When memory runs out: expiry and eviction
  7. Performance numbers and a worked sizing
  8. Pitfalls that cause real outages
  9. When to pick Redis, and when not
  10. In the interview
  11. Quiz

The problem: the database is too slow for the hot path

A game shows a live leaderboard of 5 million players. A login service checks a session on every request. An API allows each customer 100 requests a minute. All three need an answer in under a millisecond, thousands of times a second, and all three involve data that changes constantly.

A relational database can do each of these, but badly. "Rank of player X among 5 million" is a sort or an index scan; "add 1 to this counter" is a row lock and a disk write; 50,000 session checks a second is a lot of load for data that is thrown away after an hour. An indexed lookup costs 1–5 ms and much more under load.

Redis is an in-memory data structure server. It keeps everything in RAM, answers over the network in about 0.2–0.5 ms, and, unlike a plain key-value cache, it stores structures (lists, sets, sorted sets, hashes) with operations that run on the server. "Add 10 points to Alice and tell me her rank" is one command that takes microseconds. A single node handles roughly 100,000+ operations a second.

Most people meet Redis as a cache. This lesson is about how it actually works, so you can say what it guarantees, what it doesn't, and when it will hurt you.

The data structures, and what each one is for

A Redis key always holds one type of value. Picking the right type is most of the design work.

TypeWhat it isTypical useKey commands (cost)
StringBytes, up to 512 MB; can be treated as an integerCached JSON, counters, flags, locksGET, SET … EX 60 NX, INCR (O(1))
HashA small map of field → value inside one keyA user profile, a shopping cartHSET, HGET, HINCRBY (O(1))
ListA linked list of stringsSimple queues, "last 100 events"LPUSH, RPOP, BLMOVE, LTRIM (O(1) at the ends)
SetUnordered unique stringsTags, "who liked this", de-duplicationSADD, SISMEMBER (O(1)), SINTER
Sorted setUnique members, each with a score, kept in orderLeaderboards, rate-limit windows, priority queues, time-ordered feedsZADD, ZINCRBY, ZRANK (O(log N)), ZRANGE
StreamAn append-only log with consumer groupsLightweight event log or job queueXADD, XREADGROUP, XACK
HyperLogLogA probabilistic counter of distinct items, ~12 KB eachUnique visitors per day (standard error ~0.8%)PFADD, PFCOUNT
Bitmap / geoBit operations on a string; geo indexes built on sorted setsDaily active flags; "drivers within 2 km"SETBIT, BITCOUNT, GEOSEARCH

A sorted set is the one interviewers love. Inside, it is a hash table (member → score) plus a skip list (a layered linked list that allows O(log N) search) ordered by score. That gives you both "what is Alice's score?" in O(1) and "who are the top 10?" or "what is Alice's rank?" in O(log N).

import redis
r = redis.Redis()

r.zincrby("lb:2026-11", 50, "alice")                   # add 50 points
r.zincrby("lb:2026-11", 20, "bob")
top10 = r.zrevrange("lb:2026-11", 0, 9, withscores=True)
rank = r.zrevrank("lb:2026-11", "alice")                # 0 = first place

With 5 million members, ZREVRANK walks about log₂(5 M) ≈ 23 levels: microseconds, versus a database ranking query that has to count every row above Alice.

Single-threaded, and why that makes it simple

One thread, one command at a time, so every command is atomic

Redis executes commands on one main thread. An event loop reads requests from many sockets, runs each command to completion, and writes the reply. Since Redis 6, separate I/O threads can do the socket reading and writing, and background threads handle disk syncs and freeing large values, but commands themselves still run one at a time on the main thread.

This sounds like a weakness, but it is the source of Redis's main guarantee: every command is atomic. INCR on a counter that a thousand clients hit at once never loses an update, because there is no "read, add one, write back" window for another client to slip into. There are no locks inside Redis, and no lock overhead.

The price is the one you see in the next step:

A single slow command blocks every client

Anything that takes O(N) on a large N, such as KEYS *, HGETALL on a 10-million-field hash, SMEMBERS on a huge set, DEL of a giant key or a long Lua script, stops the whole server. A command that takes 2 seconds means every other client waits 2 seconds.

Several commands as one: transactions and Lua

Single commands are atomic, but "check the balance, then subtract" is two commands. Redis offers three tools:

ToolWhat it guaranteesWhat it does not do
MULTI … EXECQueued commands run back to back with nothing interleavedNo rollback: if one command fails at run time, the others still apply. You cannot read a value and decide inside the transaction.
WATCH + MULTIOptimistic locking: EXEC aborts if a watched key changed since WATCHUnder heavy contention, clients retry again and again
Lua script (EVAL, or functions in Redis 7)The whole script runs atomically on the server and can branch on what it readsWhile it runs, nothing else runs; a slow script blocks the server

The classic Lua use is releasing a lock only if you still own it, which must be a compare-and-delete in one step:

RELEASE = """
if redis.call('GET', KEYS[1]) == ARGV[1] then
    return redis.call('DEL', KEYS[1])
end
return 0
"""

def acquire(r, name, token, ttl_ms=10_000):
    return r.set(f"lock:{name}", token, nx=True, px=ttl_ms)   # True or None

def release(r, name, token):
    return r.eval(RELEASE, 1, f"lock:{name}", token) == 1

Persistence: what survives a restart

RDB snapshots versus the append-only file

Everything lives in memory, so durability is opt-in. Two mechanisms:

  • RDB snapshot: BGSAVE forks the process; the child writes the whole dataset to a compact file while the parent keeps serving. The fork uses copy-on-write (pages are shared until the parent modifies them), so a write-heavy instance can need up to about twice its memory during a snapshot. Good for backups and fast restarts; loses everything since the last snapshot.
  • AOF (append-only file): every write is appended to a log. appendfsync everysec (the default when AOF is on) flushes to disk once a second, so a crash loses about a second. always fsyncs every write: safest, and many times slower. The log is periodically rewritten (compacted) in the background.

Production setups usually enable both: AOF for a small loss window, RDB files for backups. Even with appendfsync always, though, replication (below) can still lose acknowledged writes on failover, so do not treat persistence as making Redis a fully durable database.

Replication, Sentinel and Cluster

Replication

A primary streams every write to one or more replicas. Replication is asynchronous: the primary replies to the client before replicas have the change. The WAIT 1 100 command blocks until at least one replica has acknowledged, or 100 ms pass, which narrows the loss window but does not make Redis strongly consistent: a replica that missed the write can still be promoted.

Sentinel: automatic failover for one primary

Redis Sentinel is a set of small processes (run at least three, on different machines) that watch a primary. When a quorum of Sentinels agree it is unreachable, one of them promotes a replica and tells clients the new address. Clients ask Sentinel "where is the primary?" rather than hard-coding it. Sentinel gives high availability, not more capacity: all writes still go to one primary.

Redis Cluster: sharding with hash slots

Keys map to 16,384 hash slots, spread across primaries with replicas

When data or write traffic outgrows one primary, Redis Cluster shards it. The key space is split into 16,384 hash slots: slot = CRC16(key) mod 16384. Each primary owns a range of slots; resharding moves slots (and their keys) between nodes while the cluster stays online. Clients cache the slot map; if they ask the wrong node they get a MOVED reply naming the right one and update their map. The nodes gossip with each other, and if a primary fails, its replicas are promoted by a vote of the other primaries, without Sentinel.

A command that touches several keys only works if they are in the same slot, otherwise it fails with CROSSSLOT. Hash tags fix this: only the part of the key inside {…} is hashed, so {user:42}:cart and {user:42}:profile always land in the same slot (the slot of user:42, which is 15880). Use them sparingly: one giant tag puts everything on one node.

Asynchronous replication can lose an acknowledged write on failover

When memory runs out: expiry and eviction

Keys can carry a TTL. Redis removes expired keys lazily (when you touch them) and with a background sampler that tests a few random keys with TTLs many times a second, so expired keys do not all disappear the instant they expire, but they are never returned.

When maxmemory is reached, the maxmemory-policy decides what happens:

PolicyBehaviourUse when
noeviction (default)Writes fail with an out-of-memory errorRedis holds data you must not silently lose (queues, locks)
allkeys-lruEvict the least recently used key, any keyA pure cache: the usual choice
allkeys-lfuEvict the least frequently used keyA cache whose popular keys stay popular
volatile-lru / volatile-lfu / volatile-ttlEvict only keys that have a TTLOne instance holds both cache keys (with TTLs) and permanent keys

The LRU is approximate: Redis samples 5 keys (configurable) and evicts the best candidate among them, which is cheap and close to exact LRU in practice.

Performance numbers and a worked sizing

MeasureTypical value
Latency, same data centre0.2–0.5 ms round trip; the command itself takes microseconds
Throughput per node~100 K+ simple ops/s; around 1 M ops/s with pipelining
Overhead per keyroughly 50–100 bytes on top of the key and value
Failover time (Sentinel or Cluster)a few seconds to ~15–30 s, depending on timeouts

Pipelining means sending many commands without waiting for each reply. 100 GETs one by one cost 100 round trips × 0.3 ms = 30 ms; pipelined they cost about one round trip. It is the cheapest speed-up there is.

Worked example: a session store. 20 M active sessions, 1 KB each, 30,000 reads/s and 3,000 writes/s at peak.

  • Data: 20 M × 1 KB = 20 GB. Overhead: 20 M × ~100 bytes ≈ 2 GB. Total ≈ 22 GB.
  • Headroom: with maxmemory at ~70% of RAM, you need about 32 GB of RAM in total.
  • Throughput: 33 K ops/s fits one node, but a 32 GB single instance forks slowly and takes minutes to reload. Three primaries of ~8 GB each, each with one replica, spread the memory and keep restarts fast.

Pitfalls that cause real outages

  • KEYS pattern scans every key and blocks the server. Use SCAN, which walks the key space in small batches with a cursor (it may return a key twice, so handle duplicates).
  • Big keys: a list with 10 million entries or a 50 MB string. Reading it hogs the network; deleting it with DEL blocks the thread. Use UNLINK (frees memory in a background thread), and split big collections into buckets (followers:42:0 … :99).
  • Hot keys: one key lives on one node, so a viral key overloads that node however big the cluster is. Use a short local cache in the app, or copies of the key on several nodes. See the hot-key section of caching.
  • Fork latency: forking a 50 GB process can pause the main thread for hundreds of milliseconds while page tables are copied. Keep instances moderate in size and disable Linux transparent huge pages.
  • No TTLs on cache keys: memory fills with keys nobody reads, and eviction has to work hard.
  • Treating it as the database: see the box above.

When to pick Redis, and when not

RedisMemcachedA relational databaseKafka
Good atSub-ms reads, counters, sorted sets, locks, rate limits, small queuesHuge, simple, multi-threaded string cacheDurable truth, queries, transactionsDurable, replayable event streams
Weak atData bigger than RAM, durability on failover, complex queriesNo persistence, replication or data typesMicrosecond latency, very hot countersRequest/response lookups
Use whenSpeed matters and data can be rebuiltOnly a big flat cache is neededIt is the system of recordMany consumers must read the same events

When not to use Redis: as the only copy of money, orders or anything you cannot rebuild; for datasets much larger than memory (RAM costs roughly 10× SSD); for ad-hoc queries ("all users in Paris who signed up last week"), which need a database or search index; and as a heavy-duty message broker where you need retention, replay and many consumer groups at high volume, which is Kafka's job.

In the interview

Your turn: rate limiter

Design a per-API-key rate limiter in Redis: 100 requests per rolling 60 seconds, across 40 app servers, at 50,000 requests a second in total. Which data structure, which commands, how atomic, and what happens if Redis is down?

Compare with our answer

One good answer is a sliding window log in a sorted set per key, run as one Lua script so the steps are atomic:

-- KEYS[1] = rl:{api_key}   ARGV = window_ms, limit, request_id
-- Take the time from Redis itself, so clock skew between app servers can't bend the window.
local t = redis.call('TIME')
local now = tonumber(t[1]) * 1000 + math.floor(tonumber(t[2]) / 1000)
redis.call('ZREMRANGEBYSCORE', KEYS[1], 0, now - tonumber(ARGV[1]))
if redis.call('ZCARD', KEYS[1]) < tonumber(ARGV[2]) then
  redis.call('ZADD', KEYS[1], now, ARGV[3])
  redis.call('PEXPIRE', KEYS[1], ARGV[1])
  return 1
end
return 0

A cheaper alternative is a fixed window counter (INCR rl:{key}:{minute} plus EXPIRE), which uses one small key but lets a client send up to 2× the limit across a window boundary. 50 K checks a second fits one node; shard with Cluster if it grows. If Redis is down, decide in advance whether to fail open (allow traffic, protect availability) or fail closed (reject, protect the backend).

A strong answer mentions: the data structure and why · atomicity (Lua or a single command) · a TTL so idle keys vanish · the memory cost (a sorted set stores every request in the window) · the fail-open or fail-closed decision · that limits are approximate during a failover.

Your turn: spot the bugs

A team stores user carts in Redis as one JSON string per user. To show the "abandoned carts" admin page they run KEYS cart:* and read each one. They use allkeys-lru and no persistence, because "Redis is fast". Carts are not stored anywhere else. What is wrong?

Compare with our answer

A strong answer mentions:

  • KEYS cart:* blocks the server for every user while it scans millions of keys; use SCAN, or better, keep a sorted set carts:by_updated (score = last update time) and query it with ZRANGEBYSCORE.
  • allkeys-lru will evict carts when memory is full, silently emptying customers' carts. Carts here are primary data, so use noeviction or volatile-* with TTLs only on true cache keys.
  • No persistence and no other copy means a restart loses every cart. Enable AOF and RDB, or write carts to a database and treat Redis as the cache.
  • One JSON string per cart means read-modify-write races on every change; a hash per cart (HINCRBY cart:42 sku_9 1) updates items atomically.
  • Asynchronous replication can still lose recent changes on failover, so carts that matter for revenue belong in a durable store.
Redis on one page
  • In-memory data structure server: ~0.2–0.5 ms per round trip, ~100 K+ ops/s per node, ~1 M with pipelining.
  • Pick the structure: string (cache, counter, lock), hash (object), list (simple queue), set (membership), sorted set (leaderboard, sliding window, priority), stream, HyperLogLog (~12 KB unique counts).
  • Commands run on one thread, so each is atomic; one slow O(N) command blocks everyone. SCAN not KEYS, UNLINK not DEL for big keys.
  • Multi-step atomicity: MULTI/EXEC (no rollback), WATCH (optimistic), Lua (atomic, but blocks while running).
  • Persistence: RDB snapshots (fork, copy-on-write) + AOF with fsync every second ≈ lose ~1 s on crash.
  • Replication is asynchronous: failover can lose acknowledged writes. WAIT narrows, does not close, the gap.
  • Sentinel = failover for one primary; Cluster = 16,384 hash slots, CRC16(key) mod 16384, hash tags {…} for multi-key commands.
  • Always set maxmemory and a policy: allkeys-lru for caches, noeviction for data you must keep.
  • Not the system of record. Not for data much bigger than RAM, ad-hoc queries, or durable replayable streams.
Check what stuck

1. A game needs "show the top 100 players" and "show my rank" for 5 million players, updated after every match. Which Redis structure fits best?

2. Two hundred app servers call INCR page:views at the same moment. Why is the final count correct without any locking?

3. An engineer runs KEYS session:* on a production node with 40 million keys. What happens?

4. Redis has AOF on with appendfsync everysec, one replica and Sentinel. The primary crashes and the replica is promoted. Which data can be lost?

5. A Redis Cluster rejects MGET user:1:name user:2:name with a CROSSSLOT error. What is the right fix?

6. A team uses one Redis instance for both a cache and a job queue (a list). Memory fills, and some queued jobs vanish. What was the likely cause?

7. A service sends 200 GET commands one after another for each page, and each round trip is 0.3 ms. What is the simplest large improvement?

Score 60% or more and the lesson is marked complete.

Passing the quiz also marks it complete. Progress is saved in this browser only.