当前位置:网站首页>Redis notes (15) - Pipeline (the client packages and sends batch commands to save network overhead)

Redis notes (15) - Pipeline (the client packages and sends batch commands to save network overhead)

2022-06-26 09:46:00 wohu1104

Redis It's a client based - Server model and request / In response to the protocol TCP service . This means that normally a request follows these steps :

  • The client sends a query request to the server , And monitor Socket return , Usually in blocking mode , Waiting for the server to respond .
  • The server handles commands , And return the result to the client .

One client Through a socket Connection initiates multiple request commands .

After each request order is issued client Usually it will block and wait redis Service handling ,redis After processing the request command, the result will be returned to client .

The client and server connect through the network . Such a connection can be very fast , Or very slow ( Two hosts that can only be interconnected through multiple nodes on the WAN ). But whether there is network delay or not , Data packets are transmitted from the client to the server , And it takes some time for the client to get the corresponding information from the server . This period of time becomes the round trip delay ( Round Trip Time).

So when the client needs to execute a series of requests , It's easy to see the impact on performance ( For example, adding a large number of elements to the same queue , Or insert a large number of keys into the database ). If RTT The length of 250 millisecond ( In a WAN based low-speed connection environment ), Even if the server can handle 10 Million requests , But in fact, we can only process the most per second 4 A request .
If you are in a loop network ,RTT The duration is quite short ( My host ping 127.0.0.1 When it comes to 0.044ms), But if you execute a long list of write requests , It will be a little long .

1. The Conduit

A request / The corresponding services can be implemented as , Even if the client does not read the response of the old request , The server can still handle new requests . In this way , It can send multiple instructions to the server without waiting for the server to respond , And finally read all replies at once . The most significant advantage of pipeline technology is the improvement of redis Service performance .

adopt pipeline When there is a large number of operations . We can save a lot of time that we used to waste on network delay . It needs to be noted that pipeline Method pack command send ,redis The processing results of all commands must be cached before all commands are processed . The more orders to pack , The more memory the cache consumes . So it's not that the more orders you pack, the better . How much is appropriate depends on the situation .

If multiple instructions are executed consecutively , It will take multiple network packets back and forth . As shown in the figure below .
 Multiple commands
Adjust the reading and writing order , So that two consecutive write operations and two consecutive read operations will only take one network round trip in total , It's like continuous request The operation is combined , Successive response It's the same with operations .
 Merge request
This is the essence of pipeline operation , It's not a feature of the server , There's no difference at all between servers , Still got a message , Execute a message , The normal process of replying to a message . The client can save a lot by changing the reading and writing order of the instruction list in the pipeline IO Time , To improve performance .

原网站

版权声明
本文为[wohu1104]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170549210102.html