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

Redis is a single thread. Why is it so fast?

2025-04-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about why Redis is single-threaded and why it is so fast. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

I. introduction to Redis

Redis is an open source in-memory data structure storage system that can be used as database, cache, and messaging middleware.

It supports many types of data structures, such as string (String), hash (Hash), list (List), set (Set), ordered set (Sorted Set or ZSet) and range query, Bitmaps,Hyperloglogs and geospatial (Geospatial) index radius query. The common data structure types are String, List, Set, Hash and ZSet.

Redis has built-in replication (Replication), LUA script (Lua scripting), LRU driven event (LRU eviction), transaction (Transactions) and different levels of disk persistence (Persistence), and provides high availability (High Availability) through Redis Sentinel (Sentinel) and automatic partitioning (Cluster).

Redis also provides persistence options that allow users to save their data to disk for storage. Depending on the actual situation, the dataset can be exported to disk (snapshot) at regular intervals, or appended to the command log (AOF only appends files), and he will copy the write command executed to the hard disk when executing the write command. You can also turn off persistence and use Redis as an efficient network caching function.

Redis does not use tables, and his database does not predefine or force users to associate different data stored in Redis.

According to the storage mode, the working mode of database can be divided into hard disk database and in-memory database. Redis stores data in memory, and when reading and writing data, it will not be limited by the speed of the hard disk, so it is extremely fast.

(1) working mode of hard disk database:

(2) the working mode of in-memory database:

After reading the above description, whether you have any understanding of some common Redis-related interview questions, such as: what is Redis, what are the common data structure types of Redis, how Redis is persisted, and so on.

2. How fast is Redis?

Redis adopts memory-based KV database based on single-process and single-thread model, which is written by C language. The official data is QPS (number of queries per second) which can reach 1000000 +.

This data is no worse than the same memory-based KV database Memcached using single-process multithreading!

The horizontal axis is the number of connections and the vertical axis is QPS. At this time, this picture reflects an order of magnitude. I hope everyone can describe it correctly during the interview. When you don't ask you, the order of magnitude of your answer is very different!

Three. Why is Redis so fast?

1. Based entirely on memory, most of the requests are purely memory operations, which is very fast. The data is stored in memory, and the advantage similar to HashMap,HashMap is that the time complexity of search and operation is O (1).

2. The data structure is simple and the data operation is simple. The data structure in Redis is specially designed.

3. Single thread 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 operation, and there is no performance consumption caused by possible deadlocks.

4. Using the multi-channel Istroke O multiplexing model, non-blocking IO

5. Different underlying models are used, and the underlying implementation between them and the application protocols for communication with clients are different. Redis directly builds its own VM mechanism, because the general system calls system functions, it will waste a certain amount of time to move and request.

The above points are easy to understand. Let's make a simple discussion on the multi-channel Icano reuse model.

(1) Multi-channel Istroke O multiplexing model

The multiplexing model of multiple streams uses the ability of select, poll, and epoll to monitor multiple streams' Icano events 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, so the program polls all streams once (epoll polls only those streams that actually emit events) and processes only those that are ready in sequence. This practice avoids a large number of useless operations.

Here, "multiplex" refers to multiple network connections, and "multiplexing" refers to the reuse of the same thread.

Using multi-channel IO multiplexing technology allows a single thread to process multiple connection requests efficiently (minimizing the time consumption of network IO), and Redis operates data in memory very fast, that is to say, in-memory operations will not become a bottleneck affecting Redis performance, which mainly makes Redis have high throughput.

four。 So why is Redis single-threaded?

First of all, we have to understand that the above analysis is to create a very fast atmosphere of Redis! According to the official FAQ, because Redis is a memory-based operation, CPU is not the bottleneck of Redis, and the bottleneck of Redis is most likely to be the size of machine memory or network bandwidth. Since single-threading is easy to implement, and CPU won't be a bottleneck, it makes sense to adopt a single-threaded solution (after all, multithreading can be troublesome! ).

When you see this, you may cry angrily! I thought there would be any major technical points that made it possible for Redis to use a single thread so quickly, but it was an official answer that seemed to fool us! However, we can clearly explain why Redis is so fast, and because it is so fast in single-threaded mode, there is no need to use multithreading!

However, we cannot achieve multicore CPU performance by using single-threading, but we can improve it by opening multiple Redis instances on a single machine!

Warning 1: here we have been emphasizing the single thread, but there is only one thread to handle our network requests, a formal Redis Server runtime must be more than one thread, here we need to pay attention to! For example, when Redis is persisted, it will be executed as a child process or sub-thread (whether it is a child thread or a child process for readers to study in depth); for example, I look at the Redis process on the test server and find the thread under the process:

The "- T" parameter of the ps command indicates that the display thread (Show threads, possibly with SPID column.) the "SID" column represents the thread ID, while the "CMD" column shows the thread name.

Warning 2: the last paragraph in FAQ in the figure above describes how multithreading will be supported since Redis version 4.0, but only on some operations! So whether this article is still a single-threaded way in a later version needs to be verified by the reader!

five。 Pay attention

1. We know that Redis uses the "single thread-multiplexing IO model" to implement high-performance in-memory data services. This mechanism avoids the use of locks, but at the same time, this mechanism reduces the concurrency of redis when performing more time-consuming commands such as sunion.

Because it is a single thread, there is only one operation going on at a time, so time-consuming commands will lead to a decline in concurrency, not only read concurrency, but also write concurrency. A single thread can only use one CPU core, so multiple instances can be launched in the same multi-core server to form master-master or master-slave, and time-consuming read commands can be carried out entirely in slave.

Redis.conf items that need to be changed:

Pidfile / var/run/redis/redis_6377.pid # pidfile needs to add the port number

Port 6377 # this must be changed.

The name of logfile / var/log/redis/redis_6377.log # logfile is also accompanied by the port number

Dbfilename dump_6377.rdb # rdbfile also adds port number

2. "We can't let the operating system load balance, because we know our own programs better, so we can manually assign CPU cores to it without taking up too much CPU, or squeeze our key processes with a bunch of other processes."

CPU is an important influencing factor, and because of its single-threaded model, Redis prefers large cache and fast CPU to multi-core.

On multicore CPU servers, Redis performance also depends on NUMA configuration and processor binding location. The most obvious impact is that redis-benchmark randomly uses the CPU kernel. To get accurate results, you need to use a fixed processor tool (you can use taskset on Linux). The most effective way is to separate the client and server into two different CPU for colleges and universities to use three-tier cache.

six。 Expansion

Here are some models you should know. I wish you a hand in the interview.

1. Single-process multithreading model: MySQL, Memcached, Oracle (Windows version)

2. Multi-process model: Oracle (Linux version)

3. Nginx has two types of processes, one is called Master process (equivalent to management process), and the other is called Worker process (actual working process). There are two ways to start:

(1) single process startup: there is only one process in the system at this time, which acts as both the Master process and the Worker process.

(2) Multi-process startup: at this time, the system has only one Master process, and at least one Worker process is working.

(3) the Master process mainly initializes and manages the Worker; event handling is carried out in Worker.

Thank you for reading! This is the end of this article on why Redis is single-threaded so fast. I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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