NOTE
3.1 Redis Thread Model
Redis's Reactor model, its single-threaded command execution, why it is fast, and Redis 6.x multi-threaded I/O.
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’s consumption queue is single-threaded, which is why Redis is called a single-threaded model.
- 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.
- To respond with client data, the I/O multiplexer detects the write event and writes back to the client.
2. Redis Single Thread
2.1. What Does Single Thread 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.

Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub