【问题标题】:How redis pipe-lining works in pyredis?redis 管道衬里如何在 pyredis 中工作?
【发布时间】:2016-01-21 04:59:48
【问题描述】:

我想了解,redis 中的管道衬里如何工作?根据我阅读的一篇博客,对于此代码

Pipeline pipeline = jedis.pipelined();
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
    pipeline.set("" + i, "" + i);
}
List<Object> results = pipeline.execute();

Every call to pipeline.set() effectively sends the SET command to Redis (you can easily see this by setting a breakpoint inside the loop and querying Redis with redis-cli). The call to pipeline.execute() is when the reading of all the pending responses happens.

所以基本上,当我们使用流水线时,当我们执行上面设置的任何命令时,该命令会在服务器上执行,但在我们执行 pipeline.execute() 之前我们不会收集响应。

但是,根据 pyredis 的文档, Pipelines are a subclass of the base Redis class that provide support for buffering multiple commands to the server in a single request.

我认为,这意味着,我们使用流水线,当我们执行 pipe.execute() 时,所有命令都被缓冲并发送到服务器,因此这种行为与上述行为不同。

谁能告诉我使用 pyreids 时的正确行为是什么?

【问题讨论】:

    标签: redis


    【解决方案1】:

    这不仅仅是 redis-py 的事情。在 Redis 中,流水线 always 意味着缓冲一组命令,然后一次性将它们发送到服务器。流水线的主要目的是避免无关的网络来回往返——在对 Redis 运行命令时,这通常是瓶颈。如果在管道运行之前将每个命令都发送到 Redis,则情况并非如此。

    您可以在实践中对此进行测试。打开 python 并:

    import redis
    r = redis.Redis()
    p = r.pipeline()
    p.set('blah', 'foo') # this buffers the command. it is not yet run.
    r.get('blah') # pipeline hasn't been run, so this returns nothing.
    p.execute()
    r.get('blah') # now that we've run the pipeline, this returns "foo".
    

    【讨论】:

      【解决方案2】:

      我确实运行了您在博客中描述的测试,但无法重现该行为。 在for循环中设置断点并运行

      redis-cli info | grep keys
      

      在每次设置命令后不显示大小增加。

      说到这里,你粘贴的代码好像是Java using Jedis(我也用过)。 在我运行的测试中,根据文档,jedis 中没有方法 execute() 而是 exec()sync()强> 一个。

      我确实看到了在 sync() 命令之后在 redis 中设置的值。

      此外,this question 似乎与 pyredis 文档一致。

      最后,redis documentation 本身专注于网络优化(引用示例)

      这次我们不是为每次调用支付 RTT 的费用,而是为三个命令支付一次。

      附:你能得到你阅读的博客的链接吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-20
        • 1970-01-01
        • 2012-09-14
        • 2015-02-08
        • 2020-05-08
        • 2010-11-07
        • 1970-01-01
        • 2018-08-05
        相关资源
        最近更新 更多