Quick revision
The short answer
Redis serves thousands of connections with one thread by running an event loop: it asks the operating system (epoll on Linux, kqueue on BSD and macOS) which sockets are ready, reads and parses the waiting requests, and executes the commands one at a time on in-memory data, with no locks. Because commands never interleave, each command is atomic, but one slow command makes every other client wait for its whole duration.
What to remember
In this answer, we saw that Redis serves many connections with one thread by letting epoll or kqueue report ready sockets, then reading, parsing and executing commands one at a time on in-memory data. It is fast because commands are short and there are no locks or context switches, so the real limit is usually syscalls and the network. The single thread gives every command atomicity for free, and it also means a single O(N) command blocks everyone, which is why we avoid KEYS and whole-collection reads and scale out with more instances.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Redis serves thousands of connections with one thread by running an event loop: it asks the operating system (epoll on Linux, kqueue on BSD and macOS) which sockets are ready, reads and parses the waiting requests, and executes the commands one at a time on in-memory data, with no locks. Because commands never interleave, each command is atomic, but one slow command makes every other client wait for its whole duration.
Why this question is asked
Most of us learn early that servers need many threads to serve many clients, so the claim that Redis does it with one thread sounds wrong at first. Interviewers ask about it because the design explains almost everything we see in production: why INCR needs no lock, why a single KEYS call can freeze an application, and why adding CPU cores to the host does not make one Redis instance faster. In this answer, we will recap what an event loop is, follow one pass of the Redis loop, see why it is fast, and then look at the two consequences of the single thread: atomic commands and head-of-line blocking.
1A Quick Recap of the Event Loop
A classic server gives each connection its own thread. The thread blocks on read() until the client sends something, handles the request and blocks again. With 10,000 clients we would have 10,000 threads, each with its own stack, and the operating system would spend real effort switching between them.
An event loop turns this around. The server keeps all sockets in non-blocking mode and makes one system call that says: tell me which of these sockets have data to read or room to write. On Linux this call is epoll_wait, on BSD and macOS it is kqueue. The call returns a short list of ready sockets, the server handles exactly those, and then asks again. One thread can watch tens of thousands of sockets this way, because idle connections cost nothing while they are idle.
Redis does not use a general library such as libuv for this. It has its own small event library called ae, which wraps epoll, kqueue, evport on Solaris, or plain select as a fallback, and also runs timed events such as the serverCron housekeeping job.
2One Pass Through the Redis Loop
Let us follow a client that sends SET user:1 alice. The bytes arrive on its socket, and the next call to epoll_wait reports that socket as readable. Redis reads the bytes into the client's query buffer and parses them using the RESP protocol into a command name and arguments.
Then Redis looks up the command, checks permissions and executes it directly against the in-memory dictionary. There is no lock to take, because no other thread touches the data. The reply, +OK, is not sent straight away; it is appended to the client's output buffer. Before the loop goes back to waiting, Redis writes the pending replies to their sockets (and if a socket cannot take everything, it registers a write event and finishes later). So one pass handles every ready client, one command at a time, and then the loop starts over.
- Wait for ready socketsepoll_wait or kqueue returns readable and writable sockets
- Read and parsebytes go into the query buffer, RESP is parsed into commands
- Execute one commandruns against in-memory data, no locks
- Queue the replyappended to the client's output buffer
- More parsed commands?Yesexecute the next oneNogo on to writing
- Write repliesflush output buffers to sockets
- Run timed events
serverCron: expiry, rehashing, stats
7 steps in order. Play them, or step through them one at a time.
Every command, from every client, passes through the same single step in the middle.
Notice that the only waiting happens in one place, the epoll call, and only when there is nothing to do. When the server is busy, the loop never sleeps.
3Why One Thread Is So Fast
The first reason is that the data lives in memory. A GET does not wait for a disk; it follows a hash table lookup and copies a few bytes. Most commands are O(1), such as GET, SET, INCR and HGET, or O(log N), such as ZADD on a sorted set, and the structures are compact, so a typical command finishes in about a microsecond.
The second reason is what the single thread avoids. There is no lock contention, no cache lines bouncing between cores, and no context switch between commands. A multi-threaded store must protect every shared structure, and on very short operations that protection can cost more than the operation itself.
So where does the time actually go? For small commands it goes mostly to system calls: read() and write() on sockets and protocol parsing. That is why a single instance commonly serves well over 100,000 simple operations per second, and why the usual limits are network syscalls and memory access, not the CPU cost of the commands themselves.
4Atomicity for Free
Because the main thread runs one command to the end before it starts the next, a command never interleaves with another. Two clients that both run INCR page:views at the same moment are simply ordered; one goes first, the other second, and no update is lost.
This is why INCR, SET with NX and Lua scripts need no extra locking. A Lua script, or a function in Redis 7, is executed as one unit on the same thread, so the read, the decision and the write inside it cannot be interrupted by another client.
5The Price: Head-of-Line Blocking
The same rule has a hard side. While one command runs, every other client waits, however cheap its own command is. If one client runs KEYS * on an instance with 20 million keys, the main thread walks all 20 million entries, which can take several seconds, and for those seconds no GET, no SET and not even a PING is answered.
The usual offenders are O(N) commands on large data: KEYS, HGETALL on a hash with a million fields, LRANGE key 0 -1 on a long list, SMEMBERS on a huge set, and DEL of a large key, because freeing millions of elements also takes time. The fixes follow from the cause: use SCAN, HSCAN and paged ranges instead of whole reads, and UNLINK instead of DEL so the memory is freed by a background thread.
Finally, the single thread sets a ceiling. To use more cores for command execution we need more Redis processes, through Redis Cluster or client-side sharding. I/O threads, which Redis added in version 6, only take over socket reads and writes and protocol work; the commands themselves still run on the one main thread.
6Follow-up questions
Why does a slow Lua script also delay clients that only send PING?
A Lua script runs on the main thread as one atomic unit, and the event loop cannot go back to serve other sockets until the script returns. A PING is cheap, but it still has to be read, executed and answered by that same thread, so it waits in the queue behind the script. Redis even refuses most commands while a script runs past busy-reply-threshold (5 seconds by default, formerly lua-time-limit) and answers them with a BUSY error, leaving only SCRIPT KILL or FUNCTION KILL as a way out.
Does the single thread mean Redis cannot use multiple cores at all?
No. Background threads free memory and run fsync, child processes write snapshots, and I/O threads can handle socket work. What stays on one core is command execution, so throughput for commands scales by running more instances or shards, not by giving one instance more cores.
Is a MULTI/EXEC transaction atomic for the same reason?
Yes. EXEC runs all the queued commands back to back on the main thread, so no other client's command can run in between. The transaction does not roll back on a command error, but it is never interleaved with other clients.
7Conclusion
In this answer, we saw that Redis serves many connections with one thread by letting epoll or kqueue report ready sockets, then reading, parsing and executing commands one at a time on in-memory data. It is fast because commands are short and there are no locks or context switches, so the real limit is usually syscalls and the network. The single thread gives every command atomicity for free, and it also means a single O(N) command blocks everyone, which is why we avoid KEYS and whole-collection reads and scale out with more instances.
Open with
Redis runs its own small event library, ae, over epoll on Linux or kqueue on macOS. One main thread waits until some sockets are ready, reads and parses their requests, runs each command to completion against in-memory data, and queues the replies in output buffers that it flushes before waiting again.
Then, if they want more
It is fast because the data is in memory, most commands are O(1) or O(log N) on compact structures, and there are no locks or context switches between commands, so the cost is mostly socket syscalls rather than command CPU.
The single thread gives me atomicity for free: two INCR calls can never interleave, SET NX works as a simple lock, and a Lua script runs as one unit. The flip side is head-of-line blocking.
A KEYS call, an HGETALL on a huge hash or a DEL of a big key holds the thread, and every other client, even one sending PING, waits for it.
So in practice I avoid O(N) commands on large data, use SCAN and UNLINK, and when one instance is not enough I add shards, because I/O threads help only with socket work, not with command execution.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- Redis 7
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how the Redis event loop serves thousands of connections with one thread, why...": https://scrivio-interview-library.netlify.app/databases/redis-internals/ab70378ab4/