Why Redis Uses a Single Thread and How Multithreading Boosts Performance
This article explains Redis's single‑threaded network model, the role of IO multiplexing, the multithreading features introduced in Redis 6.0, and how to configure IO threads to improve throughput and latency.
1. Redis Single Thread
Redis is often described as single‑threaded because its network I/O and key‑value read/write operations are handled by one thread; other tasks such as persistence, asynchronous deletion, and cluster synchronization use additional threads, so it is not strictly single‑threaded.
Multithreading introduces concurrency control and resource overhead, so Redis adopts a single‑threaded model to avoid these issues while still achieving high throughput.
The single‑threaded model can handle hundreds of thousands of operations per second thanks to in‑memory data structures (hash tables, skip lists) and an efficient event‑driven multiplexing mechanism that processes many client requests concurrently.
Before understanding multiplexing, one must grasp the basic I/O model and its blocking points; for a GET request, operations like bind/listen, accept, recv, parse, and send are network I/O, while the actual get is a key‑value operation.
2. IO Multiplexing Mechanism
Linux I/O multiplexing (select/epoll) allows a single thread to monitor multiple I/O streams. In Redis, the kernel watches many listening and connected sockets; when a request arrives, it is handed to the Redis thread, enabling one thread to handle many I/O streams.
Events are placed in a queue, and the single Redis thread continuously processes this queue, avoiding busy polling and reducing CPU waste while providing timely responses.
3. Redis 6.0 Multithreading Feature
Before Redis 6.0, only certain commands (e.g., background deletion, snapshot, AOF rewrite) used auxiliary threads; all network I/O and command processing were single‑threaded, creating a performance bottleneck. Starting with 6.0, multiple I/O threads handle network requests, while command execution remains single‑threaded.
Specific workflow:
The main thread accepts a client connection, creates the socket, and places it in a global queue for I/O threads.
I/O threads read and parse the request in parallel, while the main thread waits.
After parsing, the main thread executes the command in a single‑threaded manner.
Once execution finishes, the result is written to a buffer; the main thread blocks until I/O threads write the response back to the socket.
4. IO Multithreading Configuration
If CPU usage is low but throughput does not improve, enable I/O threading in
redis.conf:
<code>io-threads-do-reads yes</code>Set the number of I/O threads to fewer than the machine’s CPU cores; for an 8‑core server, Redis recommends 6 I/O threads:
<code>io-threads 6</code>Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.