Redis 快速填满内存,运行 --bigkeys 释放内存

Redis filling up memory fast, running --bigkeys free it up

一个月前,Redis 突然开始快速填满服务器内存。为了调试问题,我们有 运行 redis-cli --bigkeys 并且令我们惊讶的是所有使用的内存都被释放了。

我们有一个6节点的集群,3主3从,每个主数据库大约15GB。每个节点都存储在一个专用的盒子中,每个盒子有 64GB。 Redis 每天两次填满整个 64GB 内存。我们有一个 cron 运行ning redis-cli --bigkeys 每天两次以释放已用内存。

可能是什么原因?

谢谢。

听起来你遇到了 OOM command not allowed 错误,除非你每天 运行 redis-cli --bigkeys 两次。

如果是这种情况,您可能有许多 and/or 个带有 EXPIRE 的大键,并不断添加。过期密钥从内存中删除:

  • 被动:当你试图访问它,发现密钥超时。 这就是 redis-cli --bigkeys 对您的帮助,它强制对所有密钥进行被动移除space。
  • 主动:每 100 毫秒,它会尝试从内存中删除过期的键 at random, never investing more than 1 ms per cycle at it, until it estimates that less than 25% of expired keys remain. The logic is not that trivial, see activeExpireCycle

所以这一切都表明活动过期无法赶上你的情况。

根据您的评论,maxmemory=0maxmemory-policy=noeviction。您可能需要考虑设置一个 maxmemory 值,并将 maxmemory-policy=noeviction 设置为 volatile-ttl (删除具有最近过期时间的密钥)。

这是做什么的,每当写入命令发现您已结束 [​​=16=],它会根据策略尝试释放 space 用于新密钥。 volatile-ttl 策略将首先驱逐所有剩余的过期密钥。 See evict.c.

您还可以增加后台任务的频率,以更频繁地清除过期密钥,请参阅 redis.conf 中的 hz。你可以加倍到 20。

By default "hz" is set to 10. Raising the value will use more CPU when Redis is idle, but at the same time will make Redis more responsive when there are many keys expiring at the same time, and timeouts may be handled with more precision.

此外,activedefrag = yes 可能会有所帮助,请参阅 here

有一个新的 active-expire-effort redis.conf 设置可以让你在 active-expire 上投入更多 CPU,但它在最新的稳定版本 (5.0.0.0) 中不可用。 7).

使用 INFO memory 了解您的 Redis 服务器内存状态。如果以上内容对您没有帮助,请使用此输出更新问题。