Redis已经成为web应用开发不可或缺的一个组成部分,在项目中的应用越来越广泛,这篇文章就来讲讲那些关于Redis监控的那点事。
redis-benchmark
1.1 简介
第一个就介绍一下,Redis自带的性能检测工具redis-benchmark, 该工具可以模拟 N 个客户端同时发出 Y 个请求。可以使用 redis-benchmark -h 来查看基准参数。
1.2 命令格式:
redis-benchmark [-h ] [-p ] [-c ] [-n <requests]> [-k ]
1.3 参数介绍:
1.4 实例:
1.4.1 同时执行1000个请求来检测性能:
redis-benchmark -n 1000 -q
1.4.2 50个并发请求,10000个请求,检测Redis性能:
redis-benchmark -h localhost -p 6379 -c 50 -n 10000
[root@localhost toutou]# redis-benchmark -h localhost -p 6379 -c 50 -n 10000
====== PING_INLINE ======
requests completed in 0.11 seconds
parallel clients
bytes payload
keep alive: 1
96.25% <= 1 milliseconds
98.38% <= 2 milliseconds
99.01% <= 3 milliseconds
100.00% <= 4 milliseconds
88495.58 requests per second
====== PING_BULK ======
requests completed in 0.10 seconds
parallel clients
bytes payload
keep alive: 1
97.74% <= 1 milliseconds
100.00% <= 2 milliseconds
95238.10 requests per second
====== SET ======
requests completed in 0.11 seconds
parallel clients
bytes payload
keep alive: 1
98.44% <= 1 milliseconds
100.00% <= 1 milliseconds
93457.95 requests per second
====== GET ======
requests completed in 0.11 seconds
parallel clients
bytes payload
keep alive: 1
98.33% <= 1 milliseconds
99.13% <= 2 milliseconds
100.00% <= 2 milliseconds
93457.95 requests per second
====== INCR ======
requests completed in 0.10 seconds
parallel clients
bytes payload
keep alive: 1
98.28% <= 1 milliseconds
100.00% <= 1 milliseconds
95238.10 requests per second
====== LPUSH ======
requests completed in 0.10 seconds
parallel clients
bytes payload
keep alive: 1
98.70% <= 1 milliseconds
100.00% <= 1 milliseconds
97087.38 requests per second
====== RPUSH ======
requests completed in 0.10 seconds
parallel clients
bytes payload
keep alive: 1
98.66% <= 1 milliseconds
100.00% <= 1 milliseconds
95238.10 requests per second
====== LPOP ======
requests completed in 0.15 seconds
parallel clients
bytes payload
keep alive: 1
93.78% <= 1 milliseconds
96.51% <= 2 milliseconds
97.35% <= 3 milliseconds
98.41% <= 4 milliseconds
99.02% <= 5 milliseconds
99.23% <= 6 milliseconds
99.46% <= 7 milliseconds
99.96% <= 8 milliseconds
99.97% <= 9 milliseconds
100.00% <= 9 milliseconds
67567.57 requests per second
====== RPOP ======
requests completed in 0.31 seconds
parallel clients
bytes payload
keep alive: 1
65.78% <= 1 milliseconds
84.10% <= 2 milliseconds
90.96% <= 3 milliseconds
94.19% <= 4 milliseconds
95.72% <= 5 milliseconds
97.05% <= 6 milliseconds
98.33% <= 7 milliseconds
98.80% <= 8 milliseconds
99.40% <= 9 milliseconds
99.72% <= 10 milliseconds
100.00% <= 14 milliseconds
31746.03 requests per second
====== SADD ======
requests completed in 0.19 seconds
parallel clients
bytes payload
keep alive: 1
93.00% <= 1 milliseconds
96.88% <= 2 milliseconds
98.33% <= 3 milliseconds
98.92% <= 6 milliseconds
98.94% <= 7 milliseconds
98.95% <= 9 milliseconds
99.04% <= 10 milliseconds
99.48% <= 12 milliseconds
99.61% <= 14 milliseconds
99.62% <= 15 milliseconds
99.99% <= 16 milliseconds
100.00% <= 16 milliseconds
52083.33 requests per second
====== HSET ======
requests completed in 0.11 seconds
parallel clients
bytes payload
keep alive: 1
95.90% <= 1 milliseconds
99.95% <= 2 milliseconds
100.00% <= 2 milliseconds
90909.09 requests per second
====== SPOP ======
requests completed in 0.11 seconds
parallel clients
bytes payload
keep alive: 1
97.04% <= 1 milliseconds
99.75% <= 2 milliseconds
99.78% <= 3 milliseconds
100.00% <= 3 milliseconds
90909.09 requests per second
====== LPUSH (needed to benchmark LRANGE) ======
requests completed in 0.11 seconds
parallel clients
bytes payload
keep alive: 1
96.48% <= 1 milliseconds
99.46% <= 2 milliseconds
99.95% <= 3 milliseconds
100.00% <= 3 milliseconds
87719.30 requests per second
====== LRANGE_100 (first 100 elements) ======
requests completed in 0.33 seconds
parallel clients
bytes payload
keep alive: 1
32.63% <= 1 milliseconds
93.24% <= 2 milliseconds
99.83% <= 3 milliseconds
100.00% <= 3 milliseconds
30303.03 requests per second
====== LRANGE_300 (first 300 elements) ======
requests completed in 0.85 seconds
parallel clients
bytes payload
keep alive: 1
2.65% <= 1 milliseconds
23.01% <= 2 milliseconds
53.33% <= 3 milliseconds
77.25% <= 4 milliseconds
91.47% <= 5 milliseconds
98.58% <= 6 milliseconds
99.99% <= 7 milliseconds
100.00% <= 7 milliseconds
11764.71 requests per second
====== LRANGE_500 (first 450 elements) ======
requests completed in 1.22 seconds
parallel clients
bytes payload
keep alive: 1
1.01% <= 1 milliseconds
9.09% <= 2 milliseconds
28.25% <= 3 milliseconds
50.31% <= 4 milliseconds
68.06% <= 5 milliseconds
81.18% <= 6 milliseconds
90.78% <= 7 milliseconds
96.96% <= 8 milliseconds
99.43% <= 9 milliseconds
100.00% <= 9 milliseconds
8196.72 requests per second
====== LRANGE_600 (first 600 elements) ======
requests completed in 1.57 seconds
parallel clients
bytes payload
keep alive: 1
0.61% <= 1 milliseconds
4.90% <= 2 milliseconds
14.77% <= 3 milliseconds
28.67% <= 4 milliseconds
44.56% <= 5 milliseconds
59.45% <= 6 milliseconds
72.38% <= 7 milliseconds
82.29% <= 8 milliseconds
90.01% <= 9 milliseconds
95.42% <= 10 milliseconds
98.34% <= 11 milliseconds
99.78% <= 12 milliseconds
100.00% <= 12 milliseconds
6357.28 requests per second
====== MSET (10 keys) ======
requests completed in 0.19 seconds
parallel clients
bytes payload
keep alive: 1
68.40% <= 1 milliseconds
98.61% <= 2 milliseconds
100.00% <= 3 milliseconds
53763.44 requests per second
[root@localhost toutou]#
redis-cli
2.1 简介
查看redis的连接及读写操作
2.2 命令格式
redis-cli -h xx -p yy monitor
2.3 实例:
2.4 redis-cli info:
Redis 监控最直接的方法就是使用系统提供的 info 命令,只需要执行下面一条命令,就能获得 Redis 系统的状态报告。
# Server
redis_version:5.0.2 # Redis 的版本
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:bf5d1747be5380f
redis_mode:standalone
os:Linux 2.6.32-220.7.1.el6.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.4.7 #gcc版本
process_id:49324 # 当前 Redis 服务器进程id
run_id:bbd7b17efcf108fdde285d8987e50392f6a38f48
tcp_port:6379
uptime_in_seconds:1739082 # 运行时间(秒)
uptime_in_days:20 # 运行时间(天)
hz:10
lru_clock:1734729
config_file:/home/s/apps/RedisMulti_video_so/conf/zzz.conf
# Clients
connected_clients:1 #连接的客户端数量
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:821848 #Redis分配的内存总量
used_memory_human:802.59K
used_memory_rss:85532672 #Redis分配的内存总量(包括内存碎片)
used_memory_peak:178987632
used_memory_peak_human:170.70M #Redis所用内存的高峰值
used_memory_lua:33792
mem_fragmentation_ratio:104.07 #内存碎片比率
mem_allocator:tcmalloc-2.0
# Persistence
loading:0
rdb_changes_since_last_save:0 #上次保存数据库之后,执行命令的次数
rdb_bgsave_in_progress:0 #后台进行中的 save 操作的数量
rdb_last_save_time:1410848505 #最后一次成功保存的时间点,以 UNIX 时间戳格式显示
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0 #redis是否开启了aof
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
# Stats
total_connections_received:5705 #运行以来连接过的客户端的总数量
total_commands_processed:204013 # 运行以来执行过的命令的总数量
instantaneous_ops_per_sec:0
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:34401 #运行以来过期的 key 的数量
evicted_keys:0 #运行以来删除过的key的数量
keyspace_hits:2129 #命中key 的次数
keyspace_misses:3148 #没命中key 的次数
pubsub_channels:0 #当前使用中的频道数量
pubsub_patterns:0 #当前使用中的模式数量
latest_fork_usec:4391
# Replication
role:master #当前实例的角色master还是slave
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:1551.61
used_cpu_user:1083.37
used_cpu_sys_children:2.52
used_cpu_user_children:16.79
# Keyspace
db0:keys=3,expires=0,avg_ttl=0 #各个数据库的 key 的数量,以及带有生存期的 key 的数量
redis-cli info
结果会返回 Server、Clients、Memory、Persistence、Stats、Replication、CPU、Keyspace 8个部分。从info大返回结果中提取相关信息,就可以达到有效监控的目的。
showlog
3.1 简介
redis的slowlog是redis用于记录记录慢查询执行时间的日志系统。由于slowlog只保存在内存中,因此slowlog的效率很高,完全不用担心会影响到redis的性能。Slowlog是Redis从2.2.12版本引入的一条命令。
3.2 命令格式
在redis-cli中有关于slowlog的设置:
CONFIG SET slowlog-log-slower-than 6000
CONFIG SET slowlog-max-len 25
3.3 实例:
上面介绍的都是关于Redis自带的命令化性能查询工具。下面介绍介绍一些第三方的Redis可视化性能监控工具。
RedisLive
4.1 简介
RedisLive是由Python编写的开源的图形化监控工具。核心服务部分只包括一个web服务和基于Redis自带的Info命令以及monitor命令的监控服务。支持多实例监控,监控信息可以使用redis存储和sqlite持久化存储。
4.2 安装
4.2.1 安装依赖环境
RedisLive是由Python2.X编写的,所以最好使用Python2.7来运行RedisLive,在CentOS 7中预安装了Python2.7,但没有安装Python的包管理器pip。
yum install epel-release
sudo yum install python-pip
pip install --upgrade pip
pip install tornado
pip install redis
pip install python-dateutil
4.2.2 安装RedisLive
git clone https://github.com/nkrode/RedisLive.git
4.2.3 修改配置文件redis-live.conf
cd RedisLive/src
//按照以下方式修改配置文件
{
"RedisServers":
[
#在此处添加需要监控的redis实例
{
"server": "127.0.0.1", #redis监听地址,此处为本机
"port" : 6379, #redis端口号,可以通过lsof -i | grep redis-ser查看 redis-server端口号
"password" : "some-password" #redis认证密码,如果没有可以删除该行,注意json格式
}
],
"DataStoreType" : "redis", #监控数据存储方案的配置,可选择redis或sqllite
#用来存储监控数据的 Redis 实例
"RedisStatsServer":
{
"server" : "127.0.0.1",
"port" : 6379,
"password" : "some-password"
},
#监控数据持久化数据存储配置
"SqliteStatsStore" :
{
"path": "db/redislive.sqlite" #redis数据文件
}
}
redis-live.conf的配置可以参考redis-live.conf.example
4.3 启动
启动监控服务,每60秒监控一次
./redis-monitor.py --duration=60
再次开启一个终端,进入/root/RedisLive/src目录,启动web服务
./redis-live.py
4.4 效果图
redis-faina
5.1 简介
5.1.1 背景
redis-faina是由Instagram开发并开源的一个 Redis 查询分析小工具。Instagram团队曾经使用 PGFouine 来作为其PostgreSQL的查询分析工具,他们觉得Redis也需要一个类似的工具来进行query分析工作,于是开发了 redis-faina。
5.1.2 概念
redis-faina 是通过Redis的 MONITOR命令来实现的,通过对在Redis上执行的query进行监控,统计出一段时间的query特性。
5.2 安装
git clone https://github.com/facebookarchive/redis-faina.git
5.3 命令介绍
[root@localhost toutou]# cd redis-faina/
[root@localhost redis-faina]# ls
heroku-redistogo-faina.sh LICENSE README.md redis-faina.py
[root@localhost redis-faina]# ./redis-faina.py -h
usage: redis-faina.py [-h] [--prefix-delimiter PREFIX_DELIMITER]
[--redis-version REDIS_VERSION]
[input]
positional arguments:
input File to parse; will read from stdin otherwise
optional arguments:
-h, --help show this help message and exit
--prefix-delimiter PREFIX_DELIMITER
String to split on for delimiting prefix and rest of
key
--redis-version REDIS_VERSION
Version of the redis server being monitored
[root@localhost redis-faina]#
其中 --prefix-delimiter 主要用于统计前缀的key的数据。
可以通过 redis MONITOR 命令以及管道进行分析,例如:
redis-cli -p 6379 MONITOR | head -n | ./redis-faina.py [options]
或者
redis-cli -p 6379 MONITOR > outfile.txt
./redis-faina.py ./outfile.txt
Overall Stats
========================================
Lines Processed 117773
Commands/Sec 11483.44
Top Prefixes
========================================
friendlist 69945
followedbycounter 25419
followingcounter 10139
recentcomments 3276
queued 7
Top Keys
========================================
friendlist:zzz:1:2 534
followingcount:zzz 227
friendlist:zxz:1:2 167
friendlist:xzz:1:2 165
friendlist:yzz:1:2 160
friendlist:gzz:1:2 160
friendlist:zdz:1:2 160
friendlist:zpz:1:2 156
Top Commands
========================================
SISMEMBER 59545
HGET 27681
HINCRBY 9413
SMEMBERS 9254
MULTI 3520
EXEC 3520
LPUSH 1620
EXPIRE 1598
Command Time (microsecs)
========================================
Median 78.25
75% 105.0
90% 187.25
99% 411.0
Heaviest Commands (microsecs)
========================================
SISMEMBER 5331651.0
HGET 2618868.0
HINCRBY 961192.5
SMEMBERS 856817.5
MULTI 311339.5
SADD 54900.75
SREM 40771.25
EXEC 28678.5
Slowest Calls
========================================
3490.75 "SMEMBERS" "friendlist:zzz:1:2"
2362.0 "SMEMBERS" "friendlist:xzz:1:3"
2061.0 "SMEMBERS" "friendlist:zpz:1:2"
1961.0 "SMEMBERS" "friendlist:yzz:1:2"
1947.5 "SMEMBERS" "friendlist:zpz:1:2"
1459.0 "SISMEMBER" "friendlist:hzz:1:2" "zzz"
1416.25 "SMEMBERS" "friendlist:zhz:1:2"
1389.75 "SISMEMBER" "friendlist:zzx:1:2" "zzz"
总结
关于Redis的监控工具还有很多,这里就不一一列举了,下面给出其它几款优秀的Redis监控工具链接,感兴趣的可以看看。
其他监控工具:
https://github.com/junegunn/redis-stat
https://github.com/steelThread/redmon
https://github.com/oliver006/redis_exporter