Redis uses a memory-based KV database with single-process single-threaded models, written in C. The official data provided is a QPS that can reach 100000 +. This data is no more Memcached than the same memory-based KV database with single-process multithreading.
The main reasons for Redis's rapid speed are:
- Completely based on memory;
- The data structure is simple, and the data operation is simple;
- Using a multiplexed I/O multiplexing model;
The first to 2nd is not detailed, mainly around the 3rd using multi-channel I/O multiplexing technology to expand.
The multi-channel I/O multiplexing model is the ability to use Select, poll, Epoll to monitor I/O events for multiple streams at the same time, blocking the current thread when idle, and waking up from the blocking state when one or more flows have I/O events, so the program polls through all the streams (epoll is to poll only those streams that actually emit the event, and the process-ready stream is only sequential, which avoids a lot of useless operations. Here "multi-path" refers to multiple network connections, "multiplexing" refers to the reuse of the same thread. Multiple I/O multiplexing techniques allow a single thread to efficiently handle multiple connection requests (minimizing network IO time consumption), and Redis operates data in memory very quickly (in-memory operations do not become a performance bottleneck here), with more than two points leading to a high throughput of Redis.
Unlike Memcached, Redis does not use Libevent directly, but accomplishes a very lightweight implementation of the common interfaces of Select, Epoll, Evport, and Kqueue. In different system calls to choose the appropriate interface, Linux under the default is Epoll. Because Libevent is heavier, more general, the code is very large, with a lot of Redis does not function, Redis in pursuit of "light" and remove the dependency, they choose to encapsulate a set.
Single-process single-thread benefits
- Clearer code, simpler processing logic
- No need to consider the various locks, there is no lock-release lock operation, there is no potential for deadlock caused by the performance of the consumption
- No multi-process or multithreading-induced switchover consumes CPU
Single-process one-thread disadvantage
- Multi-core CPU performance is not possible, but can be perfected by opening multiple Redis instances on a single machine;
Some of the other best open source software models used
- Multi-process single-threaded model: Nginx
- Single-Process multithreaded model: Memcached
Reference links
- Https://en.wikipedia.org/wiki/Epoll
- 1190000003063859#articleheader6
- [http://www.ibm.com/developerworks/cn/linux/l-async/]http://www.ibm.com/developerworks/cn/linux/l-async/
Source: Syyong
Why Redis uses single-process single-threaded mode is also so fast