NOTE
3.1 Redis Thread Model
Redis organizes requests through sockets, I/O multiplexing, an event dispatcher, and handlers. These notes examine its threading model through those components.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Redis Reactor Model

- It consists of four parts: multiple sockets, I/O multiplexing, the file-event dispatcher, and event handlers.
- The file-event dispatcher consumes events from the queue on a single thread, which is why Redis is described as single-threaded.
- The Redis server opens a server socket to listen for client connections and registers this socket with the I/O multiplexer.
- After a client connects, the I/O multiplexer detects the accept event and pushes it into the queue; the file-event dispatcher gives the event to the accept handler, which creates a new socket and registers it with the I/O multiplexer.
- When the client requests data, the I/O multiplexer detects the read event and pushes it into the queue; the file-event dispatcher gives the event to the read handler, which reads the data, parses it into a command, and pushes it into the queue; the file-event dispatcher gives the event to the command-request handler, which processes it and pushes the result into the queue; the file-event dispatcher gives the event to the command-reply handler, which writes it to the socket.
- When sending a response to the client, the I/O multiplexer detects the write event and writes back to the client.
2. Redis’s Single-Threaded Model
2.1. What Does Single-Threaded Mean?
- Redis has a single-threaded architecture, and command execution is completed in this thread.
2.2. Advantages of a Single Thread
- A single thread means no thread-context switching and no locking problems for concurrent reads and writes.
2.3. Disadvantages of a Single Thread
- Once this thread blocks, Redis cannot respond.
2.4. Why Redis Is So Fast Despite Being Single-Threaded
- First, Redis is an in-memory database, so all operations are naturally fast because they occur in memory.
- Second, although it is single-threaded, it uses an I/O multiplexing model, so even one thread can serve multiple clients.
- A single thread also means no thread-context switching.
3. Redis 6.x Multi-Threaded I/O
- Multi-threaded I/O is only used to handle network-data reads/writes and protocol parsing; command execution remains single-threaded.

4. References
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub