当前位置:网站首页>Redis 排查大 key 的4种方法,优化必备
Redis 排查大 key 的4种方法,优化必备
2022-07-06 04:37:00 【Java笔记虾】
摘要:在日常Redis的使用中,难免遇到因为 key 存储了过大的数据而造成请求缓慢甚至阻塞的情况,这个时候就需要排查 Redis 的大key去优化业务了,下面提供一些排查方案总结,仅供参考。
一、多大的 key 算大呢?
Redis 实践总结(仅供参考):
合理的 Key 中 Value 的字节大小,推荐小于 10 KB。
过大的 Value 会引发数据倾斜、热点Key、实例流量或 CPU 性能被占满等问题,应从设计源头上避免此类问题带来的性能影响。
那么 value Bytes > 10 kb 可以作为判断 大 key 的一个参考值。
二、排查大 key 的方法
1、使用命令 --bigkeys
--bigkeys
是 redis 自带的命令,对整个 Key 进行扫描,统计 string,list,set,zset,hash 这几个常见数据类型中每种类型里的最大的 key。
string 类型统计的是 value 的字节数;另外 4 种复杂结构的类型统计的是元素个数,不能直观的看出 value 占用字节数,所以 --bigkeys 对分析 string 类型的大 key 是有用的,而复杂结构的类型还需要一些第三方工具。
注:元素个数少,不一定 value 不大;元素个数多,也不一定 value 就大
redis-cli -h 127.0.0.1 -p 6379 -a "password" --bigkeys
--bigkeys
是以 scan 延迟计算的方式扫描所有 key,因此执行过程中不会阻塞 redis,但实例存在大量的 keys 时,命令执行的时间会很长,这种情况建议在 slave 上扫描。
–-bigkeys
其实就是找出类型中最大的 key,最大的 key 不一定是大 key,最大的 key 都不超过 10kb 的话,说明不存在大 key。
但某种类型如果存在较多的大key (>10kb),只会统计 top1 的那个 key,如果要统计所有大于 10kb 的 key,需要用第三方工具扫描 rdb 持久化文件。
2、使用 memory 命令查看 key 的大小(仅支持 Redis 4.0 以后的版本)
redis-cli -h 127.0.0.1 -p 6379 -a password
MEMORY USAGE keyname1
(integer) 157481
MEMORY USAGE keyname2
(integer) 312583
3、使用 Rdbtools 工具包
Rdbtools 是 python写的 一个第三方开源工具,用来解析 Redis 快照文件。除了解析 rdb 文件,还提供了统计单个 key 大小的工具。
1、安装
git clone https://github.com/sripathikrishnan/redis-rdb-tools
cd redis-rdb-tools sudo && python setup.py install
2、使用
从 dump.rdb
快照文件统计, 将所有 > 10kb 的 key 输出到一个 csv 文件
rdb dump.rdb -c memory --bytes 10240 -f live_redis.csv
4、使用 go-redis-bigkv
go-redis-bigkv 是本人开发的一个小工具。主要是 基于 memory 命令,扫描 redis 中所有的 key,并将结果按照 内存大小进行排序,并将排序后的 结果输出到 txt 文件中。因为是 以 scan 延迟计算的方式扫描所有 key,因此执行过程中不会阻塞 redis,但实例存在大量的 keys 时,命令执行的时间会很长。
项目地址:
https://github.com/th3ee9ine/go-redis-bigk
推荐:
最全的java面试题库
PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧!
边栏推荐
- [face recognition series] | realize automatic makeup
- HotSpot VM
- 729. My schedule I (set or dynamic open point segment tree)
- Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps
- Distributed transaction solution
- Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
- [Zhao Yuqiang] deploy kubernetes cluster with binary package
- Implementation of knowledge consolidation source code 2: TCP server receives and processes half packets and sticky packets
- I'd like to ask about the current MySQL CDC design. In the full volume phase, if a chunk's binlog backfill phase,
- After learning classes and objects, I wrote a date class
猜你喜欢
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
Mysql数据库慢sql抓取与分析
11. Intranet penetration and automatic refresh
canal同步mysql数据变化到kafka(centos部署)
Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码
[tomato assistant installation]
View workflow
[Chongqing Guangdong education] engineering fluid mechanics reference materials of southwestjiaotonguniversity
Cross domain and jsonp details
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
随机推荐
The value of two date types is subtracted and converted to seconds
Digital children < daily question> (Digital DP)
[Zhao Yuqiang] deploy kubernetes cluster with binary package
[tomato assistant installation]
【HBZ分享】ArrayList的增删慢查询快的原因
2328. Number of incremental paths in the grid graph (memory search)
[HBZ share] reasons for slow addition and deletion of ArrayList and fast query
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
How to estimate the population with samples? (mean, variance, standard deviation)
【HBZ分享】云数据库如何定位慢查询
After learning classes and objects, I wrote a date class
【Try to Hack】john哈希破解工具
tengine 内核参数
Recommendation system (IX) PNN model (product based neural networks)
Bubble sort
In depth MySQL transactions, stored procedures and triggers
SharedPreferences 源码分析
Dynamic programming (tree DP)
[face recognition series] | realize automatic makeup
Etcd database source code analysis -- etcdserver bootstrap initialization storage