Redis: Show database size/size for keys

My redis instance seems to being growing very large and I'd like to find out which of the multiple databases I have in there consumes how much memory. Redis' INFO command just shows me the total size and the number of keys per database which doesn't give me much insight... So any tools/ideas that give me more information when monitoring the redis server would be appreciated.

The Redis documentation doesn't show me any commands that can return the consumed memory of certain keys, so I guess if any buggy code would write a lot of "trash" to redis this could be really hard to find...

166337 次浏览

也许您可以对数据库文件进行一些自省。该协议相对简单(但没有详细的文档说明) ,因此您可以为其编写一个解析器,以确定哪些单独的密钥占用了大量空间。


新建议:

你有没有试过使用 MONITOR来看看正在写什么,现场?也许你可以找到运动中数据的问题。

You might find it very useful to sample Redis keys and group them by type. Salvatore has written a tool called 重建采样器 that issues about 10000 RANDOMKEY commands followed by a TYPE on retrieved keys. In a matter of seconds, or minutes, you should get a fairly accurate view of the distribution of key types.

I've written an extension (unfortunately not anywhere open-source because it's work related), that adds a bit of introspection of key names via regexs that give you an idea of what kinds of application keys (according to whatever naming structure you're using), are stored in Redis. Combined with the more general output of redis-sampler, this should give you an extremely good idea of what's going on.

所以我自己的问题的解决方案: 在与 redis-cli玩了一段时间后,我发现 DEBUG OBJECT <key>揭示了一些类似于键的 serializedlength的东西,这实际上是我正在寻找的东西..。

对于一个完整的数据库来说,你需要聚合所有 KEYS *的值,这对于你选择的脚本语言来说应该不会太困难... ..。

坏的方面是 Redisio并没有很多关于 DEBUG OBJECT的信息。

看看这个项目,它输出了一些关于基于正则表达式和前缀的关键空间的有趣统计数据。它使用 DEBUG OBJECT命令并扫描数据库,识别键组并估计它们占用的空间百分比。

Https://github.com/snmaynard/redis-audit

产出情况如下:

Summary


---------------------------------------------------+--------------+-------------------+---------------------------------------------------
Key                                                | Memory Usage | Expiry Proportion | Last Access Time
---------------------------------------------------+--------------+-------------------+---------------------------------------------------
notification_3109439                               | 88.14%       | 0.0%              | 2 minutes
user_profile_3897016                               | 11.86%       | 99.98%            | 20 seconds
---------------------------------------------------+--------------+-------------------+---------------------------------------------------

Or this this one: Https://github.com/sripathikrishnan/redis-rdb-tools ,它通过离线分析 Dump.rdb 文件对整个密钥空间进行全面分析。这个也不错。它可以为数据库中的条目提供 avg/min/max 大小,甚至可以基于前缀执行此操作。

你可以使用. net application < a href = “ https://github.com/abhiyx/RedisSizeCalculator”rel = “ nofollow”> https://github.com/abhiyx/redissizecalculator 计算重写键的大小,

请随时给予您的反馈意见相同

这些评论提供的解决方案值得我们给出自己的答案:

redis-cli --bigkeys

MEMORY USAGE key命令给出一个键及其值需要存储在 RAM 中的字节数。

报告的使用情况是数据和管理开销的内存分配总数,这些开销是键的值所需要的 (源文档)

我通常更倾向于使用密钥抽样方法来解决这类场景的故障。

redis-cli -p 6379 -n db_number --bigkeys

例句:-

Redis-cli-p 6370-n 0—— Bigkeys

怎么样

redis-cli get KEYNAME | wc -c

您还可以在 reis 中检查 INFO命令以查看内存使用情况

$ redis-cli
127.0.0.1:6379> INFO memory

如果你知道前缀,你可以快速和键的集合:

echo "keys userfeed*" | redis-cli -h 10.168.229.48 | xargs -I{} echo "debug object {}" | redis-cli -h 10.168.229.48 | awk '{print $5}' | cut -
d: -f2 | awk '{s+=$1} END {print s}'

记住,这是“序列化长度”,而不是内存中的大小。

获取以字节为单位的内存大小:

echo "keys op*" | redis-cli -h 10.168.229.48 | xargs -I{} echo "memory usage {} samples 0" | redis-cli -h 10.168.229.48 | awk '{s+=$1} END {pr
int s}'

One-liner. All redis keys and corresponding used memory.

redis-cli keys "*" | while read line; do echo -n "$line: "; redis-cli memory usage $line; done

Lua 俏皮话

eval "local sum = 0; local matches = redis.call('KEYS', '<pattern>'); for _,key in ipairs(matches) do local val = redis.call('memory', 'usage', key); sum = sum + tonumber(val) end return sum" 0