一、Slowlog(慢查询)
记录超过指定查询时间的系统,日志记录在内存中,有队列保存,超过最大队列长度最老的记录将会移除。
slowlog-log-slower-than 10000 单位微妙,超过这个执行时间将会记录日志
slowlog-max-len 128 队列长度,保留的最大条数
redis 127.0.0.1:6379> slowlog get 2
1) 1) (integer) 14 #唯一ID
2) (integer) 1309448221 #时间戳
3) (integer) 15 #时间毫秒
4) 1) "slowlog" #命令数组
2) "get"
3) "100"
5) "127.0.0.1:58217" #客户端IP:PORT 4.0以上版本
6) "work-123" #client setname
官网参考: https://redis.io/commands/slowlog
二、Latency Monitoring(延时监控)
1> Measuring latency(测量延时)
redis-cli(客户端)提供了测量延迟的工具
-
使用--latency选项,cli将运行一个循环,将PING命令发送到Redis实例,并测量响应时间,每秒发送100次,统计时间以毫秒为单位。
-
使用--latency-history选项,每15秒新的采样会重新开始。
-
使用--latency-dist选项,使用彩色终端显示一系列延时特征。
-
使用--intrinsic-latency选项,将会检查cli客户端所在机器的延迟,即操作系统内核的延迟,后面为固有检查时间,一般100即可。
$ redis-cli --latency
min: 0, max: 1, avg: 0.19 (427 samples)
$ redis-cli --latency-history
min: 0, max: 1, avg: 0.14 (1314 samples) -- 15.01 seconds range
min: 0, max: 1, avg: 0.18 (1299 samples) -- 15.00 seconds range
$ redis-cli --intrinsic-latency 100
Max latency so far: 1 microseconds.
Max latency so far: 16 microseconds.
Max latency so far: 50 microseconds.
Max latency so far: 53 microseconds.
Max latency so far: 83 microseconds.
Max latency so far: 115 microseconds.
官网参考: https://redis.io/topics/rediscli
2>Monitoring Latency (监控延时)
上述测量延迟客户端提供,个人感觉适合在集群建立处测量使用,当业务平稳运行时,如需监控业务请求的延迟,需要服务端内部机制。
首先需要修改配置开启延迟监控,官方建议虽然这个监控消耗的内存较少,但是如果redis集群运行良好,没有充分的理由开启监控提高redis集群对内存的占用率。
CONFIG SET latency-monitor-threshold 100 #单位毫秒,表示超过100毫秒的响应将会被监控,默认为0,表示关闭状态
Latency有五种子命令,在介绍他们之前,首先需要了解redis对不同的代码命令有不同的名称称之位事件,比如commend是一个测量可能为慢命令执行延时的事件,而fast-commend是监视O(1)和O(logN)命令的事件名称,又比如fork事件仅监视fork(2)系统调用。
Latency Latest
127.0.0.1:6379> latency latest
1) 1) "command" #事件名称
2) (integer) 1405067976 #延迟发生的时间戳
3) (integer) 251 #延迟 毫秒
4) (integer) 1001 #事件的最大延迟
Latency History
127.0.0.1:6379> latency history command #查看commend事件的历史延迟 返回160个元素
1) 1) (integer) 1405067822 #时间戳
2) (integer) 251 #延迟
2) 1) (integer) 1405067941
2) (integer) 1001
Latency Reset
重置所有事件,丢弃当前记录延迟峰值,重置最大事件寄存器。
Latency Graph
图形化展示。
Latency Doctor
列出指导建议。
官方参考: https://redis.io/topics/latency-monitor
3>Latency reason
解决排查延迟问题,有时还需要知道延迟产生的原因,官网总结可能如下:
-
Latency induced by network and communication
-
Single threaded nature of Redis
-
Latency generated by slow commands
-
Latency generated by fork
-
Fork time in different systems
-
Latency induced by transparent huge pages
-
Latency induced by swapping (operating system paging)
-
Latency due to AOF and disk I/O
-
Latency generated by expires