redis内存分析工具rdbtools

当Redis的内存已经快满的时候,我们能做什么呢?
最直接的方法就是分析一下Redis内存的构成,看是哪些键比较大,或者比较多,然后考虑一下对应的功能能不能优化,例如减少超时时间,例如不必要的数据不用放缓存,例如有些键已经没有用了,但是没有及时删除,也没有超时时间
分析Redis内存的构成,常用的是用分析工具rdbtools,这个工具可以导出redis的所有key,以及它占用的内存大小。

文档

一、安装

pip install python-lzf
pip install rdbtools

能执行rdb命令,表示安装成功

二、使用

(vsing_backend) [www@NingBo_10_1_33_26 data]$ rdb --help
Usage: rdb [options] /path/to/dump.rdb

Example : rdb --command json -k "user.*" /var/redis/6379/dump.rdb

Options:
  -h, --help            show this help message and exit
  -c FILE, --command=FILE
                        Command to execute. Valid commands are json, diff,
                        justkeys, justkeyvals, memory and protocol
  -f FILE, --file=FILE  Output file
  -n DBS, --db=DBS      Database Number. Multiple databases can be provided.
                        If not specified, all databases will be included.
  -k KEYS, --key=KEYS   Keys to export. This can be a regular expression
  -o NOT_KEYS, --not-key=NOT_KEYS
                        Keys Not to export. This can be a regular expression
  -t TYPES, --type=TYPES
                        Data types to include. Possible values are string,
                        hash, set, sortedset, list. Multiple typees can be
                        provided.                      If not specified, all
                        data types will be returned
  -b BYTES, --bytes=BYTES
                        Limit memory output to keys greater to or equal to
                        this value (in bytes)
  -l LARGEST, --largest=LARGEST
                        Limit memory output to only the top N keys (by size)
  -e ESCAPE, --escape=ESCAPE
                        Escape strings to encoding: raw (default), print,
                        utf8, or base64.

三、基本命令

分析rdb文件,生成内存使用报告:

 rdb -c memory -k "test*" dump.rdb > /data1/kevinlu/a.csv
  • -c memory
  • -k 表示只统计那些keys,支持通配符
  • dump.rdb redis的RDB文件,通过bgsave生成
  • 把结果导出到/data1/kevinlu/a.csv文件

注意:

  • 报告中的内存使用量,单位是字节。报告中的内存使用量只是近似值。实际的内存使用量会比报告的使用量稍大,大概1.5倍到1.7倍左右。(测试的时候,Redis info命令的内存占用是8G,dump.rdb文件是2.7G,报告的内存使用总量是5G)

四、redis RDB文件生成

  1. 通过命令bgsave来生成,这样不影响线上业务
  2. 生成前需要备份原有的dump.rdb,不然会覆盖
  3. 生成前注意看机器内存,例如如果Redis内存已经到7G,那bgsave生成的过程中,redis进程需要再占7G内存,要注意内存够不够
  4. 注意硬盘够不够,不过这个一般是够的
原文地址:https://www.cnblogs.com/Xjng/p/11350833.html