Redis 7 latency tuning: appendfsync, THP, LATENCY DOCTOR, SLOWLOG
Find Redis latency spikes with SLOWLOG and the latency monitor: slow command, fork or AOF write. Then check appendfsync and THP, and roll back.
When Redis replies spike to hundreds of milliseconds now and then, the cause is usually one of three: a slow command (single-threaded, so every other request waits), the fork for persistence, or AOF disk writes. Before changing settings, measure which one with SLOWLOG and the latency monitor (latency-monitor-threshold). The latency monitor is off by default.
[1] Check the current values. The official image starts without a config file, so these are the built-in defaults.
$ redis-cli CONFIG GET appendonly appendfsync save latency-monitor-threshold slowlog-log-slower-than slowlog-max-len disable-thp no-appendfsync-on-rewriteno-appendfsync-on-rewritenoappendfsynceverysecappendonlynolatency-monitor-threshold0save3600 1 300 100 60 10000slowlog-max-len128slowlog-log-slower-than10000disable-thpyes| Setting | Default (7.4) | Meaning |
|---|---|---|
latency-monitor-threshold | 0 | ms; 0 turns the latency monitor off |
slowlog-log-slower-than | 10000 | Logs commands slower than this many microseconds (10ms) |
slowlog-max-len | 128 | Entries kept in the slow log |
appendonly / appendfsync | no / everysec | With AOF on, fsync once per second |
save | 3600 1 300 100 60 10000 | RDB snapshot rules (each one forks) |
disable-thp | yes | If THP is always, try to turn it off for the Redis process only |
[2] Turn on the latency monitor and see what spikes. Set the threshold to the latency you consider a problem (the docs use 100ms as an example). Below, DEBUG SLEEP 0.3 blocks the test server for 300ms. DEBUG is blocked by default; only the test server was started with --enable-debug-command local. Do not open it on production.
$ redis-cli CONFIG SET latency-monitor-threshold 100OK$ redis-cli DEBUG SLEEP 0.3OK$ redis-cli LATENCY LATESTcommand1793099731300300The four lines are event name, last occurrence (Unix time), last value (ms) and maximum (ms). The event name points to the cause:
| Event | Points to |
|---|---|
command, fast-command | Command execution itself is slow → step 3 |
fork | fork(2) for RDB or AOF rewrite → step 4 |
aof-write*, aof-fsync-always | AOF write or fsync → step 5 |
expire-cycle, eviction-* | Expire or eviction cycles |
LATENCY DOCTOR turns the same data into sentences:
$ redis-cli LATENCY DOCTOR…1. command: 1 latency spikes (average 300ms, mean deviation 0ms, period 1.00 sec). Worst all time event 300ms.…- 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.…[3] If command spikes, find the command in the slow log. Times are in microseconds.
$ redis-cli SLOWLOG GET 101793099731300151DEBUGSLEEP0.3127.0.0.1:53152 In order: entry ID, timestamp, execution time (300151µs), command and arguments, client address, client name (blank without CLIENT SETNAME). Common production culprits are KEYS, SMEMBERS/HGETALL/LRANGE 0 -1 on large keys, and DEL of a big key. Replace KEYS with SCAN and big-key deletes with UNLINK. The slow log measures execution only, not client I/O; if it is empty but the app sees slowness, look at the network and round trips. Clear it with SLOWLOG RESET when done.
[4] If fork spikes, check fork time and THP. Fork runs in the main thread, so requests stop meanwhile. The last fork's duration is latest_fork_usec:
$ redis-cli BGSAVEBackground saving started$ redis-cli INFO persistence | grep -E '^(rdb_last_bgsave_status|rdb_last_cow_size|aof_enabled|aof_last_write_status|aof_delayed_fsync)'rdb_last_bgsave_status:okrdb_last_cow_size:581632aof_enabled:0aof_last_write_status:ok$ redis-cli INFO stats | grep -E '^latest_fork_usec'latest_fork_usec:777Per the docs, fork time grows with memory size (the page table) and can be much larger on some virtualized hosts. If spikes line up with snapshots, reduce the save rules (for example CONFIG SET save "3600 1") to fork less often. Note the original value from step 1 and put it back the same way to roll back.
With Transparent Huge Pages enabled, every write after a fork copies a whole huge page, raising both latency and memory. The state is readable from inside a container (it is the host kernel's value):
$ cat /sys/kernel/mm/transparent_hugepage/enabledalways [madvise] never[madvise] or [never] is fine. With [always], Redis 7 tries to turn THP off for its own process (disable-thp yes, the default). The latency docs say to set the kernel to never (echo never > /sys/kernel/mm/transparent_hugepage/enabled) and restart Redis. That changes the whole host and resets on reboot, so make it permanent the distribution's way (kernel boot parameter or similar) and check other services on the host first. The startup WARNING Memory overcommit must be enabled! is a separate problem (background save failures), covered in Fix Redis MISCONF: unable to persist to disk.
[5] With AOF, appendfsync trades latency for durability. It changes at runtime. Tune it only on a server that already uses AOF — on a default (appendonly no) server, CONFIG SET appendonly yes immediately forks an AOF rewrite and writes to disk, and AOF stays on. This is a test server, switched on and then back off with the last command:
$ redis-cli CONFIG SET appendonly yesOK$ redis-cli CONFIG GET appendfsyncappendfsynceverysec$ redis-cli CONFIG SET appendfsync alwaysOK$ redis-cli CONFIG SET appendfsync everysecOK$ redis-cli CONFIG SET appendonly noOK| Value | Latency | Writes you can lose |
|---|---|---|
always | Highest: fsync before replying | Almost none |
everysec (default) | A background thread fsyncs each second; on a slow disk writes wait up to 2 seconds, then block | About 1 second |
no | fsync left to the kernel | Until the kernel flushes |
On everysec, a lagging disk logs Asynchronous AOF fsync is taking too long (disk is busy?) and raises aof_delayed_fsync in INFO persistence. If spikes happen only during RDB saves or AOF rewrites, no-appendfsync-on-rewrite yes pauses fsync during them; as redis.conf explains, durability then equals appendfsync no for that time, so enable it only when the latency problem is confirmed.
[6] Roll back and persist. CONFIG SET is lost on restart. A server started with a config file can write it back with CONFIG REWRITE — this writes every current CONFIG value into the file permanently, so first revert anything you changed for testing, such as appendonly. One started without (the official image default) needs its command line or mounted config file changed.
$ redis-cli CONFIG SET latency-monitor-threshold 0OK$ redis-cli LATENCY RESET1$ redis-cli CONFIG REWRITEERR The server is running without a config fileLATENCY RESET returns the number of event series reset. The latency monitor is cheap enough to leave on with a threshold; 0 is the default. For memory limits and eviction, see Fix Redis OOM command not allowed when used memory > 'maxmemory'.
Official references: Diagnosing latency issues — fork, THP, AOF, slow commands · Latency monitoring · SLOWLOG GET · Persistence · redis.conf 7.4 — disable-thp, no-appendfsync-on-rewrite, enable-debug-command