How Redis works under the hood
We'll cover the following
Visual summary

Download imageFree to share, please link back to this lesson.
Redis is fast because it keeps everything in RAM and uses simple in-memory data structures. The best way to see how it works is to follow one write, SET user:42 "Ana", from your app, into memory, and back.
One command, nine steps
| # | Step | What happens |
|---|---|---|
| 1 | Client sends | The client library sends the command. Writes always go to the primary node. |
| 2 | Decode | Redis decodes the RESP protocol into command, key, value and args. |
| 3 | Validate | Does the command exist? Are the args correct? Is the client authenticated and allowed? |
| 4 | Find handler | Look the command up in the internal command table. |
| 5 | Execute | Run it. One command at a time, in order. |
| 6 | Update RAM | Find the key in the in-memory hash table and write the value. |
| 7 | Expire and evict | TTL keys expire lazily (on access) or actively (background sweep). If memory is full, the eviction policy frees space. |
| 8 | Persist and replicate | The write is logged to AOF, captured in RDB snapshots, or copied to replicas. |
| 9 | Reply | Redis builds the reply in RESP and sends it back. |
What goes over the wire (RESP)
RESP is a simple text protocol. *3 means an array of three items, and $7 means the next string is 7 bytes long.
-> *3\r\n$3\r\nSET\r\n$7\r\nuser:42\r\n$3\r\nAna\r\n
<- +OK\r\nThe one thing to remember: step 5
Redis runs commands on a single thread. Each command is atomic, so there are no locks and no race conditions. Since Redis 6, extra threads can handle network reads and writes, but commands themselves still run one at a time.
The catch: one slow command, like KEYS * on a huge dataset, blocks everyone else until it finishes.
- Use
SCANinstead ofKEYS *so the work comes in small batches. - Be careful with any command that is O(N) on a big key (a huge set, list or hash).
- Watch the slow log: one bad command shows up as latency for every client.
Step 7: expire and evict
- Lazy expiry: an expired key is removed when someone tries to read it.
- Active expiry: a background sweep samples keys with TTLs and removes the expired ones, so keys nobody reads do not pile up.
- Eviction: when memory reaches
maxmemory, the eviction policy decides what to drop, for example LRU (least recently used) or LFU (least frequently used). Withnoeviction, writes fail instead.
Step 8: persist and replicate
| Option | What it does | Trade-off |
|---|---|---|
AOF (appendonly.aof) | Logs every write so it can be replayed | Most durable, bigger file, slower restart |
RDB (dump.rdb) | Point-in-time snapshot of memory | Compact and fast to load, but you lose writes since the last snapshot |
| Replicas | Copy writes to other nodes | A crash on the primary loses nothing that was copied, and replicas can serve reads |
Why it is fast
| Why it helps | |
|---|---|
| RAM, not disk | No disk seek on the read or write path |
| One thread | No locks to wait on |
| O(1) hash table lookups | Finding a key does not slow down as data grows |
Scaling
With one primary, all writes hit that one node. With Redis Cluster, keys are split across 16,384 hash slots, and each shard's primary handles writes for its own slots.
Check yourselfWhy does Redis not need locks even with many clients connected?
Commands run on a single thread, one at a time, in order. Each command is atomic, so two clients can never interleave inside one command.
Check yourselfOne client runs KEYS * on a dataset with 50 million keys. What do the other clients see?
Their commands wait. Execution is single-threaded, so the slow command blocks everyone until it finishes. Use SCAN to walk keys in small batches.
Check yourselfA key has a 5-minute TTL and nobody reads it after that. Does it stay in memory forever?
No. Expiry is lazy (on access) and active (a background sweep), so unread expired keys are cleaned up too. If memory still fills, the eviction policy frees space.
Check yourselfYou have one primary and the write traffic outgrows it. What changes with Redis Cluster?
Keys are split across 16,384 hash slots, with one primary per shard. Each primary handles writes for its own slots, so write load is spread across nodes.