AOF
The append-only file, fsync policies, rewrites, and multi-part AOF
The Append Only File logs every write command as it happens. On restart Redis replays the log to rebuild the dataset. It's the same idea as Postgres's WAL, with one important difference: Redis applies the write to memory first and logs it afterwards, and how soon that log reaches disk is your choice.
Turning it on
appendonly yes
appenddirname appendonlydir
appendfsync everysec
aof-use-rdb-preamble yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mbfsync policies: the durability dial
Writing to a file puts the data in the OS page cache. It's only safe from
a power loss or kernel crash after fsync. appendfsync controls how
often that happens:
| Policy | fsync | Can lose on power loss / host crash | Cost |
|---|---|---|---|
always | After every write batch | Almost nothing | Much slower writes; disk latency on every write |
everysec | Once a second, in a background thread | ~1 second of writes (up to ~2s under pressure) | Small |
no | When the OS decides (~30s on Linux) | Up to ~30 seconds | Minimal |
In theory, if only the Redis process crashes (not the host), even
no loses very little, because written data sits in the OS page cache
and gets flushed later. In practice that depends on the storage layer
underneath. The lab at the end of this lesson, run on this repo's Docker
setup (OrbStack on macOS), lost about a second of acknowledged writes
from a plain process kill under everysec, and nothing under always.
Measure on your own infrastructure rather than trusting the theory.
everysec is the default because it's the sweet spot: performance
close to no persistence, and at most about a second of loss. Note that
Redis acknowledges the write to the client before the fsync, so an
OK doesn't mean the write is on disk. Postgres, by contrast,
doesn't return from COMMIT until its WAL is flushed (by default).
Rewrites
The log grows forever: a counter incremented a million times is a
million INCR lines, when one SET would do. A rewrite compacts it.
Like BGSAVE, it forks a child that writes the current dataset as a new
base file, while the parent keeps logging new writes.
Since Redis 7, the AOF is multi-part, which makes this clean:
docker exec redis ls -la /data/appendonlydir
appendonly.aof.1.base.rdb # base: a snapshot (RDB format, thanks to aof-use-rdb-preamble)
appendonly.aof.1.incr.aof # incremental: commands since the base
appendonly.aof.manifest # which files make up the current AOF, in orderA rewrite produces a new base and a fresh incremental file, updates the
manifest atomically, and deletes the old files. With
aof-use-rdb-preamble yes, the base is in compact RDB format, so
restarts load the bulk of the data fast and replay only the recent
commands.
Automatic rewrites trigger when the AOF has grown 100% since the last rewrite and is at least 64 MB.
RDB and AOF together
This repo runs both, which is the common recommendation:
- AOF gives durability (about 1 second of loss).
- RDB gives compact backups and a fast fallback.
- On startup, if AOF is enabled, Redis loads from AOF because it's more complete.
| Setup | Loss on host crash | Restart speed | Use for |
|---|---|---|---|
| None | Everything | Instant (empty) | Pure caches you can rebuild |
| RDB only | Since last snapshot (minutes) | Fast | Caches; tolerant data |
AOF everysec (+ RDB) | ~1 second | Fast, with RDB preamble | The default for data you care about |
AOF always | ~Nothing | Fast | When losing a second matters, and you accept slower writes |
Persistence protects against restarts and crashes on one machine. It
doesn't protect against losing that machine's disk, and it says nothing
about failover. A replica promoted after the primary dies may be missing
the primary's last writes regardless of appendfsync, because
replication is asynchronous. See
Replication.
Lab: measure what you lose
# 1. write a counter as fast as possible in the background
docker exec -d redis sh -c 'while true; do redis-cli --user default --pass redis123 --no-auth-warning INCR app:durability >/dev/null; done'
# 2. after a few seconds, read the value and kill Redis hard (no clean shutdown)
docker exec redis redis-cli --user default --pass redis123 --no-auth-warning GET app:durability
docker kill -s KILL redis
# 3. start it and compare
docker start redis
docker exec redis redis-cli --user default --pass redis123 --no-auth-warning GET app:durabilityThe counter keeps climbing between your GET and the kill, so with no
loss the restored value would be higher than the one you read. When
this was run for the module, three everysec runs restored 35, 36, and
80 increments fewer than had already been acknowledged: writes that
clients saw succeed, then vanished. With appendfsync always set first
(CONFIG SET appendfsync always), the restored value was higher than
the one read, so nothing was lost.
Try both on your machine. CONFIG SET isn't saved to redis.conf, so
the restart puts everysec back. Then try CONFIG SET appendonly no
(RDB only) and see how much is lost then.