使用redis-cli 发现热key

数据库NoSQL数据库技术服务知识库
场景介绍

热 key 是指用户访问一个 key 的 QPS 特别高,导致 Server 实例出现 CPU 负载突增或者不均的情况,如果未能及时发现和处理,可能导致服务性能下降,用户体验变差,进而可能引发大规模故障。

分析热 key 的方法有很多,本文专注于如何使用原生 redis-cli 工具,通过添加 --hotkeys 选项,可以快找出业务中的热 key。

前提条件
  • ECS中已经安装了 Redis 4.0 以上版本;目的是为了使用 redis-cli 工具。
  • 保证客户端与 redis 网络互通。
  • Redis 实例的 maxmemory-policy 参数设置为 volatile-lfu 或 allkeys-lfu。
  • 直连地址无法支持 object 命令,请使用其地址。
操作步骤

运行如下命令,可以在整个实例级别来发现热 key:

(base) [root@rudonx ~]# redis-cli -h xxxxxx.redis.ivolces.com -a xxxxx --hotkeys
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

# Scanning the entire keyspace to find hot 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%] Hot key '"key:{tag}:000000000158"' found so far with counter 23
[00.00%] Hot key '"key:{tag}:000000000102"' found so far with counter 20
[00.00%] Hot key '"key:{tag}:000000000082"' found so far with counter 26
[00.00%] Hot key '"key:{tag}:000000000126"' found so far with counter 23
[00.00%] Hot key '"key:{tag}:000000000081"' found so far with counter 23
[00.00%] Hot key '"key:{tag}:000000000186"' found so far with counter 21
[00.00%] Hot key '"key:{tag}:000000000148"' found so far with counter 22
[00.00%] Hot key '"key:{tag}:000000000049"' found so far with counter 26
[00.00%] Hot key '"key:{tag}:000000000122"' found so far with counter 19
[00.00%] Hot key '"key:{tag}:000000000165"' found so far with counter 21
.......
-------- summary -------
Sampled 200 keys in the keyspace!
hot key found with counter: 30  keyname: "key:{tag}:000000000137"
hot key found with counter: 30  keyname: "key:{tag}:000000000114"
hot key found with counter: 28  keyname: "key:{tag}:000000000143"
hot key found with counter: 27  keyname: "key:{tag}:000000000130"
hot key found with counter: 27  keyname: "key:{tag}:000000000175"
hot key found with counter: 26  keyname: "key:{tag}:000000000082"
hot key found with counter: 26  keyname: "key:{tag}:000000000049"
hot key found with counter: 26  keyname: "key:{tag}:000000000179"
hot key found with counter: 26  keyname: "key:{tag}:000000000133"
hot key found with counter: 26  keyname: "key:{tag}:000000000154"
hot key found with counter: 26  keyname: "key:{tag}:000000000038"
hot key found with counter: 26  keyname: "key:{tag}:000000000028"
hot key found with counter: 26  keyname: "key:{tag}:000000000194"
hot key found with counter: 26  keyname: "key:{tag}:000000000046"
hot key found with counter: 26  keyname: "key:{tag}:000000000139"
hot key found with counter: 26  keyname: "key:{tag}:000000000182"

参数解释

  • -h:Redis 实例的地址。
  • -a:指定 Redis 实例的密码。
  • --hotkeys:进行热 key 分析。

报错解决

Error: ERR unknown command 'OBJECT'

解决方案:出现此错误是由于使用 redis 直连地址,不支持 OBJECT 命令导致,请使用其他地址类型。参考文档[1]

(base) [root@rudonx ~]# redis-cli -h xxxx-direct.redis.ivolces.com  -a xxxx --hotkeys
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Scanning the entire keyspace to find hot 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).

Error: ERR unknown command 'OBJECT'

Error: ERR An LFU maxmemory policy is not selected

解决方案:Redis 实例的 maxmemory-policy 参数需要设置为 volatile-lfu 或 allkeys-lfu。

(base) [root@rudonx ~]# redis-cli -h xxxx-direct.redis.ivolces.com  -a xxxx --hotkeys
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

# Scanning the entire keyspace to find hot 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).

Error: ERR An LFU maxmemory policy is not selected, access frequency not tracked. Please note that when switching between policies at runtime LRU and LFU data will take some time to adjust.

Couldn't determine DBSIZE!

解决方案:请检查网络联通性,密码的正确性。

(base) [root@rudonx ~]# redis-cli -h xxxx-direct.redis.ivolces.com  -a xxxx --hotkeys
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Couldn't determine DBSIZE!

参考文档

[1] https://www.volcengine.com/docs/6293/72119

0
0
0
0
关于作者

文章

0

获赞

0

收藏

0

所属团队号:
相关资源
抖音连麦音画质体验提升与进阶实践
随着互娱场景实时互动创新玩法层出不穷,业务伙伴对 RTC「体验」和「稳定」的要求越来越高。火山引擎 RTC 经历了抖音 6 亿 DAU 的严苛验证和打磨,在架构设计、音画质提升、高可靠服务等方面沉淀了丰富的经验,本次演讲将和大家分享火山引擎 RTC 在直播连麦等场景中的技术优化及其带来的新玩法。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论