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

Is redis single-threaded or multithreaded

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Editor to share with you whether redis is single-threaded or multithreaded, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

Redis4.0 used to run in a single thread; multithreading is supported after Redis4.0. The reasons why Redis4.0 used single thread before: 1, single thread mode is convenient for development and debugging; 2, Redis uses epoll-based multiplexing internally; 3, the main performance bottleneck of Redis is memory or network bandwidth.

Different versions of Redis are different. Before Redis4.0, Redis was run with a single thread, but a single thread does not represent low efficiency. For example, Nginx and Nodejs are also single-threaded programs, but their efficiency is not low.

The reason is that Redis is memory-based, and its bottleneck lies in the machine's memory and network bandwidth, not CPU. The machine memory may be full or the bandwidth may reach the bottleneck before CPU reaches the bottleneck. Therefore, CPU is not the main reason, so it is natural to use single-threading, and it is more troublesome to use multithreading.

However, in Redis4.0, multithreading is already supported, such as background deletion and other functions.

To put it simply, Redis used single-threaded mode before 4. 0 for three reasons:

Redis using single-threaded mode is easier to develop and maintain because single-threaded mode is convenient for development and debugging.

Multi-client requests can be processed concurrently even using a single-threaded model, mainly because epoll-based multiplexing is used internally in Redis.

For Redis, the main performance bottleneck is memory or network bandwidth, not CPU.

However, Redis introduces lazy deletion (also known as asynchronous deletion) in version 4.0 and later, which means that we can delete data in Redis asynchronously, for example:

