Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Redis multithreading

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is Redis multithreading". In daily operation, I believe many people have doubts about what is Redis multithreading. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "what is Redis multithreading"! Next, please follow the small series to learn together!

Weekend was a small classmate very angry. He wants to discuss with me whether redis is multithreaded or single-threaded. This problem would have been easier to explain, but what I encountered was a bar spirit.

The answer is obvious: redis6, which cannot escape the true fragrance theorem, introduces multithreading; before redis6, it was single-threaded.

That said, it's not a yes-and-no question, it involves a second dimension of version participation.

But this classmate wanted to slap me in the face. Didn't he know that the young lady's skin was very tender? Untouchable.

"By your logic, redis5 is single-threaded? "

"Yes. "

"What about this screenshot below? "

My classmate threw me a picture and gave me a look of contempt.

"View with top-Hp. Redis5 has 4 threads. How do you explain that? "

I don't know how to explain this to him either. Use the top command to observe, redis5 must be multithreaded, such as bgsave, aof, etc., must open a thread to operate, otherwise it would have exploded.

According to this logic, redis has never been a single process. Looking at this picture, I fell into endless worry.

"Whether Redis is a single process, mainly for Redis read and write operations." However, this sentence did not have any convincing force for the lever spirit.

"Be careful how you program. You guys are so lax. Multithreading is multithreading, and you should ask 'Is redis reading and writing multithreaded after all?'"

Let me ask you a question. I didn't want to talk to him anymore because I was ashamed of my erudition.

But his next question got me thinking.

1. How fast is redis multithreading?

What exactly is the performance improvement of redis multithreading?

It is possible to easily speed up two times. It might be easier to double the speed.

My English is not very good and I am confused by this English modification. If it's easy, why is it possible? Two times, did it increase by two times, or did it increase by two times?

Officials say how much can be improved depends on the hardware's ability.

Officially recommended, only when your CPU core count reaches 4, it is necessary to try this multithreaded Feature.

Don't stare at me with the eyes of a local tyrant. This 4core configuration has killed most companies. So Redis kindly turned multithreading off. (It seems to be a bit of a mistake)

I had to turn to my former colleagues on the front lines. Did they use this epoch-making multithreaded Redis6x in production?

The result is very satisfactory to me, no!

One response I was particularly pleased with. He said,"You're asking me if I'm running Redis6 on JDK 1.6. I am still using Redis3. "

Another reply I was more satisfied with, he said: "Get lost! "

2. How do I use it?

The new technology must be praised, otherwise no one will practice stepping on the pit, and as followers, they can only eat sh * t.

Multithreading, in theory, is bound to improve performance. A father makes money and two fathers make money, the effect is naturally different, just bitter mother.

Redis6 multi-thread start, you need to configure a parameter.

io-threads 4

When enabled, only outbound traffic uses multithreading. If you want inbound traffic to be multithreaded, you can configure the following parameters.

io-threads-do-reads yes

With just these two parameters, you can see that redis multithreading is still a little shabby.

After we turn it on, we still use top-Hp to check the related processes, and we can see that there are 3 more io_thd processes.

This part of the logic is implemented in networking.c. This file has reached more than 3k lines, which is also large enough.

3. Why is Redis multithreading again?

Using the redis-benchmark test, the throughput of a single core can reach 10w+.

One second is 10000000 nanoseconds, and a single memory operation is about 100 nanoseconds, which can reach a speed of 1000w/s. Where is Redis bottleneck?

Using perf to trace, you can find that its time consuming, mainly reflected in the sys_write system call, that is, writing data to socket.

Now that the bottleneck has been found, optimize it. The way redis chooses is to use multithreading.

I tested it with Benchmark. On a 4core machine, QPS reached 16w when the CPU was full, and did not double (compared to 9w/s for a single core).

benchmark 6379 clients 32 164519.20 requests per second 165411.09 requests per second

With such powerful hardware, it is not satisfactory to obtain such limited performance improvement.

It is not difficult to explain why so few people practice now. It's new, but it's not attractive enough.

After all, if I deploy 3 instances of redis cluster on a 4core machine, it will theoretically increase by three times.

There is quite a bit in the redis configuration file commenting on this new feature.

4. How?

As shown in the figure, a redis request, 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 redis multithreaded mode, getting, parsing commands, and outputting results are two processes that can be configured for multithreaded execution, because it is, after all, the main time-consuming point we have located.

But command execution, memory operations, is still single-threaded.

This design creates a feature.

Redis is still free of multithreaded lock contention and thread-safety issues because its data fetch step is still single-threaded and queued. Some time-consuming operations, such as keys *, hgetall, etc., should still be noted.

Redis is not a traditional reactor model. To be honest, many things can only be drilled into the head to leak a tail. It is also not a clean memcached model like master, worker, because it extracts the command execution operation. For this reason, the picture above is enough.

At this point, the study of "what is Redis multithreading" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report