The reasons for high concurrency and speed of Redis
1.Redis is based on memory, and the reading and writing speed of memory Very fast;
2.Redis is single-threaded, saving a lot of time in context switching threads;
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.
The following focuses on the reasons why single-threaded design and IO multiplexing core design are fast
Why Redis is single-threaded
1.Official answer
Because Redis is a memory-based operation, the CPU is not the bottleneck of Redis. The bottleneck of Redis is most likely the size of the machine 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) There is no need for the performance consumption of various locks
The data structure of Redis is not all simple Key-Value, but also list and hash Such complex structures may perform very fine-grained operations, such as adding an element to the end of a long list, adding or deleting an object from a 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 environment, even the upper limit of single-machine multi-threading often cannot meet the needs. What needs to be further explored is multi-server clustering solutions, in which multi-threading technology is still not available.
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 CPU consumption due to switching caused by multi-process or multi-thread.
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.
IO multiplexing technologyRedis uses network IO multiplexing technology to ensure high throughput of the system when there are multiple connections.
Multi-channel - refers to multiple socket connections, and 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 "multiple" 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.
Redis High Concurrency Quick Summary1. Redis is a pure memory database, generally simple For access operations, threads take up a lot of time, and the time spent is mainly concentrated on IO, so the reading speed is fast.
2. Let’s talk about IO again. Redis uses non-blocking IO and IO multiplexing. It uses a single thread to poll the descriptor and converts the opening, closing, reading and writing of the database into events. , reducing context switching and competition when switching threads.
3. Redis adopts a single-threaded model, which ensures the atomicity of each operation and reduces thread context switching and competition.
4. In addition, the data structure also helps a lot. Redis uses hash structure throughout the process, which has fast reading speed. There are also some special data structures that optimize data storage, such as compressed tables. Short data is compressed and stored, another example is skip tables, and ordered data structures are used to speed up reading.
5. Another point is that Redis uses its own event separator, which is relatively efficient. It uses a non-blocking execution method internally and has a relatively large throughput capacity.
For more redis knowledge, please pay attention to the
redis introductory tutorialThe above is the detailed content of Introduction to redis high concurrency processing. For more information, please follow other related articles on the PHP Chinese website!