系列文章目录
文章目录
- 系列文章目录
- 前言
- 一、慢查询日志
- 二、慢查询的配置参数
- 三、如何配置慢查询日志
- 3.1 直接在配置文件redis.conf配置
- 3.2 使用命令config set进行动态修改
- 四、慢查询日志查询
- 4.1 获取慢查询日志
- 4.2 获取慢查询日志列表当前的长度
- 4.3 慢查询日志重置
- 总结
前言
许多存储系统(例如MySQL)提供慢查询日志帮助开发和运维人 员定位系统存在的慢操作。所谓慢查询日志就是系统在命令执行前后计 算每条命令的执行时间,当超过预设阀值,就将这条命令的相关信息 (例如:发生时间,耗时,命令的详细信息)记录下来,Redis也提供 了类似的功能。
一、慢查询日志
慢查询只记录命令执行时间,并不包括命令排队和网络传输时间。因此客户端执行命令的时间会大于命令实际执行时间。因为命令执 行排队机制,慢查询会导致其他命令级联阻塞,因此当客户端出现请求超时,需要检查该时间点是否有对应的慢查询,从而分析出是否为慢查询导致的命令级联阻塞。
二、慢查询的配置参数
Redis提供了slowlog-log-slower-than和slowlog-max-len配置来解决这 两个问题。
- 从字面意思就可以看出,slowlog-log-slower-than就是那个预 设阀值,它的单位是微秒(1秒=1000毫秒=1000000微秒),默认值是 10000,假如执行了一条“很慢”的命令(例如keys*),如果它的执行时 间超过了10000微秒,那么它将被记录在慢查询日志中。(提示:如果slowlog-log-slower-than=0会记录所有的命令,slowlog-log- slower-than<0对于任何命令都不会进行记录。)
- Redis使用了一个列表来存储慢 查询日志,slowlog-max-len就是列表的最大长度。一个新的命令满足慢 查询条件时被插入到这个列表中,当慢查询日志列表已处于其最大长度时,最早插入的一个命令将从列表中移出,例如slowlog-max-len设置为 5,当有第6条慢查询插入的话,那么队头的第一条数据就出列,第6条 慢查询就会入列。
三、如何配置慢查询日志
3.1 直接在配置文件redis.conf配置
################################## SLOW LOG ###################################
# The Redis Slow Log is a system to log queries that exceeded a specified
# execution time. The execution time does not include the I/O operations
# like talking with the client, sending the reply and so forth,
# but just the time needed to actually execute the command (this is the only
# stage of command execution where the thread is blocked and can not serve
# other requests in the meantime).
#
# You can configure the slow log with two parameters: one tells Redis
# what is the execution time, in microseconds, to exceed in order for the
# command to get logged, and the other parameter is the length of the
# slow log. When a new command is logged the oldest one is removed from the
# queue of logged commands.
# The following time is expressed in microseconds, so 1000000 is equivalent
# to one second. Note that a negative number disables the slow log, while
# a value of zero forces the logging of every command.
slowlog-log-slower-than 10000
# There is no limit to this length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
slowlog-max-len 1283.2 使用命令config set进行动态修改
config set slowlog-log-slower-than 20000
config set slowlog-max-len 1000
config rewrite提示:config rewrite 用于将配置持久化到本地配置文件。
四、慢查询日志查询
4.1 获取慢查询日志
通过命令slowlog get [n] 即可查询慢查询日志 (参数n可以指定条数)
127.0.0.1:6379> slowlog get
1) 1) (integer) 666
2) (integer) 1456786500
3) (integer) 11615
4)1) "BGREWRITEAOF"
2)
1) (integer) 665
2) (integer) 1456718400
3) (integer) 12006
4) 1) "SETEX"
2) "video_info_200"
3) "300"
4) "2"可以看到每个慢查询日志有4个属性组成,分别是慢查询日志的标 识id、发生时间戳、命令耗时、执行命令和参数。
4.2 获取慢查询日志列表当前的长度
通过命令slowlog len 即可获取慢查询日志列表当前的长度
127.0.0.1:6379> slowlog len
(integer) 454.3 慢查询日志重置
通过命令slowlog reset 即可对慢查询日志重置,实际就对日志列表做清理操作
127.0.0.1:6379> slowlog len
(integer) 45
127.0.0.1:6379> slowlog reset
OK
127.0.0.1:6379> slowlog len
(integer) 0总结
开发建议:
- slowlog-max-len配置建议:线上建议调大慢查询列表,记录慢查询 时Redis会对长命令做截断操作,并不会占用大量内存。增大慢查询列 表可以减缓慢查询被剔除的可能,例如线上可设置为1000以上。
- slowlog-log-slower-than配置建议:默认值超过10毫秒判定为慢查 询,需要根据Redis并发量调整该值。由于Redis采用单线程响应命令, 对于高流量的场景,如果命令执行时间在1毫秒以上,那么Redis最多可 支撑OPS不到1000。因此对于高OPS场景的Redis建议设置为1毫秒。
- ·由于慢查询日志是一个先进先出的队列,也就是说如果慢查询比 较多的情况下,可能会丢失部分慢查询命令,为了防止这种情况发生,可以定期执行slow get命令将慢查询日志持久化到其他存储中(例如 MySQL),然后可以制作可视化界面进行查询,
















