AOF Sync Options

Difficulty: Advanced

Question

Compare the three appendfsync settings for Redis AOF persistence. How much data can you lose with each, and which would you choose?

Answer

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.

What actually happens on a write

  1. A client runs SET order:991 paid.
  2. Redis applies it in memory and appends the command to an in-memory AOF buffer.
  3. Before the event loop goes back to waiting for clients, Redis 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.
  4. fsync() pushes the page cache to the physical disk. The setting decides when step 4 happens.

The three options compared

SettingWhen fsync runsPossible loss on power failurePerformance
alwaysAfter each event-loop write batch, before replies are sentPractically none for acknowledged writesSlowest; bound by disk fsync latency
everysecOnce per second in a background threadAbout 1 second, slightly more if the disk stallsClose to no for most workloads
noWhenever the OS decidesWhatever was not yet flushed, often up to ~30 seconds on LinuxFastest

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.

Related settings

  • 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.
  • Since Redis 7.0, AOF is multi-part: a base file, incremental files and a manifest inside appenddirname (default appendonlydir).

Which to choose

  • Cache only: often no AOF at all, or RDB only.
  • Sessions, queues, counters: everysec. Losing a second of writes after a machine crash is acceptable, and it is the default for good reason.
  • Data you truly cannot lose: 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.

Where a write lives on its way to disk. appendfsync decides when data moves from the page cache to the physical disk.
  1. Redis memory (dataset): Lost if the Redis process crashes.
  2. AOF buffer in Redis: Written out every event-loop cycle.
  3. Kernel page cache: Survives a Redis crash, not a power cut.
  4. Physical disk: Reached on fsync: always, every second, or OS decides.

Code examples

AOF configuration in redis.conf

# 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.

Key points

  • always: fsync per write batch, safest, slowest
  • everysec (default): background fsync each second, about 1 second of loss
  • no: OS flushes, often up to ~30 seconds of loss on Linux, fastest
  • A Redis crash alone does not lose data already written to the page cache; power loss does
  • Redis 7.0 uses multi-part AOF; WAITAOF (7.2+) can wait for fsync per command

Common follow-up questions

  • What is an AOF rewrite and why is it needed?
  • When would you use RDB and AOF together?
  • What is the difference between a process crash and a power failure for durability?
  • How does WAITAOF differ from WAIT?

Interview tips

  • Explain write() vs fsync() and the page cache; it shows you understand why the options differ
  • Recommend everysec with a reason rather than just naming it
  • Point out that Redis should rarely be the only store for money-critical data

Concepts covered

AOF, appendfsync, fsync, Durability, Multi-part AOF

More Redis & Caching questions