当前位置:网站首页>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:因為公眾號平臺更改了推送規則,如果不想錯過內容,記得讀完點一下“在看”,加個“星標”,這樣每次新文章推送才會第一時間出現在你的訂閱列錶裏。點“在看”支持我們吧!
边栏推荐
- Data processing methods - smote series and adasyn
- Knowledge consolidation source code implementation 3: buffer ringbuffer
- MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
- C. The third problem
- 【HBZ分享】ArrayList的增删慢查询快的原因
- word封面下划线
- cdc 能全量拉去oracle 表嘛
- Slow SQL fetching and analysis of MySQL database
- One question per day (Mathematics)
- NPM command -- install dependent packages -- Usage / explanation
猜你喜欢
The implementation of the maize negotiable digital warehouse receipt standard will speed up the asset digitization process of the industry
1291_ Add timestamp function in xshell log
8. Static file
The most detailed and comprehensive update content and all functions of guitar pro 8.0
In depth MySQL transactions, stored procedures and triggers
Easyrecovery reliable and toll free data recovery computer software
Mlapi series - 04 - network variables and network serialization [network synchronization]
Visio draw fan
Fedora/rehl installation semanage
JVM garbage collector concept
随机推荐
C. The third problem
牛顿插值法
P3500 [poi2010]tes intelligence test (two points & offline)
Can Flink SQL read multiple topics at the same time. How to write in with
View 工作流程
Delete subsequence < daily question >
Introduction to hashtable
11. Intranet penetration and automatic refresh
Sqlserver query results are not displayed in tabular form. How to modify them
Uva1592 Database
Digital children < daily question> (Digital DP)
2328. 网格图中递增路径的数目(记忆化搜索)
Vulnerability discovery - vulnerability probe type utilization and repair of web applications
Implementation of knowledge consolidation source code 2: TCP server receives and processes half packets and sticky packets
newton interpolation
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
HotSpot VM
The value of two date types is subtracted and converted to seconds
Solve the compilation problem of "c2001: line breaks in constants"