【问题标题】:redis expire: What's the purpose of fast expire cycleredis expire:快速过期周期的目的是什么
【发布时间】:2021-03-27 08:11:56
【问题描述】:

我正在阅读redis 3.2的源代码。 redis服务器主动让key过期,在事件循环中调用activeExpireCycle函数。

这个函数有一个参数type来指定时间限制。类型可以是ACTIVE_EXPIRE_CYCLE_FAST(快循环)或ACTIVE_EXPIRE_CYCLE_SLOW(慢循环)。快速循环的时间限制被硬编码为 1 毫秒 (ACTIVE_EXPIRE_CYCLE_FAST_DURATION)。慢循环的时间限制为 serverCron 周期的 25%,默认为 25 毫秒。所以第一个周期比慢周期花费的时间要短得多。

另外,快循环在beforSleep函数中调用,慢循环在serverCron函数中调用。我在事件循环中绘制了关于这两个调用的图表。

我无法弄清楚快速循环的目的。为什么 redis 不让所有活跃的过期工作慢循环?

更新:我更改了上图。并非在每个循环中都调用函数 serverCron。 Redis 默认每 100 毫秒执行一次 serverCron()。

【问题讨论】:

  • 我个人的理论是,快周期是为了利用空闲时间,所以慢周期可能会有更少的工作要做
  • @Itamar Haber 感谢您的评论。我同意你。快速循环仅在上一个循环发生超时时执行,这意味着当时可能有很多密钥要过期。所以快周期可以减轻慢周期。

标签: redis


【解决方案1】:

我在redis源代码中找到了注释。希望对您有所帮助(它会更加积极地避免可以从键空间中删除的键使用过多的内存):

// in https://github.com/redis/redis/blob/6.0/src/expire.c#L76
/* Try to expire a few timed out keys. The algorithm used is adaptive and
 * will use few CPU cycles if there are few expiring keys, otherwise
 * it will get more aggressive to avoid that too much memory is used by
 * keys that can be removed from the keyspace.
 *
 * The function can perform more or less work, depending on the "type"
 * argument. It can execute a "fast cycle" or a "slow cycle". The slow
 * cycle is the main way we collect expired cycles: this happens with
 * the "server.hz" frequency (usually 10 hertz).
 *
 * However the slow cycle can exit for timeout, since it used too much time.
 * For this reason the function is also invoked to perform a fast cycle
 * at every event loop cycle, in the beforeSleep() function. The fast cycle
 * will try to perform less work, but will do it much more often.
 */

【讨论】:

    猜你喜欢
    • 2017-09-24
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多