排查Redis大key的方法总结
目录
- 排python查Redis大key的方法
- redis-cli --bigkeys
- redis-cli scan VS memory usage组合
- 使用云上的Redis可以直接使用CloundDBA功能
- redis-rdb-tools
排查Redis大key的方法
redis-cli --bigkeys
特点:
- 使用–bigkeys参数会扫描整个Redis数据库,应该在低流量峰值时执行
- 这个方法只能返回每种类型中最大的那个bigkey,无法得到大小排到前N位的bigkey
- 对于集合类型来说,这个方法只统计集合元素的多少,而不是实际占用的内存量。因为一个集合中元素个数多,并不一定占用内存就多
@GetMapping("init") @Async public void initData(@RequestParam (name = "size", defaultValue = "5000") Integer size){ redisTemplate.opsForValue().set("string_large_key1", generateTestData(10* 1024)); redisTemplate.opsForValue().set("string_large_key2", generateTestData(10* 1024)); redisTemplate.opsForSet().add("set_large_key1", new HashSet<>(50000)); redisTemplate.opsForSet().add("set_large_key2", new HashSet<>(50000)); redisTemplate.opsForHash().putAll("hash_large_key1", buildMapData(50000)); redisTemplate.opsForHash().putAll("hash_large_key2", buildMapData(50000)); redisTemplate.opsForList().rightPushAll("list_large_key1", buildListData(50000)); redisTemplate.opsForList().rightPushAll("list_large_key2", buildListData(50000)); } private Map buildMapData(int initialCapacity){ Map<String, String> result =new HashMap<>(initialCapacity); for (int i = 0; i < initialCapacity; i++) { result.put("kevin_" + i, "123"); } return result; } private List<String> buildListData(int initialCapacity){ List<String> result = new ArrayList<>(initialCapacity); for (int i = 0; i < initialCapacity; i++) { result.add("kevin_" + i ); } return result; }
- 使用–bigkeys查询
root@DESKTOP-0js7U4E:~# redis-cli -h 127.0.0.1 -p 16379 --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 hash found so far 'hash_large_key2' with 50000 fields # 只返回了最大的那个bigkey [00.00%] Biggest string found so far javascript'string_large_key2' with 10485762 bytes [00.00%] Biggest set found so far 'set_large_key2' with 1 members [00.00%] Biggest list found so far 'list_large_key1' with 50000 items -------- summary ------- Sampled 9 keys in the keyspace! Total key length in bpythonytes is 135 (avg len 15.00) Biggest list found 'list_large_key1' has 50000 items Biggest hash found 'hash_large_key2' has 50000 fields Biggest string found 'string_large_key2' has 10485762 bytes Biggest set found 'set_large_key2' has 1 members # 返回了list的容量 2 lists with 100000 items (22.22% of keys, avg size 50000.00) # 返回了hash的容量 2 hashs with 100000 fields (22.22% of keys, avg size 50000.00) 2 strings with 20971524 bytes (22.22% of keys, avg size 10485762.00) 0 streams with 0 entries (00.00% of keys, avg size 0.00) 3 sets with 3 members (33.33% of keys, avg size 1.00) 0 zsets with 0 members (00.00% of keys, avg size 0.00) root@DESKTOP-0JS7U4E:~#
redis-cli scan VS memory usage组合
实际上bigkey的底层也使用SCAN命令执行。
SCAN命令可以用于迭代遍历所有key。它是一个非阻塞操作,支持游标(cursor)的方式来逐步遍历所有key。使用SCAN命令可以避免阻塞,减少对Redis性能的影响。
Redis Scan命令说明
1、先使用scan扫描出key
127.0.0.1:16379> scan 1000 MATCH "string*" 1) "0" 2) 1) "string_large_key2" 2) "string_large_key1" 127.0.0.1:16379> 127.0.0.1:16379> scan 0 MATCH "string*" count 20 1) "0" 2) 1) "string_large_key2" 2) "string_large_key1" 127.0.0.1:16379>
2、使用 memory usage
查询key占用的内存大小
127.0.0.1:16379> memory usage string_large_key1 (integer) 1编程客栈0485831 127.0.0.1:16379>
这样组合的方式操作比较复杂,需要对命令使用非常熟悉。在生产环境需要更快,更高效的发现问题还是建议使用成熟的分析工具,毕竟也都是用这些命令组合起来的。
使用云上的Redis可以直接使用CloundDBA功能
redis-rdb-tools
该三方工具github地址redis-rdb-tools。
安装该分析工具
python setup.py install
要使用memory功能,需要安装
pip3 install python-lzf
如果出现没有权限的问题,那就以管理员打开cmd再运行
error: [Errno 13] Permission denied: 'C:\\Python310\\Scripts\\rdb-script.py'
安装完成之后目录下面多出这几个文件。
C:\Python310\Scripts>dir .................... 2024/08/08 13:38 996 rdb-script.py 2024/08/08 13:38 74,752 rdb.exe 2024/08/08 13:38 1,030 redis-memory-for-key-script.py 2024/08/08 13:38 74,752 redis-memory-for-key.exe 2024/08/08 13:38 1,018 redis-profiler-script.py 2024/08/08 13:38 74,752 redis-profiler.exe C:\Python310\Scripts>
使用rdb进行分析
C:\Python310\Scripts>rdb --command memory --bytes 102400 \\wsl.localhost\Ubuntu-20.04\var\lib\redis\dump.rdb database,type,key,size_in_bytes,encoding,num_elements,len_largest_element,expiry 0,hash,hash_large_key2,3186588,hashtable,50000,11, 0,hash,hash_large_key1,3186588,hashtable,50000,11, 0,string,string_large_key1,12582976,string,10485762,10485762, 0,list,list_large_key1,744355,quicklist,50000,13, 0,string,string_large_key2,12582976,string,10485762,10485762, 0,list,list_large_key2,744355,quicklist,50000,13, C:\Python310\Scripts>
也可以加上-f
参数,将结果输出到本地文件中。
rjsdb --command memory --bytes 102400 \\wsl.localhost\Ubuntu-20.04\var\lib\redis\dump.rdb -f d:\kevin.csv
以上就是排查Redis大key的方法总结的详细内容,更多关于排查Redis大key的资料请关注编程客栈(www.devze.com)其它相关文章!
精彩评论