bigywater.tech
Redis · 97

LATENCY DOCTOR

LATENCY DOCTOR 是 Redis 自带的「延迟医生」:它读取延迟监控记录下来的事件,输出一份带建议的文字报告。这篇不评价建议好不好,只做一件事:让它在受控条件下报告,对照我制造的事件,看它的输出与什么对应、什么情况下不会出现。

2 类事件
command 与 aof-fsync-always(阈值 5 ms)
原文输出
LATENCY DOCTOR 的报告和建议全文在下面
阈值决定
延迟监控只记录超过 latency-monitor-threshold 的事件

完整的报告

实测 · 本机执行输出
Dave, I have observed latency spikes in this Redis instance. You don't mind talking about it, do you Dave?

1. command: 2 latency spikes (average 55ms, mean deviation 44ms, period 2.50 sec). Worst all time event 100ms.
2. aof-fsync-always: 1 latency spikes (average 9ms, mean deviation 0ms, period 6.00 sec). Worst all time event 9ms.

I have a few advices for you:

- Your current Slow Log configuration only logs events that are slower than your configured latency monitor threshold. Please use 'CONFIG SET slowlog-log-slower-than 5000'.
- Check your Slow Log to understand what are the commands you are running which are too slow to execute. Please check https://redis.io/commands/slowlog for more information.
- Your fsync policy is set to 'always'. It is very hard to get good performances with such a setup, if possible try to relax the fsync policy to 'onesec'.
- Deleting, expiring or evicting (because of maxmemory policy) large objects is a blocking operation. If you have very large objects that are often deleted, expired, or evicted, try to fragment those objects into multiple smaller objects.

报告分两块:先是「观察到的事件」(事件名、尖峰次数、平均值、平均偏差、周期、历史最大值),然后是根据这些事件生成的「建议」。命令类事件会提示查慢日志并把慢日志阈值与监控阈值对齐(CONFIG SET slowlog-log-slower-than 5000);aof-fsync-always 会提示放宽 fsync 策略(报告里把 everysec 写成了 onesec);最后一条是关于大对象删除的通用提示。这些建议是服务器的通用规则,不是根据你的具体负载算出来的。

事件怎么来的

实测 · 本机执行输出
1) 1) "command"
   2) (integer) 1790706093
   3) (integer) 11
   4) (integer) 100
2) 1) "aof-fsync-always"
   2) (integer) 1790706090
   3) (integer) 9
   4) (integer) 9

我制造了 DEBUG SLEEP、KEYS、BGSAVE、BGREWRITEAOF 和带 fsync 的 SET,LATENCY LATEST 里只有两类事件:command(慢命令)与 aof-fsync-always。fork 的耗时(这台机器上约几毫秒)没有超过 5 ms 阈值,所以没有 fork 事件——第 66 篇量过 fork 的停顿,这里能不能记录取决于阈值。LATEST / HISTORY / GRAPH 的细节见第 98 篇。

阈值

实测 · 本机执行输出
  阈值 100 ms,DEBUG SLEEP 0.05 之后 LATENCY LATEST = []
  再 DEBUG SLEEP 0.15 之后 LATENCY LATEST = [('command', 151)]

把 latency-monitor-threshold 调到 100 ms:50 ms 的阻塞不被记录,150 ms 的才被记录(151 ms)。0 表示关闭。所以「LATENCY DOCTOR 说没问题」可能只是「没有超过阈值的事件」。

分步演示:一份延迟报告

LATENCY DOCTOR
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 / Redis 8.10.2 独立实例(本机回环端口,save 关闭),redis-cli 风格输出由自写的 RESP 客户端打印;计时用 time.perf_counter,数字每次运行会有小幅抖动。appendonly yes + appendfsync always;延迟事件用 DEBUG SLEEP 等命令制造
    • 没有验证:在 Linux 上 fork / transparent-huge-page 等事件的报告、报告里 "Dave" 那句开场白的出处(只是原文)、更多事件类型(expire-cycle、eviction-cycle 等)
    京ICP备2026067502号-1京公网安备11010502063487号