Methods for fast redis query speed: 1. redis query is completely based on memory; 2. The data structure in redis is simple; 3. redis uses a single thread; 4. redis uses a multi-channel I/O multiplexing model; 5. Redis builds a VM mechanism.
#In the career of a programmer, setting up cache will remind me of it, controlling concurrency will remind me of it, doing some compressed storage will still remind me of it, always staying in the know The starting point, but there is no in-depth exploration and summary of the reasons why it is so popular. Recently, the time, place and people are favorable, and I have sorted out every detail of it.
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 reuse model:
(1) Multi-channel I/O O Multiplexing model
The multi-channel I/O multiplexing model uses select, poll, and epoll to monitor the I/O events of multiple streams at the same time. When idle, the current The thread blocks. When one or more streams have I/O events, it wakes up from the blocked state, so the program polls all streams (epoll only polls those streams that actually emitted events). And only process the 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 query fast?. For more information, please follow other related articles on the PHP Chinese website!