Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report