当前位置: 代码网 > it编程>数据库>Redis > 排查Redis大key的方法总结

排查Redis大key的方法总结

2024年08月14日 Redis 我要评论
排查redis大key的方法redis-cli --bigkeys特点:使用–bigkeys参数会扫描整个redis数据库,应该在低流量峰值时执行这个方法只能返回每种类型中最大的那个big

排查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 '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命令执行。

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) 10485831
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参数,将结果输出到本地文件中。

rdb --command memory --bytes 102400 \\wsl.localhost\ubuntu-20.04\var\lib\redis\dump.rdb -f d:\kevin.csv

以上就是排查redis大key的方法总结的详细内容,更多关于排查redis大key的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com