Home > Backend Development > C#.Net Tutorial > Redis tutorial (13): Detailed explanation of pipeline

Redis tutorial (13): Detailed explanation of pipeline

黄舟
Release: 2016-12-28 15:11:19
Original
1295 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template