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:

  • The client sends a query request to the server and listens for the Socket return, usually in blocking mode, waiting for the server's response.
  • The server processes the command and returns the result to the client.

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 view the Redis pipeline, just start the Redis instance and enter the following command:

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

+PONG
+OK
redis
:1
:2
:3

In the above example, we used thePINGcommand to check whether the Redis service is available. After that, we set the value of examplekey to redis, then we got the value of examplekey and incremented visitor 3 times.

From the returned results, we can see that these commands are submitted to the Redis service at once, and finally all server responses are read at once.


Advantages of Pipelining

The most significant advantage of pipelining is that it improves the performance of the Redis service.

Some Test Data

In the following test, we will use Redis's Ruby client, which supports pipelining features, to test the speed improvement effect of 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 executing the above simple script on a Mac OS X system on the LAN shows that after enabling pipelining, the round-trip delay has been reduced 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 has improved by 5 times.

Other Extensions