排查Redis大key的方法總結
排查Redis大key的方法
redis-cli --bigkeys
特點:
- 使用–bigkeys參數(shù)會掃描整個Redis數(shù)據(jù)庫,應該在低流量峰值時執(zhí)行
- 這個方法只能返回每種類型中最大的那個bigkey,無法得到大小排到前N位的bigkey
- 對于集合類型來說,這個方法只統(tǒng)計集合元素的多少,而不是實際占用的內存量。因為一個集合中元素個數(shù)多,并不一定占用內存就多
@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 '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 bytes 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命令執(zhí)行。
SCAN命令可以用于迭代遍歷所有key。它是一個非阻塞操作,支持游標(cursor)的方式來逐步遍歷所有key。使用SCAN命令可以避免阻塞,減少對Redis性能的影響。
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) 10485831 127.0.0.1:16379>
這樣組合的方式操作比較復雜,需要對命令使用非常熟悉。在生產環(huán)境需要更快,更高效的發(fā)現(xiàn)問題還是建議使用成熟的分析工具,畢竟也都是用這些命令組合起來的。
使用云上的Redis可以直接使用CloundDBA功能
redis-rdb-tools
該三方工具Github地址redis-rdb-tools。
安裝該分析工具
python setup.py install
要使用memory功能,需要安裝
pip3 install python-lzf
如果出現(xiàn)沒有權限的問題,那就以管理員打開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
參數(shù),將結果輸出到本地文件中。
rdb --command memory --bytes 102400 \\wsl.localhost\Ubuntu-20.04\var\lib\redis\dump.rdb -f d:\kevin.csv
以上就是排查Redis大key的方法總結的詳細內容,更多關于排查Redis大key的資料請關注腳本之家其它相關文章!
相關文章
redis cluster支持pipeline的實現(xiàn)思路
本文給大家介紹redis cluster支持pipeline的實現(xiàn)思路,在 cluster 上執(zhí)行 pipeline 可能會由于 redis 節(jié)點擴縮容 中途 redirection 切換連接導致結果丟失,具體細節(jié)問題請參考下本文2021-06-06redis執(zhí)行l(wèi)ua腳本的實現(xiàn)方法
redis在2.6推出了腳本功能,允許開發(fā)者使用Lua語言編寫腳本傳到redis中執(zhí)行。本文就介紹了redis執(zhí)行l(wèi)ua腳本的實現(xiàn)方法,感興趣的可以了解一下2021-11-11redis-cli創(chuàng)建redis集群的實現(xiàn)
本文主要介紹了redis-cli創(chuàng)建redis集群的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-06-06