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 introduce multithreading?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains why Redis introduces multithreading. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn why Redis introduces multithreading.

I. Overview of the problem

The version after Redis 6.0abandoned the design of single-threaded model, and Redis, which originally used single-threaded operation, also began to selectively use multithreaded model. At first glance, the author of Redis is so good that he can not escape the "Law of True incense".

If you think about it, this question can actually be split into two main questions:

(1) Why did Redis choose the single-threaded model in the first place (the benefits of single-threading)?

(2) Why did Redis add multithreading after 6. 0 (in some cases, single threading has shortcomings that can be solved by multithreading)?

In fact, it is not that the author did not escape the true incense theorem, but with the passage of time, there are more and more problems, the original design must be some outdated, should make a change. OK, with two questions, let's take a good look at it.

Why did Redis use single thread in the first place

Whether it is single-threaded or multithreaded, it is to improve the efficiency of Redis development, because Redis is a memory-based database, but also to deal with a large number of external network requests, which inevitably requires multiple IO. Fortunately, Redis uses many excellent mechanisms to ensure its high efficiency. So why is Redis designed to be single-threaded? It can be summarized as follows:

(1) IO Multiplexing

Let's take a look at the Redis top-level design.

FD is a file descriptor that indicates whether the current file is readable, writable, or abnormal. The Icano multiplexing mechanism is used to monitor the readable and writable states of multiple file descriptors at the same time. You can understand it as having the characteristics of multithreading.

Once a network request is received, it will be processed quickly in memory, and since the vast majority of operations are pure memory, the processing speed will be very fast. In other words, in single-threaded mode, even if there are a lot of connected network processing, because of IO multiplexing, it can still be ignored in high-speed memory processing.

(2) High maintainability

Although the multithreading model performs well in some aspects, it introduces the uncertainty of program execution order and brings a series of problems of concurrent reading and writing. In single-threaded mode, it is easy to debug and test.

(3) based on memory, the efficiency of single thread is still high.

Multithreading can make full use of the resources of CPU, but for Redis, because of its high memory speed, it can handle 100000 user requests in a second. If 100, 000 per second is not enough, then we can use Redis sharding technology to give it to different Redis servers. Such cooking avoids the introduction of a large number of multithreaded operations in the same Redis service.

And based on memory, basically no AOF O operations are involved unless you are making a backup of it. Since the reading and writing of this data occurs only in memory, the processing speed is very fast; using a multithreaded model to handle all external requests may not be a good solution.

Now we know that basically can be summarized into two sentences, based on memory and the use of multiplexing technology, single-threaded speed is very fast, but also ensures the characteristics of multithreading. Because there is no need to use multithreading.

Third, why introduce multithreading?

I just talked about a lot of benefits of using single threading, but now I want to talk about why multithreading should be introduced, and don't be uncomfortable. The introduction of multithreading shows that in some aspects of Redis, single threading no longer has an advantage.

Because the read/write system call of the read-write network takes up most of the CPU time during the execution of Redis, if the network read and write is multithreaded, the performance will be greatly improved.

The multithreaded part of Redis is only used to handle the reading and writing of network data and protocol parsing, and the execution of commands is still single-threaded. The reason for this design is that you don't want Redis to be complicated by multithreading, and you need to control the concurrency of key, lua, transactions, LPUSH/LPOP, and so on.

Redis has added some delete operations that can be handled asynchronously by other threads in the latest versions, that is, UNLINK, FLUSHALL ASYNC, and FLUSHDB ASYNC we mentioned above. Why do we need these deletions, and why do they need to be handled asynchronously in a multi-threaded way?

We know that Redis can use the del command to delete an element. If the element is very large and may occupy tens of megabytes or hundreds of megabytes, it cannot be done in a short period of time, which requires multi-threaded asynchronous support.

Now the deletion can be done in the background.

At this point, I believe you have a deeper understanding of "why Redis introduces multithreading". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report