Home  >  Article  >  Database  >  How to understand redis single thread

How to understand redis single thread

(*-*)浩
(*-*)浩Original
2019-11-28 09:13:081956browse

How to understand redis single thread

Redis communicates through sockets. The socket server can accept multiple client connection requests at the same time. In other words, the redis service faces multiple redis client connections at the same time. request, and the redis service itself runs in a single thread. (Recommended Learning: Redis Video Tutorial )

## This assumes that now there are five clients of A, B, C, D, and E to initiate Redis requests at the same time. The first one arrives, then B, C, D, and E arrive in sequence. At this time, the redis server starts processing the A request. It takes 30 seconds to establish the connection, 10 seconds to obtain the request data, and then 0.1 seconds to process the data and send the data back to It takes 5 seconds on the client side and about 45 seconds in total.

In other words, the next B request needs to wait 45 seconds. Note here that these five requests may be almost at the same time. Since the socket can handle multiple requests at the same time, the time difference in the network connection phase can be ignored, but in In the second stage, the server needs to do nothing and sit and wait for 10 seconds, which is intolerable for the CPU and the client.

So the efficiency of single thread is very, very low, but precisely because of these similar problems, Redis single thread does not run like this in nature. Next, we will discuss the real single-threaded operation mode of redis.


The connection between the client and the server is established by socket. Multiple connections can be established at the same time. (This should be multi-thread/multi-process), The established connection is redis You know (why you know, go to socket programming, again emphasizing the basics is very important), Then redis will detect which connection has received the client's request data based on these established connections.

Note: It is not to detect which connection has been established, but to detect which one has received the request data, and the detection action here is the start of a single thread. Once detected, it is based on the received The data starts the data processing phase, then returns the data, and then continues to detect the next network connection that has received the requested data.

Note that the entire process from detection to data processing to data return is single-threaded.

This should be the so-called redis single thread. We don’t need to care about how complicated the internals are. What we pursue is to understand the process and demand the principles, but we cannot dig out all the internal organs.

From detecting the network connection after accepting the request data to the final data return, the server only takes 5.1 seconds. This time is the data after I enlarged it N times. The actual time is much shorter than this, maybe It is one ten thousandth of N time of 5.1.

Why do you say that? Because the data is processed in local memory, you can imagine how fast it is. Although the final returned data involves the network, the network connection has been established, and the speed is also very, very fast. , just a little slower than the data processing stage. Therefore, there is actually no need to worry about the efficiency of the single-threaded method.

For more Redis-related technical articles, please visit the

Redis Getting Started Tutorial column to learn!

The above is the detailed content of How to understand redis single thread. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn