Detailed explanation of Redis single-threaded model

Redis is single-threaded, which mainly means that Redis's network IO and key-value pair reading and writing are completed by one thread. This is also the main process for Redis to provide external key-value storage services.

Other Redis functions, such as persistence, asynchronous deletion, cluster data synchronization, etc., are actually executed by additional threads.

Why single-threaded Redis can be so fast

  1. Most of Redis's operations are done in memory, and it uses efficient data structures, such as hash tables and jump tables, which is an important reason for its high performance.
  2. Redis uses a multiplexing mechanism to enable it to concurrently process a large number of client requests in network IO operations and achieve high throughput.

High-performance I/O model based on multiplexing

In the case that Redis only runs a single thread, this mechanism allows multiple listening sockets and connected sockets to exist in the kernel at the same time.

The kernel will always monitor connection requests or data requests on these sockets. Once a request arrives, it will be distributed to different event handlers. The event handlers will call the corresponding functions asynchronously, and these events will be It will be put into an event queue and handed over to the Redis thread for processing, which achieves the effect of one Redis thread processing multiple IO streams.

Insert picture description here

The Redis network framework calls the epoll mechanism to let the kernel monitor these sockets. The Redis thread can be notified when the request arrives, and the corresponding processing function can be called for the occurrence of different events.

These events will be put into an event queue, and Redis single thread continuously processes the event queue. In this way, Redis does not need to keep polling whether the request actually occurs, which can avoid waste of CPU resources. At the same time, when Redis processes the events in the event queue, it will call the corresponding processing function, which implements event-based callbacks. Because Redis has been processing the event queue, it can respond to client requests in a timely manner and improve the corresponding performance of Redis.

Why not use multithreading

1. Single-threaded programming is easy and easy to maintain
2. Redis's performance bottleneck is not the CPU, mainly in memory and network
3. Multi-threading is prone to deadlock, thread context switching and other problems, and even affect performance

Why is multithreading introduced after Redis6.0?

The multithreading introduced by Redis 6.0 is mainly to improve the network IO read and write performance, because this is a performance bottleneck (network and memory) of Redis.

Although Redis 6.0 introduces multi-threading, Redis multi-threading is only used for time-consuming operations such as reading and writing network data. Execution commands are still executed in a single-threaded order, so there is no need to worry about thread safety.

Redis 6.0's multi-threading is disabled by default, and only the main thread is used. To enable it, you need to modify the redis configuration file, redis.conf:

io-thread-do-reads yes

After enabling multithreading, you need to set the number of threads, otherwise it will not take effect. Also need to modify the redis configuration file redis.conf.

io-threads 4 #官网建议4核的机器设置为2或3个线程,8核的建议设置为6个线程。