Unlink key: similar to del key, delete the specified key. If key does not exist, key is skipped. But del produces blocking, and the unlink command reclaims memory in another thread, that is, it is non-blocking [http://www.redis.cn/commands/unlink.html]

Flushdb async: delete all data in the current database [http://www.redis.cn/commands/flushdb.html]

Flushall async: delete data from all libraries [http://www.redis.cn/commands/flushall.html].

The advantage of this processing is that it does not stutter the main thread of the Redis and leaves these operations to the background thread to perform.

Usually, using the del instruction can delete data quickly, but when the deleted key is a very large object, for example, when the deleted hash collection contains thousands of elements, then the del instruction will cause the Redis main thread to stutter, so using lazy deletion can effectively avoid the Redis stutter problem. ]

Test site analysis:

The question about Redis threading model (single-threaded or multithreaded) is almost one of the questions that must be asked by Redis, but not many people have answered it well. Most of them can only answer that Redis is single-threaded and talk about the many benefits of single-threading, but very few people can accurately answer the characteristics of multithreading in Redis4.0 and Redis6.0, especially in Redis6.0. For knowledge about single-thread and multi-thread, there are also the following interview questions.

Since the 1.Redis main thread is single-threaded, why is it still so fast?

two。 Introduce IO multiplexing in Redis?

3. Introduce multithreading in Redis6.0?

Why is 1.Redis so fast?

The reasons are as follows:

a. Memory-based operations: all data in Redis is stored in memory, so all operations are memory-level, so its performance is relatively high.

b. Data structure is simple: the data structure of Redis is relatively simple and is specially designed for Redis. The time complexity of searching and operation of these simple data structures is O (1).

c. Multiplexed and non-blocking IO:Redis uses IO multiplexing to monitor clients with multiple socket connections, so that a single thread can be used to handle multiple situations, thus reducing the overhead of thread switching and avoiding IO blocking operations, thus greatly improving the performance of Redis.

d. Avoid context switching: because it is a single-threaded model, unnecessary context switching and multithreaded contention are avoided, which saves the time and performance overhead of multithreaded switching, and single-threading does not cause deadlocks.

The benchmark results used officially show that single-threaded Redis can achieve the throughput of 10W/S.

What is 2.IO multiplexing?

The read-write method of the socket is blocked by default. For example, when the read operation read method is called, the buffer does not have any data, then the thread will be stuck here, and the read method will not return until there is data in the buffer or the connection is closed, and the thread can continue to process other business.

But this obviously reduces the execution efficiency of the program, and Redis uses non-blocking IO, which means that the read and write process of IO is no longer blocked, and the read and write methods are completed instantly and returned, that is, it will use the strategy of reading as much as it can and writing as much as it can to perform IO operations, which is obviously more in line with our pursuit of performance.

But this non-blocking IO also faces a problem, that is, when we perform a read operation, we may only read part of the data; the same is true for writing data. When the buffer is full and our data is not finished, then when the valid data will be written becomes a problem.

And IO multiplexing is to solve the above problem, the easiest way to use IO multiplexing is to use the select function, which is the API interface provided by the operating system to the user program, which is used to monitor the readability and writability of multiple file descriptors, so that the read and write events of the file descriptor can be monitored. When the corresponding time is monitored, the thread can be notified to handle the corresponding business, which ensures the normal execution of the Redis read and write function.

[however, the current operating system basically does not apply the select function, calling the epoll function (Linux) instead, while macOS uses Kqueue (inheritance and Unix), because the select function performs very poorly when there are many file descriptors. ]

Multithreading in 3.Redis6.0?

The advantage of Redis single thread is that it not only reduces the responsibility of the internal implementation of Redis, but also allows all operations to be performed without locks, and there is no performance and time consumption caused by deadlocks and thread switching. But its disadvantage is also obvious, the single-threaded mechanism makes it difficult to effectively improve the QPS (Query Per Second, queries per second) of Redis (although fast enough, but people still have to pursue higher after all).

Although multithreading is introduced in version 4.0 of Redis, this version of multithreading can only be used for asynchronous deletion of large amounts of data, which is not of great significance for non-delete operations.

If we use Redis multithreading, we can share the pressure of Redis synchronous reading and writing IO, and make full use of multi-core CPU resources, and can effectively improve the QPS of Redis. Although IO multiplexing is used in Redis and operates based on non-blocking IO, the read and write of IO itself is blocked. For example, when there is data in the socket, Redis will first copy the data from the kernel state space to the user state space, and then perform related operations, and the copy process is blocked, and the larger the amount of data, the more time it takes to copy, and these operations are all based on a single thread.

Therefore, multithreading is added to Redis6.0 to improve the read and write performance of IO. Its main implementation idea is to split the IO read and write tasks of the main thread into a group of independent threads to execute, so that multiple socket read and write parallelization can be used, but the Redis commands are still executed serially by the main thread.

Note, however: Redis6.0 is disabled by default, but can be enabled by the fact that io-threads-do-reads in the configuration file redis.conf equals true. But it is not enough, in addition, we also need to set the number of threads to correctly start the multi-threading function, also modify the configuration of Redis, such as setting io-threads 4, means to start 4 threads.

With regard to the setting of the number of threads, the official recommendation is to set the number of threads to 2 or 3 for 4-core CPU and 6 for 8-core CPU. In short, the number of threads must be less than the number of CPU cores of the machine. The larger the number of threads, the better. ]

With regard to the performance of Redis, the authors of Redis mentioned at the RedisConf conference in 2019 that the multithreaded IO feature introduced by Redis6.0 has at least doubled the performance improvement. Chinese people also use the 4-thread Redis version and the single-threaded Redis for comparative testing in Aliyun. It is found that the test results are consistent with what the Redis author said, and the performance can basically be doubled.

Summary:

This paper introduces the reasons why Redis is still fast in single thread before 4. 0: based on memory operation, simple data structure, IO multiplexing and non-blocking IO, avoiding unnecessary thread context switching. And in Redis4.0 began to support multithreading, mainly reflected in big data's asynchronous deletion, such as: unlink key, flushdb async, flushall async and so on. The multithreading of Redis6.0 increases the concurrency of reading and writing to IO, which is used to improve the performance of Redis.

These are all the contents of the article "whether redis is single-threaded or multithreaded". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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