Redis is an open source (BSD licensed), in-memory data structure server that can be used as a database, cache and message queue broker . It supports String,Hash table,List, Set, Ordered Set , bitmap, hyperloglogs and other data types . Built-in replication, Lua script, LRU eviction, transaction and different levels of disks The persistence function also provides high availability through Redis Sentinel and automatic partitioning through Redis Cluster.
Redis is based on memory and uses a single process Single-threaded model's KV database , is written in C language , the official data provided can reach 100,000 QPS (number of queries per second).
The horizontal axis is the number of connections, and the vertical axis is QPS.
We looked at the official data and found that it is really fast; as a programmer with dreams, we must know why it is so fast, you are right wrong!
Then I checked some information on the web page, and the general situation is as follows:
Completely based on memory, most of them are Some requests are purely memory operations and are 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 also There is no CPU consumption due to switching caused by multi-process or multi-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;
Use multi-channel I/O multiplexing model, non-blocking IO;
Use the underlying model differently. The underlying implementation method and the application protocol for communication with the client are different. Redis directly builds its own VM mechanism, because if the general system calls system functions, it will waste a certain amount of time to move and request;
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 thread Blocked, when one or more streams have I/O events, they will wake up from the blocking state, so the program will poll all streams (epoll only polls those streams that actually emitted events), and Only ready streams are processed sequentially, which avoids a large number of useless operations.
To sum up, there are actually three points:
Use the epoll network model and use a single thread to process requests.
#Use various high-performance data structures that meet your needs.
#redis uses memory operations; and is written in C language.
This series of articles does not focus on discussing the epoll network model, but mainly records the process of learning the redis data structure principle. Let us know why redis data processing is very fast.
The string implementation principle of redis
The list implementation principle of redis
Redis's set implementation principle
redis's sorted set implementation principle
Redis hash implementation principle
Introduction to other data types
The above is the detailed content of redis note recording-overview. For more information, please follow other related articles on the PHP Chinese website!