How AOF logs every write command, the three appendfsync strategies (always/everysec/no) and their durability-vs-latency trade-offs, AOF rewrite to prevent unbounded log growth, and the hybrid RDB+AOF preamble format.
P-2 — AOF: Append-Only File Mechanics and fsync Strategies
Who this module is for: RDB's data loss window (up to the last snapshot interval) is too large for your use case. You need Redis to survive a crash with minimal data loss. AOF (Append-Only File) is the answer — but its three fsync strategies have very different durability and performance characteristics that you must understand before enabling it in production.
What AOF Is
AOF (Append-Only File) records every write command that Redis executes. When Redis restarts, it replays the AOF log to reconstruct the dataset. The result is the same in-memory state as before the crash, minus the commands that were not yet written to disk.
Unlike RDB (which snapshots the entire dataset periodically), AOF logs commands continuously. The trade-off:
- More durable — data loss is bounded by how often the OS flushes the buffer to disk
- Larger file — grows with every write until rewritten
- Slower startup — must replay all commands vs loading a binary snapshot
Enabling AOF
At runtime:
CONFIG SET appendonly yes
Redis 7.0 introduced multi-part AOF: the AOF directory contains a base file (an RDB snapshot or an existing AOF) plus incremental AOF files. This makes AOF rewrite safer and more efficient. On Redis < 7.0, a single appendonly.aof file is used.
The Three fsync Strategies
This is the most important decision in AOF configuration. The appendfsync setting controls how often Redis calls fsync() — the system call that tells the OS to flush its write buffer to durable storage.
Without fsync(), data written to disk may still be in the OS's page cache (RAM) and can be lost if the machine loses power before the OS writes it to storage. fsync() forces a flush.
appendfsync always
appendfsync always
Redis calls fsync() after every write command. Every SET, HSET, LPUSH — every command — is flushed to disk before returning OK to the client.
Durability: Maximum. If Redis crashes after returning OK, the command is on disk.
Performance: ~100–200 writes/second (bottlenecked by disk fsync latency). Unusable for write-heavy workloads on spinning disks; borderline on SSDs.
Use case: Financial transactions where every write must survive. Rarely appropriate for most Redis use cases.
appendfsync everysec (default recommended)
appendfsync everysec
Redis calls fsync() once per second in a background thread. Write commands are appended to the OS buffer immediately (fast), and the OS flushes to disk every second.
Durability: You can lose up to 1 second of writes. In practice, the loss window is usually < 1 second because the background fsync runs independently of write traffic.
Performance: Excellent — writes are buffered and only 1 fsync per second. Handles hundreds of thousands of writes/second on modern hardware.
Use case: Most production Redis deployments. The right default when you need durability but cannot sacrifice write throughput.
appendfsync no
appendfsync no
Redis never calls fsync() — it lets the OS decide when to flush write buffers to disk. On Linux, the OS typically flushes every 30 seconds, but this is not guaranteed.
Durability: Worst — you can lose up to 30+ seconds of writes on a system crash.
Performance: Highest — no fsync overhead at all.
Use case: When Redis is purely a cache (you can regenerate all data) and you want AOF for replay capability but are not concerned about data loss.
What Happens on Write
When a client sends SET mykey "value":
- Redis executes the command in memory
- Redis appends the command in RESP format to the AOF write buffer (in-process memory)
- At the next
appendfsyncopportunity, the buffer is flushed to the OS and optionallyfsync()'d - Redis returns
OKto the client
The AOF write buffer is in Redis's process memory. If the process crashes before the buffer is flushed to the OS, the command is lost regardless of appendfsync setting. The buffer flush to the OS happens via write() — this happens on every command for always and everysec, and the fsync() determines whether the OS flushes to storage.
AOF Rewrite
Sign in to keep reading
The rest of this module is free — sign in with Google to unlock it and track your progress.
Sign in & RegisterDiscussion
0Join the discussion