The following column Redis Tutorial will introduce to you the reasons why Redis is single-threaded and the three major reasons for high concurrency and fast speed. I hope it will be helpful to friends in need!
2.redis is single-threaded, eliminating the need for A lot of context switching thread time;
3.redis uses multiplexing technology and can handle concurrent connections. The internal implementation of non-blocking IO uses epoll, using a simple event framework implemented by epoll itself. Read, write, close, and connect in epoll are all converted into events, and then use the multiplexing feature of epoll to never waste any time on io.
Because Redis is a memory-based operation, CPU is not the bottleneck of Redis. Redis is the most bottleneck. It could be the size of the machine's memory or the network bandwidth. Since single-threading is easy to implement and the CPU will not become a bottleneck, it is logical to adopt a single-threaded solution.
2. Performance indicators
Regarding the performance of redis, the official website also has it. An ordinary notebook can easily handle hundreds of thousands of requests per second.
3. Detailed reasons
1) No performance consumption of various locks is required
The data structures of Redis are not all simple Key-Value, but also complex structures such as list and hash. These structures may Very fine-grained operations will be performed, such as adding an element at the end of a long list, adding or deleting an object from the hash. These operations may require adding a lot of locks, resulting in a greatly increased synchronization overhead.
In short, in the case of a single thread, there is no need to consider various lock issues. There is no locking and releasing lock operations, and there is no performance consumption caused by possible deadlocks.
2) Single-threaded multi-process cluster solution
The power of single-thread is actually very powerful, and the efficiency of each core is also very high. Multi-threading can naturally have a higher performance limit than single-threading, but in today's computing In the environment, even the upper limit of single-machine multi-threading often cannot meet the needs. What needs to be further explored is the multi-server clustering solution. Multi-threading technology is still not available in these solutions.
So single-threaded, multi-process cluster is a fashionable solution.
3) CPU consumption
Uses a single thread to avoid unnecessary context switching and competition conditions, and there is no switching caused by multi-process or multi-threading to consume CPU.
But what if the CPU becomes the bottleneck of Redis, or you don’t want other CPU cores of the server to be idle?
You can consider starting several more Redis processes. Redis is a key-value database, not a relational database, and there are no constraints between data. As long as the client distinguishes which keys are placed in which Redis process, it will be fine.
There is no need to consider various locks Problem, there is no locking and releasing lock operation, and there is no performance consumption caused by possible deadlock
There is no CPU consumption due to switching caused by multi-process or multi-thread
The disadvantages of single process and single thread
Cannot be used Multi-core CPU performance can be improved by opening multiple Redis instances on a single machine;
IO multiplexing technology
redis uses network IO multiplexing technology to ensure high throughput of the system during multiple connections. quantity.
Multi-channel - refers to multiple socket connections, multiplexing - refers to reusing one thread. There are three main multiplexing technologies: select, poll, and epoll. epoll is the latest and best multiplexing technology available.
Here "multi-channel" refers to multiple network connections, and "reuse" refers to reusing the same thread. The use of multi-channel I/O multiplexing technology allows a single thread to efficiently handle multiple connection requests (minimizing the time consumption of network IO), and Redis operates data in memory very quickly (in-memory operations will not become a problem here). Performance bottleneck), the above two points mainly contribute to the high throughput of Redis.
The above is a detailed explanation of redis’ high concurrency and fast speed.
The above is the detailed content of Detailed explanation of the reasons why Redis is single-threaded and the three major reasons for high concurrency and speed. For more information, please follow other related articles on the PHP Chinese website!