Why does Redis introduce multithreading again? Could the author not escape the "True Fragrance Theorem"?

I believe you must have seen Redis in single-threaded mode more than once, but to be honest, it is just an old version. This question is an interview question from a big brother, and I shared it with me. Thinking about it, I know that redis 6.0 has always been single-threaded before, and multi-threading was added to version 6. It is not very clear. I have summarized this article after inquiring and searching.

1. Problem overview

The version after Redis 6.0 abandoned the design of the single-threaded model. Redis, which originally used single-threaded operation, also began to selectively use the multi-threaded model. At first glance, the author of Redis is so awesome, but he can’t escape the “law of true fragrance”.

Think about it carefully, this problem can actually be divided into two main problems:

(1) Why did Redis choose the single-threaded model in the first place (the benefits of single-threaded)?

(2) Why did Redis add multithreading after 6.0 (in some cases, single thread has its shortcomings, and multithreading can solve it)?

In fact, it is not that the author did not escape the true fragrance theorem, but as time passed, more and more problems appeared. The original design was definitely a bit out of date, and it was necessary to make changes. OK, with two questions, let's analyze it carefully.

2. Why did Redis use single thread in the first place?

Whether it is single-threaded or multi-threaded, it is to improve the development efficiency of Redis, because Redis is a memory-based database and has to handle a large number of external network requests, which inevitably requires multiple IOs. Fortunately, Redis uses many excellent mechanisms to ensure its high efficiency. So why is Redis designed to be single-threaded? It can be summarized as follows:

(1) IO multiplexing

Let's take a look at the top-level design of Redis.

 

FD is a file descriptor, which means that the current file is in a readable, writable or abnormal state. Use the I/O multiplexing mechanism to monitor the readable and writable status of multiple file descriptors at the same time. You can understand it as having the characteristics of multi-threading.

Once a network request is received, it will be processed quickly in memory. Since most of the operations are pure memory, the processing speed will be very fast. That is to say, in single-threaded mode, even if there are a lot of connected network processing, because of IO multiplexing, it can still be ignored in high-speed memory processing.

(2) High maintainability

Although the multithreading model performs well in some aspects, it introduces the uncertainty of program execution order and brings a series of problems with concurrent reading and writing. In single-threaded mode, debugging and testing can be carried out conveniently.

(3) Based on memory, the efficiency is still high in a single-threaded state

Multithreading can make full use of CPU resources, but for Redis, because of its memory speed, it can handle 100,000 user requests in one second. If 100,000 user requests per second cannot be satisfied, then we will You can use Redis sharding technology to hand it over to different Redis servers. This cooking avoids the introduction of a large number of multi-threaded operations in the same Redis service.

And based on memory, unless AOF backup is to be performed, basically no I/O operations will be involved. The reading and writing of these data only occurs in memory, so the processing speed is very fast; it may not be a good solution to process all external requests with a multi-threaded model.

Now we know that it can basically be summarized in two sentences, based on memory and using multiplexing technology, single-threaded speed is very fast, but also to ensure the characteristics of multi-threading. Because there is no need to use multiple threads.

Third, why the introduction of multi-threading?

I just talked about the benefits of using single thread. Now that the topic has changed, I have to talk about why we need to introduce multiple threads. Don’t get used to it. The introduction of multi-threading shows that in some aspects of Redis, single-threaded has no advantage.

Because the read/write system calls to read and write to the network take up most of the CPU time during Redis execution, the performance will be greatly improved if the network read and write is made in a multi-threaded manner.

The multi-threaded part of Redis is only used to process network data reading and writing and protocol analysis, and the execution of commands is still single-threaded. The reason for this design is that Redis does not want to become complicated due to multi-threading, and it needs to control the concurrency problems of key, lua, transaction, LPUSH/LPOP, etc.

Redis has added some delete operations that can be processed asynchronously by other threads in the latest versions, that is, the UNLINK, FLUSHALL ASYNC and FLUSHDB ASYNC we mentioned above. Why do we need these delete operations, and why do they need to pass Asynchronous processing in a multi-threaded way?

We know that Redis can use the del command to delete an element. If this element is very large, it may occupy tens of megabytes or hundreds of megabytes, then it cannot be completed in a short period of time. This requires multi-threaded asynchronous support.

 

Now the deletion can be done in the background.

Four, summary

Redis chooses to use the single-threaded model to process client requests mainly because the CPU is not the bottleneck of the Redis server, so the performance improvement brought by the multi-threaded model cannot offset the development and maintenance costs it brings. The performance bottleneck of the system is also mainly Network I/O operations; Redis introduces multi-threaded operations for performance reasons. For some large key-value pair deletion operations, releasing memory space through multi-threading non-blocking can also reduce the blocking time of the Redis main thread. , Improve the efficiency of execution.

In one sentence: Single thread was used before because of the fast memory speed, and multiplexing has the effect of multiplexing, which is enough. Now it is introduced because some operations need to be optimized, such as delete operations, so Introduced multithreading.

 

 

Guess you like

Origin blog.csdn.net/Crystalqy/article/details/108141531