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.
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!