Threading model
We all know that Redis is single-threaded, so why is Redis so fast? Here’s why.
Strictly speaking, Redis Server is multi-threaded, except that its request processing process is single-threaded. It is important to be clear at this point that Redis Server is not considered single-threaded.
The performance of Redis is very high, it can withstand 10W+ QPS per second, its excellent performance depends on the following aspects:
- Redis does most of its operations in memory
- The IO multiplexing mechanism is adopted
- Non-cpu intensive tasks
- Advantages of single threading
1. Pure memory operation
Redis is an in-memory database, and all of its data is stored in memory, which means that we read and write data in memory, which is very fast.
Redis uses efficient data structures, such as hash tables and skip tables, which is an important reason for its high performance.
2. Adopt the I/O multiplexing mechanism
Redis has developed its own network event handler based on the Reactor model: this processor is called a File Event Handler. The file event handler uses the I/O multiplexing program to listen for more than one socket at a time and associate different event handlers for the socket according to the task that the socket is currently performing.
When a socket is ready to perform an operation, such as accept, read, write, close, etc., file events are generated and the file event handler calls the event handler associated with the socket to handle these events.
Although the file event handler runs in a single-threaded manner, by using an I/O multiplexer to listen for multiple sockets, the file event handler implements both a high-performance network communication model and interconnects well with other Redis server modules that also run in a single-threaded manner. This preserves the simplicity of Redis’ internal single-threaded design.
3. Non-cpu intensive tasks
The disadvantage of using a single thread is obvious: you can’t use a multi-core CPU. The Redis authors mention that since most operations in Redis are not CPU intensive tasks, the bottlenecks are memory and network bandwidth.
With high concurrent requests, Redis needs more memory and higher network bandwidth, otherwise bottlenecks can easily occur in the case of insufficient memory and network latency.
Of course, if you feel that the performance of a single Redis instance is not enough to support your business, the Redis authors recommend deploying multiple Redis nodes in a cluster to take advantage of the power of multi-core cpus, rather than using multiple threads on a single instance.
4. Advantages of single threading
Based on the above features, Redis uses a single thread is enough to achieve very high performance, so Redis does not use a multi-threaded model.
In addition, the single-threaded model has the following benefits:
- Avoid the performance cost of multithreaded context switching
- Avoid performance cost caused by multiple threads accessing shared resources
So Redis is based on the above advantages, so the single thread model to complete the work of request processing.
5. Disadvantages of single threading
The biggest disadvantage of single-threaded processing is that if the previous request takes a long time, the entire Redis will be blocked and no other requests can come in until the long operation is processed and returns.
We often encounter problems of slow Redis response or long time blocking, most of which are caused by the reason that Redis handles requests in a single thread.
Therefore, when using Redis, we must avoid time-consuming operations, such as obtaining data in a way with high time complexity, obtaining too much data at one time, and increasing pressure on Redis to eliminate key due to expiration of a large number of key sets. These scenarios will block the entire processing thread until the processing is completed. Service access will be affected.
6. Multi-threaded optimization
Redis Server is multi-threaded, in addition to the request processing process is a single thread, Redis internal worker threads executing in the background, it is responsible for asynchronous execution of some of the more time-consuming tasks, such as AOF flushing per second, AOF file rewriting is completed in another thread.
After Redis 4.0, The mechanism of Lazyfree was introduced. Provides the unlink, flushall AYsc, flushDB Async commands, lazyfree-lazy-eviction, lazyfree-lazy-expire and other mechanisms to asynchronically free memory. It is mainly to solve the performance problem of releasing large memory data which causes the whole Redis to block.
When deleting a large key, it is often time-consuming to free memory, so Redis provides an asynchronous way to free memory, so that these time-consuming operations can be processed in another thread asynchronously, which does not affect the execution of the main thread and improves performance.
In Redis 6.0, Multiple threads were introduced to complete protocol parsing of request data, further improving performance. It is mainly to solve the high concurrency scenarios, single-thread resolution request data protocol pressure. After the protocol parsing of the request data is completed by multiple threads, the subsequent request processing phase is still single-threaded queued processing.
Redis is not being conservative about how good single threading is, nor is he introducing multithreading for the sake of using it. Redis author is very clear about the use of single and multi-threaded scenarios, targeted optimization.