How Redis works under the hood

Lesson 3.4 · 6 min read · by Vinay C

We'll cover the following

Visual summary

Infographic summarizing How Redis works under the hood: Redis is fast because it keeps everything in RAM and uses simple in-memory data structures. The best way to see how it…

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

#StepWhat happens
1Client sendsThe client library sends the command. Writes always go to the primary node.
2DecodeRedis decodes the RESP protocol into command, key, value and args.
3ValidateDoes the command exist? Are the args correct? Is the client authenticated and allowed?
4Find handlerLook the command up in the internal command table.
5ExecuteRun it. One command at a time, in order.
6Update RAMFind the key in the in-memory hash table and write the value.
7Expire and evictTTL keys expire lazily (on access) or actively (background sweep). If memory is full, the eviction policy frees space.
8Persist and replicateThe write is logged to AOF, captured in RDB snapshots, or copied to replicas.
9ReplyRedis 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\n

The 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 SCAN instead of KEYS * 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). With noeviction, writes fail instead.

Step 8: persist and replicate

OptionWhat it doesTrade-off
AOF (appendonly.aof)Logs every write so it can be replayedMost durable, bigger file, slower restart
RDB (dump.rdb)Point-in-time snapshot of memoryCompact and fast to load, but you lose writes since the last snapshot
ReplicasCopy writes to other nodesA crash on the primary loses nothing that was copied, and replicas can serve reads

Why it is fast

Why it helps
RAM, not diskNo disk seek on the read or write path
One threadNo locks to wait on
O(1) hash table lookupsFinding 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.