Why Redis is single-threaded, and the three major reasons for the fast high concurrency Detailed

### ** Redis high concurrency and fast memory is ** 1.redis reason, very fast read and write speeds of memory based; 2.redis is single-threaded, eliminating the need for a lot of time thread context switch; 3. redis using multiplexing techniques, you can handle concurrent connections. Internal achieve non-blocking IO using epoll, epoll + uses a simple event framework for their own implementation. Epoll in reading, writing, close, connections are transformed into an event, and then use epoll multiplexing characteristics, and never wasted little time on the io. ! [File] (https://img2018.cnblogs.com/blog/1202601/201911/1202601-20191125170338641-645055080.jpg) ### ** Why Redis is single-threaded ** ##### ** 1 . ** the official answer is because Redis-based memory bottleneck operation, CPU is not the Redis, Redis bottlenecks are most likely to be the size of the machine memory or network bandwidth. Since the single-threaded easy to implement, and the CPU does not become a bottleneck, it is logical to adopt a single-threaded program. ##### ** 2. ** For redis performance indicators of performance, the official website also has ordinary notebook easily handles hundreds of thousands of requests per second. ##### ** 3. ** ###### ** detailed reasons 1) does not require the performance of a variety of locking the Redis consumption * not all data structures are simple Key-Value, as well as list, hash and other complex structures which have It may be very fine-grained operations, such as adding an element behind a long list, add or delete an object in the hash them. These operations may need to add a lot of locks, resulting in synchronization overhead is greatly increased. In short, in the single-threaded case, we will not have to consider various issues lock, lock lock release operation does not exist, not because of a deadlock may occur and cause performance overhead. ###### ** 2) ** single-threaded multi-process single-threaded cluster solution might in fact very strong, very high efficiency per core, multi-threaded nature can have higher performance than single-threaded cap, but in today's computing environment, even a single multi-threaded cap often can not meet the need for further exploration of multi-server clustered programs that are multithreaded technology is still unobtainable. ** So single-threaded, multi-process clusters would be a stylish solution. ** ### ** 3) CPU consumption ** single-threaded, to avoid unnecessary context switches and competitive conditions, there is no multi-process or multi-threaded switching lead is consumed CPU. However, if the CPU becomes the bottleneck Redis, or do not want to let other server CUP nuclear idle, how to do? Can be considered more than a few Redis process, Redis is a key-value database, there is no constraint between is not a relational database, data. As long as the client distinguish which key on which Redis process it. ### ** Redis advantages and disadvantages of single-threaded ** ##### ** 1. ** * advantages of single-process single-threaded code clearer, simpler processing logic * do not have to consider a variety of lock problem does not exist lock release the lock operation is not possible because of a deadlock caused by the consumption * performance multi-process or multi-threaded switching due to the absence consume CPU ##### ** 2. Single-threaded process drawbacks ** * You can not play a multi-core CPU performance, but can open multiple instances of Redis single to perfect; ### ** IO multiplexing ** redis IO network using multiplexing techniques when multiple connections to ensure high throughput of the system. Multiple - refers to a plurality of socket connections, multiplexing - refers to a multiplex thread. There are three main multiplexing technology: select, poll, epoll. epoll is the latest multiplexing technology is currently the best. Here "multiplexing" refers to multiple network connections, "multiplexing" refers to reuse the same thread. Multi-channel I / O multiplexing technique that allows efficient processing a plurality of single thread connection request (IO minimize the time consumed by the network), and the data in memory Redis operation is very fast (within a memory operation will not be here performance bottleneck), the above two main Redis created with high throughput. ! [File] (https://img2018.cnblogs.com/blog/1202601/201911/1202601-20191125170338916-1252634732.jpg) ### ** Redis high concurrency quick summary ** [file] (https:! // img2018.cnblogs.com/blog/1202601/201911/1202601-20191125170339448-1316148773.jpg) 1. Redis is pure in-memory database, it is generally simple access operation, the thread take up a lot of time to spend time focused on IO on so fast read speed. 2. To tell you the IO, Redis using a non-blocking IO, IO multiplexing, using a single thread to poll the descriptor, will open the database, close, read, write, are converted into the event, reducing the thread switching time context switching and competition. 3. Redis uses a single-threaded model to ensure that each of the atomic operation also reduces context switching and competition thread. 4. Further, the data structure also helped a lot, using the Redis entire hash structure, high reading speed, there are some special data structure, stored data is optimized, such as the compression tables, data compression short storage, Again, jump table, using an ordered data structure to speed read. 5. Another point, Redis event-splitter own implementation of more efficient internal use of non-blocking execution mode, the handling capacity is relatively large. > This article from the blog article multiple platforms [OpenWrite] (https://openwrite.cn?from=article_bottom) released!

Guess you like

Origin www.cnblogs.com/chuyang2017/p/11928952.html