Difficulty: Advanced
Compare the three appendfsync settings for Redis AOF persistence. How much data can you lose with each, and which would you choose?
appendfsync controls how often Redis forces the append-only file from the OS page cache onto disk with fsync. always fsyncs on every write batch and is the safest and slowest, everysec (the default) fsyncs once per second from a background thread and can lose about one second of writes, and no leaves flushing to the operating system, which on Linux usually means up to around 30 seconds of exposure.
SET order:991 paid.write()s the buffer to the AOF file. The data is now in the kernel page cache, safe from a Redis crash but not from a power cut or kernel panic.fsync() pushes the page cache to the physical disk. The setting decides when step 4 happens.| Setting | When fsync runs | Possible loss on power failure | Performance |
|---|---|---|---|
always | After each event-loop write batch, before replies are sent | Practically none for acknowledged writes | Slowest; bound by disk fsync latency |
everysec | Once per second in a background thread | About 1 second, slightly more if the disk stalls | Close to no for most workloads |
no | Whenever the OS decides | Whatever was not yet flushed, often up to ~30 seconds on Linux | Fastest |
With everysec, if the background fsync is still running when the next one is due, Redis waits; if fsync takes more than about two seconds, Redis blocks writes until it finishes. That is why slow or shared disks show up as latency spikes in INFO persistence (aof_delayed_fsync).
always uses group commit: writes from many clients in the same event-loop iteration share one fsync, but throughput still depends heavily on the disk. On cloud volumes with millisecond fsync latency, it can cut write throughput dramatically.
appendonly yes turns AOF on (off by default; RDB snapshots are on by default).no-appendfsync-on-rewrite yes skips fsync while a rewrite or RDB save is running to avoid disk contention, at the cost of more potential loss during that window.aof-use-rdb-preamble yes (default) makes rewrites start with a compact RDB snapshot.appenddirname (default appendonlydir).everysec. Losing a second of writes after a machine crash is acceptable, and it is the default for good reason.always is possible, but Redis is rarely the system of record for money. Keep payments in a database with transactions and use Redis for derived or fast-path state.Also remember replication is asynchronous; WAIT and WAITAOF (7.2+) let a client block until writes reach replicas or are fsynced, for stronger guarantees per command.
# redis.conf
appendonly yes
appendfsync everysec # always | everysec | no
appenddirname "appendonlydir" # multi-part AOF directory (7.0+)
aof-use-rdb-preamble yes
no-appendfsync-on-rewrite no # yes trades durability for lower latency during rewrites
# Change at runtime and verify
# redis-cli CONFIG SET appendfsync always
# redis-cli INFO persistence | grep -E 'aof_enabled|aof_delayed_fsync|aof_last_write_status'
everysec is the usual production choice. Watch aof_delayed_fsync to spot a disk that cannot keep up.
AOF, appendfsync, fsync, Durability, Multi-part AOF