Redis是一个响应式的服务,当客户端发送一个请求后,就处于阻塞状态等待Redis返回结果。这样一次命令消耗的时间就包括三个部分:请求从客户端到服务器的时间、结果从服务器到客户端的时间和命令真正执行时间,前两个部分消耗的时间总和称为RTT(Round Trip Time),当客户端与服务器存在网络延时时,RTT就可能会很大,这样就会导致性能问题。管道(Pipeline)就是为了改善这个情况的,利用管道,客户端可以一次性发送多个请求而不用等待服务器的响应,待所有命令都发送完后再一次性读取服务的响应,这样可以极大的降低RTT时间从而提升性能。
下面这个例子,在本地分别以普通请求和管道对一个键调用2000次incr命令的测试。
public class App
{
public static void main( String[] args ) {
long start = System.currentTimeMillis();
withoutPipeline();
System.out.println("Without Pipeline takes: " + (System.currentTimeMillis() - start) + " ms.");
start = System.currentTimeMillis();
withPipeline();
System.out.println("With Pipeline takes: " + (System.currentTimeMillis() - start) + " ms.");
}
public static void withPipeline() {
Jedis jedis = null;
try {
jedis = new Jedis("localhost", 6379);
jedis.flushDB();
Pipeline p = jedis.pipelined();
p.set("thekey", Integer.toString(0));
for (int i = 1; i <= 2000; i++) {
p.incr("thekey");
}
Response<String> r = p.get("thekey");
p.sync();
System.out.println(r.get());
} finally {
jedis.close();
}
}
public static void withoutPipeline() {
Jedis jedis = null;
try {
jedis = new Jedis("localhost", 6379);
jedis.flushDB();
jedis.set("thekey", Integer.toString(0));
for (int i = 1; i <= 2000; i++) {
jedis.incr("thekey");
}
System.out.println(jedis.get("thekey"));
} finally {
jedis.close();
}
}
}
//输出结果
2000
Without Pipeline takes: 183 ms.
2000
With Pipeline takes: 47 ms.
结果很直观的反映出两者的差别,要知道这是在本地测试,几乎不存在网络延时的问题,如果是在不同的网段测试的话,效果会更明显。虽然管道在一定程度上对性能有所提升,但是在使用时一点要注意,每个命令的返回结果是先被缓存在服务器端的,最后一次性返回给客户端。如果一次批量提交涉及大量的返回结果,可能会导至服务器的内存溢出,这在生产环境是致命的,一次批次处理多少量,最好在设计阶段做出合理评估。
最后,管道只是一个方案,并不意味着在任何时候都要尽可能的使用它,而是应该结合考虑网络延迟时间、业务涉及的请求量等等因素综合考虑,毕竟创建管道本身也会有一定的消耗。