In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you whether using single-threaded Redis will use multithreading, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Redis is a well-known in-memory database, which has a very rich application in various scenarios. Some time ago, Redis launched version 6.0, in which the multithreaded model was adopted.
Because the in-memory database used by our company is self-developed, I don't pay much attention to Redis, but because Redis is widely used, I need to know it is convenient for me to interview.
It is impossible for a candidate to have used Redis, but I have to ask him what is wrong with Ali's Tair.
So, after the introduction of Redis 6. 0, I want to know why multithreading is adopted, and what is the difference between multithreading and previous versions? Why do you use multithreading so late?
Isn't Redis already using multiplexing technology? Isn't it the so-called high performance? Why use the multithreaded model?
Why was Redis designed to be single-threaded in the first place?
As a mature distributed cache framework, Redis consists of many modules, such as network request module, index module, storage module, high availability cluster support module, data operation module and so on.
Many people say that Redis is single-threaded, so they think that the operation of all modules in Redis is single-threaded, but this is not true.
By Redis single thread, we mean that its network IO and key-value pairs are read and written by a single thread, that is to say, only the network request module and data operation module in Redis are single-threaded. Others, such as persistent storage module and cluster support module, are multithreaded.
Therefore, there is not no multithreading model in Redis. As early as Redis 4.0, multithreading has been done for some commands.
So why didn't the network operation module and data storage module use multithreading in the first place?
The answer to this question is relatively simple! Because: "there is no need!"
Why isn't it necessary? Let's start with, under what circumstances do you want to use multithreading?
1. Multi-threaded scenarios
In the process of execution, a computer program mainly needs to carry out two kinds of operations: read-write operation and calculation operation.
Among them, the read and write operation is mainly related to the Imax O operation, including the network Imax O and the disk Imax O. The calculation operation mainly involves CPU.
The purpose of multithreading is to improve the utilization rate of I _ map O and CPU by means of concurrency.
So, does Redis need to improve the utilization of Imax O and CPU through multi-threading?
First of all, we can say for sure that Redis does not need to improve CPU utilization, because Redis operations are basically memory-based, and CPU resources are not the performance bottleneck of Redis at all.
Therefore, it is totally unnecessary to improve the CPU utilization of Redis through multithreading technology.
So, what about using multithreading technology to improve the utilization of Redis? Is it necessary?
Redis is indeed an operation-intensive framework for IWeiO. During its data operation, a large number of network IWeiO and disk IWeiO will occur. There is no doubt that if you want to improve the performance of Redis, you must improve the utilization of Redis.
However, it is not the only way to use multithreading technology to improve the utilization rate of Iwhite O.
2. Disadvantages of multithreading
In many articles, we have introduced some multithreading technologies in Java, such as memory model, lock, CAS, etc., which are provided by Java to ensure thread safety in the case of multithreading.
Thread safety: a term in programming, which means that when a function or library is called in a concurrent environment, it can correctly deal with the shared variables between multiple threads, so that the program function can be completed correctly.
Like Java, all programming languages or frameworks that support multithreading have to face a problem, that is, how to solve the concurrency control problem of shared resources caused by multithreaded programming mode.
Although the use of multithreading can help us improve the utilization of CPU and Imax O, the concurrency problems caused by multithreading also bring more complexity to these languages and frameworks. Moreover, in the multithreaded model, the switching of multiple threads will also bring some performance overhead.
Therefore, Redis does not use multithreading technology, but chooses multiplexing Ithumb O technology in order to improve the utilization rate of Istroke O.
3. Summary
Redis does not use the multithreading model in the network request module and data manipulation module for the following four reasons:
Redis operations are based on memory, and the performance bottleneck of most operations is not CPU.
Using the single-threaded model, it is more maintainable and less expensive to develop, debug and maintain.
Single-threaded model avoids the performance overhead caused by switching between threads
The use of multiplex Icano technology in single thread can also improve the utilization rate of Redis Icano.
Keep in mind that Redis is not completely single-threaded, except that the network IO of keys and key-value pairs are read and written by a single thread.
Second, the multiplexing of Redis
I believe many people are not unfamiliar with the word multiplex. I have mentioned this word enough in many previous articles.
We mentioned it when we introduced the Linux IO model, and we also mentioned it when we introduced the principle of HTTP/2.
So, what's the difference between Redis's multiplexing technology and what we introduced earlier?
Here we first talk about Linux multiplexing technology, that is, the IO of multiple processes can be registered with the same pipe, which will uniformly interact with the kernel. When the data required by a request in the pipeline is ready, the process copies the corresponding data to the user space.
Take another look at the above picture and the above sentence, and you may still need it later.
That is, multiple IO streams are processed by a single thread.
IO multiplexing includes three kinds under Linux, select, poll and epoll. Abstractly, their functions are similar, but the details are different.
In fact, all the functions of Redis's IO multiplexing program are realized by packaging the IO multiplexing function library of the operating system. Each IO multiplexing function library has a corresponding separate file in the Redis source code.
In Redis, a file event occurs whenever a socket is ready to perform connection reply, write, read, close, and so on. Because a server usually connects to multiple sockets, multiple file events can occur concurrently.
Once a request arrives, it is handed over to the Redis thread for processing, which achieves the effect of a single Redis thread processing multiple IO streams.
Therefore, Redis chooses to use multiplexing IO technology to improve the utilization of Imax O.
The reason why Redis has such a high performance is not only related to the use of multiplexing technology and single thread, but also for the following reasons:
Completely based on memory, most of the requests are pure memory operations, very fast
The data structure is simple, and the data operation is also simple, such as hash table and jump table with high performance.
The use of single thread avoids unnecessary context switching and competition conditions, and there is no CPU consumption caused by multi-process or multi-thread switching.
The multiplexing model of multi-channel Icano is used.
3. Why Redis 6.0introduces multithreading
In May 2020, Redis officially launched version 6.0. there are many important new features in this version, among which the multi-threading feature has attracted wide attention.
However, we need to remind you that the multithreading in Redis 6.0is only used in the process of dealing with network requests, while the read and write commands of data are still processed in a single thread.
However, I wonder if anyone will have such a question:
Doesn't Redis claim that single threading also has high performance?
Isn't it said that multiplexing technology has greatly improved the utilization of IO, why do you still need multithreading?
Mainly because we have higher requirements for Redis.
According to estimates, Redis keeps all the data in memory, and the response time of memory is about 100 nanoseconds. For small packets, the Redis server can handle 80000 to 100000 QPS. For 80% of companies, single-threaded Redis is enough.
But with more and more complex business scenarios, some companies are prone to hundreds of millions of transactions, so they need a larger QPS.
In order to improve QPS, many companies deploy Redis clusters and increase the number of Redis machines as much as possible. But the resource consumption of this approach is huge.
After analysis, the main bottleneck that limits the performance of Redis appears in the processing of network IO, although the multiplexing technology is used before. But as we mentioned earlier, the multiplexed IO model is still essentially a synchronous blocking IO model.
The following is the process of multiplexing the select function in IO:
As we can see from the above figure, in the multiplexed IO model, the process of calling select (similarly to other functions) is blocked when processing network requests, that is, this process will block threads, and this may become a bottleneck if the concurrency is high.
Although many servers have multiple CPU cores, for Redis, because of the use of single thread, in the process of data operation, a large number of CPU time slices are spent on the synchronous processing of network IO, and do not give full play to the advantages of multi-core.
If we can use multithreading to make the requests processed by the network concurrently, the performance can be greatly improved. Multi-threading can not only reduce the impact caused by the network Iamp O wait, but also make full use of the multi-core advantage of CPU.
Therefore, Redis 6.0uses multiple IO threads to process network requests, and the parsing of network requests can be completed by other threads, and then the parsed requests are handed over to the main thread for actual memory reading and writing. Improve the parallelism of network request processing, and then improve the overall performance.
However, Redis's multi-IO threads are only used to process network requests, and Redis still uses a single thread for read and write commands.
So, after the introduction of multithreading, how to solve the thread safety problem caused by concurrency?
This is why we have mentioned many times that the multithreading of Redis 6.0is only used to process network requests, while the reading and writing of data is still single-threaded.
Redis 6.0uses multithreading only when receiving and parsing network requests and when the requested data is returned through the network. However, data read and write operations are completed by a single thread, so there is no concurrency problem.
The above content is whether the single-threaded Redis will use multithreading. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.