In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand multithreading in Redis". In daily operation, I believe many people have doubts about how to understand multithreading in Redis. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to understand multithreading in Redis". Next, please follow the editor to study!
I was very angry by a little classmate at the weekend. He wants to talk to me about whether redis is multithreaded or single-threaded. This problem would have been easier to explain, but what I encountered was a lever expert.
The answer is obvious: redis6, which cannot escape the true incense theorem, introduces multithreading, while before redis6, it was single-threaded.
In other words, this is not a question of yes or no, but also involves the participation of the version of the second dimension.
However, this classmate wants to hit me in the face. Don't you know that the little sister's skin is very tender? I can't touch it.
"according to your logic, redis5 is single-threaded?"
"Yes."
"what about the screenshot below?"
My classmate threw me a picture and sent me a look of disdain.
"use top-Hp to view. Redis5 has 4 threads. How do you explain it?"
I don't know how to explain this problem to him. Using the top command to observe, redis5 must be multithreaded, such as bgsave,aof, etc., must start a thread to operate, otherwise it will explode a long time ago.
According to this logic, redis has never been a single process. Looking at this picture, I fell into endless sorrow.
Whether Redis is a single process or not is mainly for the read and write operations of Redis. But this sentence is not convincing to the lever spirit.
"write programs to be rigorous, you people are too lax. Multithreading is multithreading, and you should ask 'whether the read and write operation of redis is multithreaded in the end'".
Let me ask you a big head. I don't want to communicate with him anymore, because I feel ashamed of my erudition.
But his next question got me into real contemplation.
1. How fast is multithreading in redis?
What is the performance improvement of multithreading in redis?
The official saying is: possible to easily speedup two times. It may be easier to increase to twice the speed.
My English is not very good, and I am very confused about this kind of English modification. Since there is easily, why is there still possible? Two times, in the end increased by 2 times, or increased to 2 times.
Officials say how much can be improved depends on the ability of the hardware.
It is officially recommended that it is only necessary to try this multi-threaded Feature when your CPU core count reaches 4.
Don't stare at me with tuhao's eyes. This configuration of 4core has killed most companies. So Redis kindly turns off multithreading. (seems to be a bit of a speech problem)
I can only turn to my former colleagues who are on the front line. Do they use this epoch-making multithreaded Redis6x in a production environment?
The result is very satisfactory to me, no!
I am very satisfied with one of the responses. He said: "you are asking a me who stays in JDK1.6, who is running a Windows version of Redis, if I use Redis6. I'm still using Redis3."
I was more satisfied with the other reply, saying, "get out!"
two。 How do I use it?
The new technology must be touted, otherwise no one will practice stepping on it, and as followers will have no choice but to eat Xiang.
In theory, multithreading will definitely improve performance. One father makes money and two fathers make money, the effect is naturally different, but the mother is suffering.
For multithreading of Redis6, you need to configure a parameter.
Io-threads 4
When enabled, only outbound traffic is multithreaded. If you want inbound traffic to be multithreaded, you can also configure the following parameters.
Io-threads-do-reads yes
Just these two parameters, you can see that the current redis multithreading is still a little shabby.
After we turn it on, we still use top-Hp to look at the relevant processes, and we can see that there are three more io_thd processes.
This part of the logic is implemented in networking.c. This file has reached more than 3k lines, which is huge enough.
3. Why is Redis multithreaded again?
Using redis-benchmark test, the throughput of single machine and single core can reach 10w +.
1 second is 1000000000 nanoseconds, a single memory operation is about 100ns, that memory operation can reach the speed of 1000w/s. So where is the bottleneck of Redis?
Using perf for tracing, you can find that its time-consuming is mainly reflected in sys_write system calls, that is, writing data to socket.
Now that the bottleneck is found, optimize it. Redis chooses to use multithreading.
I tested the 4core machine with benchmark, and when the CPU was full, the QPS reached 16w, which did not double (compared to the single-core 9w/s).
Benchmark 6379 clients 32 164519.20 requests per second 165411.09 requests per second
With such strong hardware, it is far from satisfactory to get such a limited performance improvement.
It is not difficult to explain why so few people practice now. Because it's new, it's still not attractive enough.
After all, the 4core machine, which I deployed with three instances of redis cluster, would theoretically be tripled.
There are a lot of comments on this new feature in the redis configuration file.
4. How do you do that?
As shown in the figure, a redis request is to establish a connection, then get the command of the operation, then execute the command, and finally write the result of the response to the socket.
In the multithreaded mode of redis, getting, parsing commands, and outputting the results have two processes that can be configured for multithreaded execution, because it is, after all, the main time-consuming point we locate.
But the execution of commands, that is, memory operations, is still run in a single thread.
This design creates a feature.
Redis still has no multithreaded lock contention and thread safety problems because its data reading step is still single-threaded and has to be queued to run. Some time-consuming operations, such as keys *, hgetall, etc., should still be paid attention to.
Redis is not a traditional reactor model, to be honest, if you rigidly apply the concept of many things, you can only get into the head and leak a tail. Nor is it a clean memcached-like model of master,worker, because it extracts command execution operations. The reason is that the picture above is enough.
At this point, the study on "how to understand multithreading in Redis" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.