Redis Pipeline Technology
Redis is a TCP service based on the client-server model and a request/response protocol. This means that typically a request follows these steps:
The client sends a query request to the server and listens for a return on the Socket, usually in blocking mode, waiting for the server's response.
The server processes the command and returns the result to the client.
Redis Pipeline Technology
Redis pipeline technology allows the client to continue sending requests to the server without waiting for a response, and eventually read all responses from the server at once.
Example
To view the Redis pipeline, simply start a Redis instance and enter the following command:
$(echo -en "PING\r\n SET tutorialprokey redis\r\nGET tutorialprokey\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 check if the Redis service is available using the PING command. Then, we set the value of tutorialprokey
to redis
, retrieve the value of tutorialprokey
, and increment the visitor
key three times.
In the returned results, we can see that these commands are submitted to the Redis service at once, and all responses from the server are read at once.
Advantages of Pipeline Technology
The most significant advantage of pipeline technology is the improvement in the performance of the Redis service.
Some Test Data
In the following tests, we will use Redis's Ruby client, which supports pipeline technology, to test the speed improvement effect of pipeline technology.
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 this simple script on a Mac OS X system within a local network shows that the round-trip delay is significantly reduced with pipelining enabled.
without pipelining 1.185238 seconds
with pipelining 0.250783 seconds
As you can see, enabling pipelining improves our speed efficiency by 5 times.