零、概述
在redis cluster中经常会出现下面的情况,集群中的一个节点内存95%了,可是其他的才20-30%,大部分原因是集群中某些节点存在大key。
一、查找大key
Redis官方提供了--bigkeys 命令来查找大key
例如:
[root@ipcpu-redis ~]# redis-cli --bigkeys
# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).
[00.00%] Biggest string found so far '"impression:period:strategy:579:userId:f66b2839262f"' with 1 bytes
[00.00%] Biggest string found so far '"pass:last:update:uid:10212846649"' with 13 bytes
[00.00%] Biggest string found so far '"browser:navigation:lid:10170657990"' with 677 bytes
[00.00%] Biggest set found so far '"pass:removals:uid:10217623325"' with 2 members
[00.00%] Biggest string found so far '"browser:navigation:lid:10219149653"' with 3516 bytes
[00.02%] Biggest string found so far '"browser:navigation:lid:10208824123"' with 3832 bytes
[00.03%] Biggest string found so far '"browser:navigation:lid:10180242059"' with 4046 bytes
[00.05%] Biggest string found so far '"browser:navigation:lid:10126395487"' with 4279 bytes
[00.08%] Biggest string found so far '"pass:data:uid:10029247729"' with 6318 bytes
[00.11%] Biggest set found so far '"pass:removals:uid:10180995079"' with 34 members
[00.25%] Biggest string found so far '"pass:data:uid:10084747582"' with 10088 bytes
[00.32%] Biggest string found so far '"pass:data:uid:10129088379"' with 11806 bytes
[01.12%] Biggest string found so far '"pass:data:uid:10158643786"' with 14170 bytes
[01.24%] Biggest string found so far '"pass:data:uid:10182731762"' with 15852 bytes
[01.42%] Biggest string found so far '"pass:data:uid:10028668052"' with 28877 bytes
[02.87%] Biggest hash found so far '"plugins:update:time"' with 52 fields
[10.94%] Biggest string found so far '"search:pc:browser:keywords:611"' with 69029 bytes
[11.76%] Biggest set found so far '"pass:removals:uid:10173864283"' with 216 members
[16.18%] Biggest hash found so far '"book_mark_hash"' with 3092951 fields
[31.63%] Biggest set found so far '"sn:require:strategy:id:885"' with 50000 members
[45.35%] Biggest string found so far '"search:pc:browser:keywords:887"' with 2455925 bytes
[57.16%] Biggest hash found so far '"version_hash"' with 3187899 fields
[96.55%] Biggest hash found so far '"newAll"' with 44663331 fields
-------- summary -------
Sampled 586240 keys in the keyspace!
Total key length in bytes is 28345793 (avg len 48.35)
Biggest hash found '"newAll"' has 44663331 fields
Biggest string found '"search:pc:browser:keywords:887"' has 2455925 bytes
Biggest set found '"sn:require:strategy:id:885"' has 50000 members
0 lists with 0 items (00.00% of keys, avg size 0.00)
6 hashs with 54101170 fields (00.00% of keys, avg size 9016861.67)
584988 strings with 117329811 bytes (99.79% of keys, avg size 200.57)
0 streams with 0 entries (00.00% of keys, avg size 0.00)
1246 sets with 53753 members (00.21% of keys, avg size 43.14)
0 zsets with 0 members (00.00% of keys, avg size 0.00)
如出现TYPE returned an error: MOVED 12346 10.192.10.74:6379 这样的错误,则说明操作节点不是MASTER节点,需要去MASTER节点操作。
二、结果初步分析
虽然我们分别找到了最大hash类型key是newAll,最大set类型key是sn:require:strategy:id:885,但是他们占用了多少内存,我们仍然不清楚,所以说,--bigkeys出来的结果仅供参考,要想查询具体是谁,还需要分析RDB文件。
三、RDB内容分析
雪球开源的RDR工具,速度比较快,我们采用了这个来分析。
https://github.com/xueqiu/rdr
注意不要在MASTER上执行bgsave,容易出问题。
分析结果是个页面,如下
通过这个页面,我们才真正找到了最终凶手,book_mark_hash, 占用了8个G。
转载请注明:IPCPU-网络之路 » Redis大Key查找bigkeys