Home >Database >Redis >Detailed explanation of why Redis is single-threaded, highly concurrency and fast

Detailed explanation of why Redis is single-threaded, highly concurrency and fast

WJ
WJforward
2020-06-06 17:00:502407browse

Detailed explanation of why Redis is single-threaded, highly concurrency and fast

The reasons for high concurrency and speed of Redis

1.redis is based on memory, and the memory read and write speed is very fast;

2.redis It is single-threaded, which saves 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.

Detailed explanation of why Redis is single-threaded, highly concurrency and fast

Why Redis is single-threaded

1. Official answer

Because Redis is a memory-based operation , CPU is not the bottleneck of Redis. The bottleneck of Redis is most likely the size of machine memory or 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 , as well as complex structures such as list and hash. These structures may perform very fine-grained operations, such as adding an element after 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 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.

Advantages and disadvantages of Redis single thread

1. Advantages of single process and single thread

The code is clearer and the processing logic is simpler. There is no need to consider various lock issues, they do not exist There is no performance consumption due to possible deadlocks in locking and releasing lock operations. There is no CPU consumption due to switching caused by multiple processes or multi-threads

2. The disadvantages of single process and single thread

Unable to Give full play to multi-core CPU performance, but it can be improved by opening multiple Redis instances on a single machine;

IO multiplexing technology

redis uses network IO multiplexing technology to ensure that multiple connections time, high throughput of the system.

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 The operation will not become the performance bottleneck here). The above two points mainly contribute to the high throughput of Redis.

Detailed explanation of why Redis is single-threaded, highly concurrency and fast

Redis High Concurrency Quick Summary

Detailed explanation of why Redis is single-threaded, highly concurrency and fast1. Redis is a pure memory database, generally They are all simple access operations. The thread takes up a lot of time. 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 and short data. Compressed storage, another example, skip tables, uses ordered data structures 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.

Related references:Redis Tutorial

The above is the detailed content of Detailed explanation of why Redis is single-threaded, highly concurrency and fast. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51dev.com. If there is any infringement, please contact admin@php.cn delete