Home >Database >Redis >Why is redis so fast?

Why is redis so fast?

(*-*)浩
(*-*)浩Original
2019-11-22 09:12:345147browse

Why is redis so fast?

Redis is a pure memory database, which is generally a simple access operation. Threads take up a lot of time, and the time spent is mainly concentrated on IO, so the reading speed is fast. . (Recommended study: Redis video tutorial)

Let’s talk about IO again. Redis uses non-blocking IO, IO multiplexing, and uses a single thread to poll the descriptor. Convert the opening, closing, reading and writing of the database into events, reducing context switching and competition when switching threads.

Redis adopts a single-threaded model to ensure the atomicity of each operation and reduce thread context switching and competition.

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, Compress and store short data, another example is jump tables, and use ordered data structures to speed up reading.

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.

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);

The data structure is simple, and it is Data operations are also simple. The data structure in Redis is specially designed;

adopts a single thread to avoid unnecessary context switching and competition conditions, and there is no consumption caused by switching caused by multi-process or multi-threading. CPU, 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;

uses a multi-channel I/O multiplexing model, Non-blocking IO;

The underlying models used are different, the underlying implementation methods and the application protocols for communication with the client are different. Redis directly builds its own VM mechanism because of general system calls. For system functions, a certain amount of time will be wasted to move 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 multiplexing model

The multi-channel I/O multiplexing model uses select, poll, and epoll to monitor I/O events of multiple streams at the same time. Ability, when idle, will block the current thread. When one or more streams have I/O events, it will wake up from the blocked state, so the program will poll all streams (epoll only polls Query those streams that actually emitted events), and only process ready streams in sequence. This approach avoids a large number 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.

For more Redis-related technical articles, please visit the Introduction to Using Redis Database Tutorial column to learn!

The above is the detailed content of Why is redis so fast?. 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