In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains why the redis speed of a single thread is so fast. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn why the redis speed of a single thread is so fast.
Catalogue
Redis stand-alone QPS
Why so fast?
Memory database
Simple data structure
Single thread
IO multiplexing
Redis stand-alone QPS
. / redis-benchmark-t set,lpush-n 100000-qSET: 82101.80 requests per secondLPUSH: 82440.23 requests per second
Testing SET and LPUSH 100,000 times on your computer, you can find that SET and LPUSH are probably more than 8w per second, close to the official 10w qps write on a single machine.
Why so fast?
Memory database
Redis is completely memory-based, and the vast majority of requests are purely memory operations, so it's very fast.
Simple data structure
Redis currently supports five data types (string, list, hash, set, zset). The data structure is relatively simple and the operation is relatively fast.
Sds data structure
For string, redis uses SDS to organize data:
The core idea of this kind of data is to exchange space for time.
Space pre-allocation: when space is expanded, not only the required space is allocated, but also additional space is allocated.
If the sds length is less than 1m after allocation, then the same amount of extra space will be allocated. Suppose a key modified len=13, then free=13 will also be allocated, and finally buf=13+13+1=27
If the allocated len is greater than or equal to 1m, then the additional fixed allocation is 1m, assuming the modified len=30M, assigning free=1M, and finally buf=30M+1M+1byte
Release of inert space
Suppose there is a string of len=13,free=13. At this time, if the character becomes shorter len=10, then the extra 3 byte space will not be recycled. Put it in free first. At this time, free=16
Through this allocation method, the number of memory requests can be reduced in some scenarios, thus achieving a certain speed.
Jump table
The ordered collection of redis adopts the data structure of jump table and accelerates access to other nodes through layers.
Each node will have a random layer height. For example, the o1 node can jump to o3 directly through the L4 layer, and the ordered set with a span of 2 redis is used to speed up the access between the nodes.
Single thread
Redis adopts single-thread model. The advantage of single-thread is that it avoids the problem of multi-thread competition for data, locking and context switching.
According to the official explanation, the bottleneck of redis is not cpu, but memory or network bandwidth, and then single threading is adopted. By single thread, we mean that only one thread is used when processing network requests, and redis itself uses additional threads when persisting.
Multithreading of redis4.0
Redis4.0 also supports multithreading at the beginning, of course, only for some commands, such as UNLINK, FLUSHALL, ASYNC, FLUSHDB. The purpose of introducing these is: in some cases, to improve efficiency as much as possible. If there is a key as large as dozens of M, when DEL this key, it may block for a short time. If you delete it with unlink, you will just delete the key at first, and the real value will be deleted by the background thread.
IO multiplexing
Redis adopts non-blocking IO multiplexing technology. Redis itself is an event driver, and redis abstracts socket into file events. IO multiplexing here means that file event handlers listen for related sockets (accept, read, write, close) in a single-threaded manner.
Because the IO multiplexer is a single thread, when multiple socket arrives, they must be queued, and they are always processed sequentially in the form of queues.
C10K problem
In the absence of IO multiplexing, suppose there are 10000 client connections (fd1-10000), but only one client has sent data, but the computer does not know which fd has data and can only traverse it 10000 times, each time it has to fall into the kernel, which is expensive, and in fact 9999 times is a waste.
IO multiplexing
IO multiplexing means that multiple network IO multiplexes a process or thread for multiple TCP connections. The biggest advantage of this model is that there is no need to create a process or thread for each connection. The more classic models are select, poll and epoll.
Select:select (fds), which gives the fds to the kernel at once, and then the kernel tells which fd is readable and writable (the kernel traverses itself instead of the user, turning multiple system calls into one system call). The maximum fds is 1024, which also determines that the maximum concurrency of the select model is 1024.
Poll: similar to select, except that the concurrency is more than 1024 and can be more.
Epoll: the disadvantage of select and poll is that the kernel traversal time complexity is O (n), although the user state does not have to traverse, reducing the number of times trapped in the kernel, but the kernel still has to traverse. The advantage of epoll is that the kernel does not need to traverse. When users pass fds to the kernel, they then rely on hardware interrupts. For example, when the network card has data coming, the interrupt will tell cpu,cpu which fd has arrived.
Redis defaults to epoll unless the system does not support it.
Summary
Redis is a memory database
Special data structure of redis
Single thread to avoid lock competition
Io multiplexing
The above four points are the main reasons why single-threaded redis is fast.
Thank you for reading, the above is the content of "Why single-threaded redis is so fast". After the study of this article, I believe you have a deeper understanding of why single-threaded redis is so fast, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.