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

The reason for the High Speed of redis single Thread

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Editor to share with you the reasons for the high speed of redis single thread. I hope you will gain a lot after reading this article. Let's discuss it together.

The speed of Redis execution mainly depends on the following reasons:

(1) Pure memory operation, avoiding a large number of access to the database, reducing the direct reading of disk data, redis stores the data in memory, reading and writing data will not be limited by the speed of the hard disk, so the speed is fast.

(2) single-thread operation avoids unnecessary context switching and competition conditions, and there is no CPU consumption caused by multi-process or multi-thread switching, there is no need to consider all kinds of locks, there is no lock release lock operation, and there is no performance consumption caused by possible deadlocks.

(3) the non-blocking Iripple O multiplexing mechanism is adopted.

The principle of multiplexing:

The user first adds the socket that requires an IO operation to the select, and then blocks and waits for the select system call to return. When the data arrives, socket is activated and the select function returns. The user thread formally initiates the read request, reads the data and continues execution. In this way, the user can register multiple socket, and then constantly call select to read the activated socket,redis server to put these socke in the queue, and then the file event dispatcher takes it from the queue in turn and forwards it to different event handlers to improve the reading efficiency.

The use of multi-channel Icano multiplexing technology allows a single thread to process multiple connection requests efficiently (minimizing the time consumption of network IO). The multi-channel Icano multiplexing model makes use of the ability of select, poll, and epoll to monitor the Icano events of multiple streams at the same time, blocking the current thread when idle, and waking up from the blocked state when one or more streams have Icano events. The program then polls all streams (epoll polls only those streams that actually emit events) and processes only those that are ready in sequence, which avoids a large number of useless operations and improves efficiency.

(4) flexible and diverse data structures.

Redis uses a redisObject object internally to represent all key and value. The main information of redisObject includes data type, encoding mode, data pointer, virtual memory and so on. It contains five data types of String,Hash,List,Set,Sorted Set and uses the corresponding data types for different scenarios to reduce memory usage and save network traffic transmission.

(5) persistence

Since the data of redis is stored in memory, if persistence is not configured, all data will be lost after redis restart, so you need to enable the persistence feature of redis to save the data to disk. When redis restarts, you can recover the data from disk. Redis provides two ways for persistence, one is RDB persistence (the principle is to dump the database records of redis in memory regularly to RDB persistence on disk), and the other is AOF (append only file) persistence (the principle is to write the operation log of redis to a file in an appended way). Persistence does not seem to be directly related to the speed of redis, but it ensures the security and reliability of redis data and also plays a role in data backup.

(6) Summary

Imagine whether a single thread cannot perform multi-core CPU performance, but it is not. We can improve it by opening multiple redis instances on a single machine. Only one CPU core can be used in a single thread, so multiple instances can be started in the same multi-core server to form master-master or master-slave. Time-consuming read commands can be carried out entirely in slave, giving full play to the role of redis.

Single thread means that the network request module uses one thread (so there is no need to consider concurrent security), and other modules also use multiple threads. In the process of using redis, give full play to its advantages to avoid some improper operations, resulting in performance degradation.

After reading this article, I believe you have a certain understanding of the reason why redis single-threaded speed is fast. If you want to know more about it, welcome to follow the industry information channel. Thank you for reading!

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

Database

Wechat

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

12
Report