Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Why does Redis use single-process and single-thread mode so fast?

Shulou Source: shulou.com Published: 2022-06-01 03:11:45 09月28日 Update

Redis uses a memory-based KV database using a single-process single-thread model, written in C. The official figure is qps that can reach 100000+. This data is no worse than Memcached, the same memory-based KV database with single-process multithreading.

The main reasons why Redis is fast are:

based entirely on memory;

The data structure is simple, and the data operation is also simple;

Using the multiplex I/O model;

The first and second points are not detailed, mainly around the third point using multi-channel I/O multiplexing technology to expand.

Multi-channel I/O multiplexing model is the ability to monitor I/O events of multiple streams at the same time by using select, poll and epoll. When idle, the current thread will be blocked. When one or more streams have I/O events, it will wake up from the blocking state, so the program will poll all streams (epoll is only those streams that actually issue events), and only process the ready streams in sequence. This approach avoids a lot of useless operations. Here "multiplexing" refers to multiple network connections, and "multiplexing" refers to multiplexing the same thread. The use of multi-channel I/O multiplexing technology allows a single thread to efficiently process multiple connection requests (minimizing the time consumption of network IO), and Redis operates very fast in memory (the operation in memory will not become the performance bottleneck here), mainly the above two points make Redis has a high throughput.

Unlike Memcached, Redis does not use Libevent directly, but completes a very lightweight implementation of the common interfaces select, epoll, evport, kqueue. Select the appropriate interface for different system calls. Under linux, the default is epoll. Because Libevent is heavier and more versatile, the code volume is also very large, and it has many functions that Redis does not use. In order to pursue "lightness" and remove dependence, Redis chooses to package a set of itself.

Benefits of Single Process Single Thread

Clearer code, simpler processing logic

There are no locks to worry about, no lock add and lock release operations, and no performance cost due to possible deadlocks

There are no multi-process or multi-threaded switches that consume CPU

Single Process Single Thread Disadvantages

It is not possible to use multi-core CPU performance, but it can be improved by opening multiple Redis instances on a single machine;

Some of the other great open source software models

Multi-process single-threaded model: Nginx

Single Process Multithreaded Model: Memcached

Tags: Threads processes data models reuse memory multiple multiplexing events performance processing consumption differences code technology interfaces databases networks blocking excellent Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn Xiaomi MySQL Microsoft