Redis
How Redis works and when to use it: data structures, single-threaded atomicity, persistence, replication, Sentinel, Cluster hash slots, eviction and the pitfalls.
On this page
- The problem: the database is too slow for the hot path
- The data structures, and what each one is for
- Single-threaded, and why that makes it simple
- Persistence: what survives a restart
- Replication, Sentinel and Cluster
- When memory runs out: expiry and eviction
- Performance numbers and a worked sizing
- Pitfalls that cause real outages
- When to pick Redis, and when not
- In the interview
- 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.
| Type | What it is | Typical use | Key commands (cost) |
|---|---|---|---|
| String | Bytes, up to 512 MB; can be treated as an integer | Cached JSON, counters, flags, locks | GET, SET … EX 60 NX, INCR (O(1)) |
| Hash | A small map of field → value inside one key | A user profile, a shopping cart | HSET, HGET, HINCRBY (O(1)) |
| List | A linked list of strings | Simple queues, "last 100 events" | LPUSH, RPOP, BLMOVE, LTRIM (O(1) at the ends) |
| Set | Unordered unique strings | Tags, "who liked this", de-duplication | SADD, SISMEMBER (O(1)), SINTER |
| Sorted set | Unique members, each with a score, kept in order | Leaderboards, rate-limit windows, priority queues, time-ordered feeds | ZADD, ZINCRBY, ZRANK (O(log N)), ZRANGE |
| Stream | An append-only log with consumer groups | Lightweight event log or job queue | XADD, XREADGROUP, XACK |
| HyperLogLog | A probabilistic counter of distinct items, ~12 KB each | Unique visitors per day (standard error ~0.8%) | PFADD, PFCOUNT |
| Bitmap / geo | Bit operations on a string; geo indexes built on sorted sets | Daily 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:
| Tool | What it guarantees | What it does not do |
|---|---|---|
MULTI … EXEC | Queued commands run back to back with nothing interleaved | No rollback: if one command fails at run time, the others still apply. You cannot read a value and decide inside the transaction. |
WATCH + MULTI | Optimistic locking: EXEC aborts if a watched key changed since WATCH | Under 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 reads | While 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:
BGSAVEforks 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.alwaysfsyncs 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:
| Policy | Behaviour | Use when |
|---|---|---|
noeviction (default) | Writes fail with an out-of-memory error | Redis holds data you must not silently lose (queues, locks) |
allkeys-lru | Evict the least recently used key, any key | A pure cache: the usual choice |
allkeys-lfu | Evict the least frequently used key | A cache whose popular keys stay popular |
volatile-lru / volatile-lfu / volatile-ttl | Evict only keys that have a TTL | One 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
| Measure | Typical value |
|---|---|
| Latency, same data centre | 0.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 key | roughly 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
maxmemoryat ~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 patternscans every key and blocks the server. UseSCAN, 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
DELblocks the thread. UseUNLINK(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
| Redis | Memcached | A relational database | Kafka | |
|---|---|---|---|---|
| Good at | Sub-ms reads, counters, sorted sets, locks, rate limits, small queues | Huge, simple, multi-threaded string cache | Durable truth, queries, transactions | Durable, replayable event streams |
| Weak at | Data bigger than RAM, durability on failover, complex queries | No persistence, replication or data types | Microsecond latency, very hot counters | Request/response lookups |
| Use when | Speed matters and data can be rebuilt | Only a big flat cache is needed | It is the system of record | Many 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
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.
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; useSCAN, or better, keep a sorted setcarts:by_updated(score = last update time) and query it withZRANGEBYSCORE.allkeys-lruwill evict carts when memory is full, silently emptying customers' carts. Carts here are primary data, so usenoevictionorvolatile-*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.
- 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.
SCANnotKEYS,UNLINKnotDELfor 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.
WAITnarrows, 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
maxmemoryand a policy:allkeys-lrufor caches,noevictionfor data you must keep. - Not the system of record. Not for data much bigger than RAM, ad-hoc queries, or durable replayable streams.
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?
A sorted set keeps members ordered by score with O(log N) updates and rank lookups. HGETALL and SCAN read all 5 million entries per request, and LINSERT needs an O(N) walk to find the position.
2. Two hundred app servers call INCR page:views at the same moment. Why is the final count correct without any locking?
Single-threaded execution makes every command atomic, so there is no read-modify-write window. Redis has no row locks, never retries commands for you, and the client sends each INCR separately.
3. An engineer runs KEYS session:* on a production node with 40 million keys. What happens?
KEYS is O(N) and runs on the one command thread, blocking everything. SCAN is the cursor-based command that returns small batches; KEYS is not disabled by default.
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?
Replication is asynchronous, and the promoted replica serves from its own memory, not the old primary's AOF. Writes acknowledged but not yet copied are gone, whatever the fsync setting.
5. A Redis Cluster rejects MGET user:1:name user:2:name with a CROSSSLOT error. What is the right fix?
Multi-key commands need all keys in one slot, and only the part inside braces is hashed. Transactions have the same restriction, and the slot count is fixed at 16,384.
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?
allkeys-* policies evict any key. Lists have no default length limit and keys have no default TTL. Keep queues on an instance with noeviction, or use volatile-* with TTLs only on cache keys.
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?
The cost is 200 × 0.3 ms = 60 ms of round trips; the commands themselves take microseconds. Pipelining or MGET pays about one round trip. A Lua script would work but is heavier than needed and blocks the server while it runs.
Score 60% or more and the lesson is marked complete.