The life of a Redis query
Follow one command from your client to a reply — and see why single-threaded is a feature, not a bug.
A Redis command travels from the client over a TCP socket, into Redis's single-threaded event loop, where it's parsed from the RESP protocol, executed against an in-memory data structure in microseconds, and sent back as a reply. Being single-threaded makes every operation atomic and lock-free; pipelining batches commands to cut round trips; and persistence (RDB/AOF) is kept off the hot path so it never slows the query.
Client sends the command
Your Redis client holds a persistent TCP connection to the server (default port 6379) and sends the command encoded in RESP, Redis's simple text-based serialization protocol. Reusing one connection avoids paying for a new TCP handshake on every command. The bytes for something like GET user:42 travel over the socket to the server.
The event loop receives it
Redis runs a single-threaded event loop built on the OS's I/O multiplexing (epoll on Linux, kqueue on BSD/macOS), which watches thousands of connections at once and wakes only for the ones with data ready. When your command's bytes arrive, the loop notices the socket is readable and pulls them in. One thread serves every client without spawning a thread per connection.
Parse the command
The reader parses the incoming RESP bytes into a command name and its arguments, then looks up the command in Redis's command table to find the function that implements it. This step is pure, cheap bookkeeping — validating the argument count and types before anything touches the data. Parsing is trivially fast compared to any network round trip.
Execute in memory
The command function runs directly against the in-memory data structure for that key — a hash table for strings, a skip list for sorted sets, a linked list or ziplist for lists. Because the data lives entirely in RAM, the operation completes in microseconds with no disk access. And because only one command executes at a time, the operation is inherently atomic — no locks, no half-updated state, no race conditions.
Reply to the client
Redis encodes the result back into RESP and writes it to the client's socket, and the event loop ships it back over the same TCP connection. The client decodes the reply and hands it to your code. The whole journey — parse, execute, reply — is so fast that for a single command the dominant cost is almost always the network round trip, not Redis itself.
Persistence, off the hot path
Durability never blocks the query. RDB snapshots fork a child process that writes a point-in-time dump while the parent keeps serving commands, and the AOF append-only log records writes and is flushed to disk on a configurable schedule. Because this work happens in the background — often in a forked child — persistence and replication run without stalling the single-threaded command path. Pipelining complements this by letting clients batch many commands into one round trip.
Why trace a single Redis query?
Redis has a reputation for being almost unreasonably fast — sub-millisecond operations, hundreds of thousands of requests per second from a single node — and that reputation confuses people who learn one surprising fact: Redis processes commands on a single thread. Every intuition from multi-core, multi-threaded server design says that should be a bottleneck. Following one command through its whole life explains why it isn't, and in the process reveals the design choices that make Redis what it is.
The journey is short by design: client → TCP socket → single-threaded event loop → parse → in-memory data-structure operation → reply. Each stage is deliberately kept lean, and the two things that could slow it down — locking and disk persistence — are engineered *out of* the hot path entirely. Understanding this path is also how you understand Redis's performance guarantees: why operations are atomic, why the network round trip usually costs more than the work itself, and why pipelining can give you an order-of-magnitude throughput jump for free.
From client to the event loop
A command's life begins at the client. Your application (or redis-cli) keeps a persistent TCP connection to the Redis server, by default on port 6379. Keeping the connection open matters: a new TCP handshake per command would add a full round trip of latency to an operation that otherwise takes microseconds, so clients reuse connections and typically pool them.
The command itself is encoded in RESP — the REdis Serialization Protocol — a deliberately simple, human-readable text format. A GET user:42 becomes a short, length-prefixed array of bulk strings. RESP is easy to parse quickly, which keeps this stage cheap.
Those bytes arrive at the server's event loop, and this is the heart of Redis's design. Rather than spawning a thread per connection, Redis runs a single-threaded event loop built on the operating system's I/O multiplexing primitives — epoll on Linux, kqueue on BSD and macOS. The loop registers every client socket with the kernel and then sleeps until the kernel reports which sockets have data ready. This lets one thread efficiently watch thousands of concurrent connections, waking only to do actual work. The key distinction: Redis handles *concurrency* (many connections in flight) without *parallelism* (many commands executing simultaneously). One thread, thousands of clients, one command at a time.
Parse, execute, reply
Once the loop pulls a command's bytes off a socket, three quick steps finish the job.
Parse. The RESP bytes are read into a command name and its arguments, and Redis looks the name up in its command table — a registry mapping GET, ZADD, LPUSH, and every other command to the C function that implements it. It validates the argument count and basic types here. This is pure bookkeeping and essentially free compared to any network hop.
Execute. The command function runs directly against the in-memory data structure backing that key. This is where Redis's "data-structure server" nature shows: a string lives in a hash table, a sorted set in a skip list plus hash table, a list in a linked list or compact ziplist, a set in a hash table or int-set. Because every structure is in RAM, the operation completes in microseconds with zero disk access. And because the single thread runs one command through to completion before starting the next, every operation is inherently atomic — there is no interleaving, so there are no locks, no mutexes, and no race conditions to reason about. This is a profound simplification: multi-key primitives like INCR or SETNX are atomic for free, purely because of the execution model.
Reply. The result is encoded back into RESP and written to the client's socket, and the event loop ships it home over the same connection. For a single command, the parse-execute-reply work is so small that the network round trip almost always dominates the total latency you observe.
Why single-threaded is fast, and how pipelining helps
The natural objection is: surely one thread wastes a modern multi-core machine? The answer is that Redis's bottleneck is not the CPU — it's memory and network. For the tiny, in-memory operations Redis performs, the cost of coordinating multiple threads would *exceed* the work being coordinated. Going single-threaded buys three concrete wins:
- No lock contention. Multi-threaded data structures need mutexes; locking, unlocking, and waiting on contended locks burns cycles and can serialize execution anyway. Redis pays none of this.
- No context-switching. Threads bouncing on and off cores thrash CPU caches. One thread stays hot.
- Simplicity and atomicity. The whole codebase — and your mental model as a user — is dramatically simpler, and atomicity comes for free.
Because a single core handles Redis so well, the standard way to use *all* your cores is to run multiple Redis instances (or Redis Cluster) rather than multi-threading one. (Modern Redis does offload some *network I/O* to helper threads, but the command execution core stays single-threaded.)
The single-threaded model also makes pipelining the key performance tool. Normally a client sends a command and *waits* for the reply before sending the next, so throughput is capped by round-trip latency. Pipelining lets the client fire off many commands without waiting, then read all the replies together — collapsing N round trips into one. Since the round trip was the dominant cost, this often multiplies throughput dramatically, all while Redis still executes each command atomically in order.
Keeping persistence off the hot path
A single-threaded, in-memory server raises an obvious worry: if everything is in RAM and one thread does all the work, how does Redis save data to disk without freezing the command path? The answer — keeping durability *off the hot path* — is the final piece of the design.
Redis offers two persistence mechanisms, and both are engineered not to block command processing:
- RDB (snapshotting). At intervals, Redis takes a point-in-time snapshot of the dataset. Crucially, it does this by `fork()`-ing a child process: the child inherits a copy-on-write view of memory and writes the snapshot to disk, while the parent keeps serving commands uninterrupted. The OS's copy-on-write means the fork is cheap and the parent's throughput is barely affected.
- AOF (append-only file). Redis logs every write command to a file, which can be replayed to reconstruct state after a restart. The flush to disk is on a configurable schedule (every second is the common default), trading a tiny durability window for speed rather than
fsync-ing on every write.
Replication works on the same principle — a replica gets an initial snapshot and then a stream of commands — all handled without stalling the primary's event loop. The unifying design philosophy is clear: the command path stays lean and predictable, and every expensive, slow, or blocking activity — persistence, replication, big snapshots — is pushed into the background or a forked child. That discipline is exactly why Redis can be both durable *and* deliver the microsecond, single-threaded speed that makes it the default in-memory store of the industry.
The client sends a command over a persistent TCP connection encoded in RESP (Redis Serialization Protocol).
A single-threaded event loop (built on epoll/kqueue) handles all connections, processing one command at a time to completion.
Because everything is in RAM and one command runs at a time, each operation is atomic and lock-free — no locks, no race conditions.
Single-threaded is fast here because Redis is memory-bound, not CPU-bound: it avoids lock contention and context-switching overhead entirely.
Pipelining lets a client send many commands without waiting for each reply, collapsing N round trips into one.
Persistence (RDB snapshots, AOF log) and replication run off the hot path — often via a forked child — so durability doesn't stall command processing.
Quick quiz
Why is Redis being single-threaded not a performance problem for most workloads?
What does pipelining achieve?
Redis vs Memcached
Two in-memory key-value stores — one a lean cache, one a data-structure server.
Comparing key-value stores
Four stores that all map keys to values — and almost nothing else in common.
Where do we cache — and how it goes wrong
Caching lives at every layer. So do the ways it can bite you.
The data structures behind databases
Every database is a clever arrangement of a few data structures chosen for how you read and write.