1. Request response protocol and RTT:
Redis is a typical TCP server based on the C/S model. In the communication process between the client and the server, the client usually initiates the request first, and the server performs the corresponding tasks after receiving the request, and finally sends the obtained data or processing results to the client in the form of a response. During this process, the client will wait in a blocking manner for the results returned by the server. See the following command sequence:
Client: INCR X Server: 1 Client: INCR X Server: 2 Client: INCR X Server: 3 Client: INCR X Server: 4
In the process of each pair of request and response, we have to bear the additional overhead caused by network transmission. We usually call this overhead RTT (Round Trip Time). Now we assume that the RTT of each request and response is 250 milliseconds, and our server can process 100k data in one second, but the result is that our server can process at most 4 requests per second. To solve this performance problem, how can we optimize it?
2. Pipelining:
Redis has provided support for command pipelines in very early versions. Before giving a specific explanation, we first transform the above example of the synchronous response method into an asynchronous response method based on the command pipeline, so that everyone can have a better perceptual understanding.
Client: INCR X Client: INCR X Client: INCR X Client: INCR X Server: 1 Server: 2 Server: 3 Server: 4
As can be seen from the above example, after sending the command, the client does not need to wait for a response from the server immediately, but can continue to send subsequent commands. After the command is sent, the responses to all previous commands are read at once. This saves the RTT overhead in synchronous mode.
The last thing to note is that if the Redis server finds that the client's request is pipeline-based, then after receiving the request and processing it, the server will store the response data of each command in the queue and then send it to the client. .
3. Benchmark:
The following are test cases and test results from the Redis official website. It should be noted that this test is based on loopback (127.0.0.1), so the time taken by RTT is relatively small. If it is based on the actual network interface, then the performance improvement brought by the pipeline mechanism will be even more significant.
require 'rubygems' require 'redis' def bench(descr) start = Time.now yield puts "#{descr} #{Time.now-start} seconds" end def without_pipelining r = Redis.new 10000.times { r.ping } end def with_pipelining r = Redis.new r.pipelined { 10000.times { r.ping } } end bench("without pipelining") { without_pipelining } bench("with pipelining") { with_pipelining } //without pipelining 1.185238 seconds //with pipelining 0.250783 seconds
The above is the content of Redis Tutorial (13): Detailed explanation of pipeline. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!