Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Redis Pipelining

Redis Pipelining

Redis is a TCP service based on the client-server model and the request/response protocol. This means that normally a request follows these steps:


Redis Pipelining

Redis pipelining allows the client to continue sending requests to the server while the server has not yet responded, and finally read all server responses at once.

Example

To see the redis pipeline, simply start the redis instance and enter the following command:



$(echo -en "PING\r\n SET runoobkey redis\r\nGET runoobkey\r\nINCR visitor\r\nINCR visitor\r\nINCR visitor\r\n"; sleep 10) | nc localhost 6379



+PONG

+OK

redis

:1

:2

:3

In the example above, by using PING command to check if the redis service is available, then we set the value of runoobkey to redis, and then we get the value of runoobkey and increment visitor 3 times.

In the returned result, we can see that these commands are submitted to the redis service at once and all server responses are read at once


Advantages of Pipelining

The most significant advantage of pipelining is improved performance of the redis service.

Some test data

In the test below, we will use Redis's Ruby client, which supports pipelining, to test the speed improvement brought by pipelining.



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 

}

Data from running the simple script above on a Mac OS X system in a LAN shows that after enabling pipelining, the round-trip latency has been improved to quite a low level.



without pipelining 1.185238 seconds 

with pipelining 0.250783 seconds

As you can see, after enabling pipelining, our speed efficiency improved by 5 times.