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

Why Redis single-threaded design can support high concurrency

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains why Redis single-threaded design can support high concurrency. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn why Redis single-threaded design can support high concurrency.

First of all, Redis may have been originally designed to make a cache to use. However, the distributed environment is complex, and many problems may be exposed, so Redis has to do cluster. After clustering, the effect may be similar to that of Memcahed, and we want to go beyond it, so we may have a storage structure of multiple data types. Just for caching, the data is lost when it has been down. Our slogan is beyond Memcahed, so we need to support data persistence. So you may have AOF and RDB, which can be used as databases. This is the efficient and reliable design of Redis, so it can be used as message middleware. These are the three main features of Redis, which can be used to do: cache, database, and message middleware.

Again, how is Redis designed to be single-threaded for processes?

According to the official test results, "How fast is Redis? "from a point of view, in the case of operating memory, CPU does not play a decisive role, but may bring some other problems. For example, locks, performance overhead caused by CPU handover, etc. We can prove this according to the data provided by the official test report. And the official data can reach 1000000 + QPS (the number of queries per second), this data is no worse than using single-process multi-threaded Memcached! Therefore, in the case of memory-based operations, CPU is not the bottleneck of Redis, the official use of single-process single-threaded design.

The reasons are as follows:

First, we ask Redis to do more with memory. It is very fast to operate memory directly, and the data is stored in memory, similar to HashMap. The advantage of HashMap is that the time complexity of search and operation is O (1).

Second, single-threaded, without the overhead caused by CPU context switching. And as mentioned above, memory operations and the multicore impact of CPU are not significant. If you use single thread directly, you don't have to consider all kinds of locks, and the related problems such as locking, unlocking, deadlock and so on will no longer exist.

Third, multiplex IO. I will talk about it in detail later. To be able to talk about this shows that I have a certain understanding of Redis. This involves the operating system-based network IO model. Reactor network mode, epoll,poll,select,kqueue and other multiplexing IO.

Fourth, rely on the second point. Because it is single-threaded, there is a problem of sequential reading and writing. You can compare the speed of random reading and writing and sequential reading and writing.

Fifth, the data structure of Redis is specially studied and designed. So the operation is simple and fast.

Sixth, Redis built its own VM mechanism. Because the general call system functions, will waste a certain amount of time.

To sum up the above, Redis has the characteristics of single thread and high performance.

Finally, one more point, Redis is a single-process and single-threaded design, not that it cannot be multi-process and multi-threaded. For example, the backup will fork a new process to operate; for example, the RDB operation based on the principle of COW is multithreaded.

At this point, I believe you have a deeper understanding of why Redis single-threaded design can support high concurrency, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Servers

Wechat

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

12
Report