Home >Database >Redis >Why is redis single thread efficient?

Why is redis single thread efficient?

青灯夜游
青灯夜游Original
2019-06-17 14:42:596292browse

Why is redis single thread efficient?

Redis uses a memory-based key/value database that uses a single-process single-thread model. It is written in C language. The official data can reach 100,000. QPS (queries per second). So why is Redis so fast? Is it so efficient?

1. Completely based on memory, most requests are pure memory operations, very fast. The data is stored in memory, similar to HashMap. The advantage of HashMap is that the time complexity of search and operation is O(1);

2. The data structure is simple, and the data operation is also simple. The data structure in Redis It is specially designed;

3. It uses a single thread to avoid unnecessary context switching and competition conditions. There is no switching caused by multi-process or multi-threading to consume the CPU, and there is no need to consider various locks. There is no problem of locking and releasing locks, and there is no performance consumption caused by possible deadlocks;

4. Use multi-channel I/O multiplexing model, non-blocking IO;

5. The underlying models used are different, the underlying implementation methods and the application protocols for communication with the client are different. Redis directly builds the VM mechanism by itself, because if the general system calls system functions, it will waste a certain amount of time. Movement and request;

The above points are relatively easy to understand. Below we will briefly discuss the multi-channel I/O multiplexing model:

The multi-channel I/O multiplexing model is to use Select, poll, and epoll have the ability to monitor I/O events of multiple streams at the same time. When idle, they will block the current thread. When one or more streams have I/O events, they will exit from the blocked state. Wake up, then the program will poll all streams (epoll only polls those streams that actually emit events), and only process ready streams in sequence. This approach avoids a lot of useless operations.

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, which means that operations in memory do not It will become a bottleneck that affects the performance of Redis. The above points mainly contribute to the high throughput of Redis.

The above is the detailed content of Why is redis single thread efficient?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn