redis之pipeline

redis之pipeline

When we have to send multiple commands, we can pack them together in one request and save connection overhead by using pipelines, it is essentially a network optimization. As long as the operations are mutually independent, we can take advantage of this technique:

Redis 是基于client-server的请求响应模型,一条命令的执行:

  • 发送命令
  • 命令排队
  • 命令执行
  • 接收返回结果

RTT - round trip time, 如果发送N次命令,等待上一条命令应答后再执行,这中间不仅仅多了RTT(Round Time Trip),而且还频繁的调用系统IO,发送网络请求。

Screen Shot 2020-09-21 at 10.30.51 PM

pipeline只要是提升执行效率,减少网络IO。

Screen Shot 2020-09-21 at 10.51.25 PM

jedis pipeline support

public void mdel(List<String> keys){
Jedis jedis = new Jedis("127.0.0.1");
// 创建Pipeline对象
Pipeline pipeline = jedis.pipelined();
for (String key : keys){
// 组装命令
pipeline.del(key);
}
// 执行命令
pipeline.sync();
}

notes: redis 提供批量操作命令 mge,mset,同样实现缩减rtt的目的。


https://redislabs.com/ebook/part-2-core-concepts/chapter-4-keeping-data-safe-and-ensuring-performance/4-5-non-transactional-pipelines/

https://redis.io/topics/pipelining