redis dbsize 命令的准确性 [英] Accuracy of redis dbsize command

查看:56
本文介绍了redis dbsize 命令的准确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

redis 中的 dbsize 命令有多准确?

How accurate is the dbsize command in redis?

我注意到 dbsize 返回的键数与 keys 命令返回的实际键数不匹配.

I've noticed that the count of keys returned by dbsize does not match the number of actual keys returned by the keys command.

这是一个例子:

redis-cli dbsize
(integer) 3057
redis-cli keys "*" | wc -l
2072

为什么 dbsize 比实际的键数高这么多?

Why is dbsize so much higher than the actual number of keys?

推荐答案

我认为这与密钥过期有关.

I would say it is linked to key expiration.

像 Redis 或 memcached 这样的键/值存储无法为每个对象定义一个物理计时器以使其过期.他们会太多.相反,他们定义了一个数据结构来轻松跟踪要过期的项目,并将所有过期事件多路复用到单个物理计时器.他们还倾向于实施一种懒惰的策略来处理这些事件.

Key/value stores like Redis or memcached cannot afford to define a physical timer per object to expire. There would be too many of them. Instead they define a data structure to easily track items to be expired, and multiplex all the expiration events to a single physical timer. They also tend to implement a lazy strategy to deal with these events.

使用 Redis,当项目过期时,什么也不会发生.但是,在每个项目访问之前,都会系统地进行检查以避免返回过期项目,并可能删除该项目.在这种惰性策略之上,每 100 毫秒,就会触发一个清道夫算法,以物理使许多项目过期(即从主字典中删除它们).每次迭代时考虑的键数取决于过期工作量(算法是自适应的).

With Redis, when an item expires, nothing happens. However, before each item access, a check is systematically done to avoid returning expired items, and potentially delete the item. On top of this lazy strategy, every 100 ms, a scavenger algorithm is triggered to physically expire a number of items (i.e. remove them from the main dictionary). The number of considered keys at each iteration depends on the expiration workload (the algorithm is adaptative).

结果是,当您有稳定的到期事件流时,Redis 可能会在给定的时间点积压要到期的项目.

The consequence is Redis may have a backlog of items to expire at a given point in time, when you have a steady flow of expiration events.

现在回到问题,DBSIZE 命令只返回主字典的大小,因此它包括尚未删除的过期项目.KEYS 命令遍历整个字典,访问单个键,因此它排除所有过期的项目.因此,项目数量可能不匹配.

Now coming back to the question, the DBSIZE command just return the size of the main dictionary, so it includes expired items that have not yet been removed. The KEYS command walks through the whole dictionary, accessing individual keys, so it excludes all expired items. The number of items may therefore not match.

这篇关于redis dbsize 命令的准确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