Learn Labs
Persistence

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 64mb

fsync 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:

PolicyfsyncCan lose on power loss / host crashCost
alwaysAfter every write batchAlmost nothingMuch slower writes; disk latency on every write
everysecOnce a second, in a background thread~1 second of writes (up to ~2s under pressure)Small
noWhen the OS decides (~30s on Linux)Up to ~30 secondsMinimal

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.

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 order

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

BGREWRITEAOFrewrite nowINFO persistenceaof_rewrite_in_progress, aof_last_bgrewrite_status, aof_current_sizeredis-check-aof --fix <file>repair a truncated AOF (after a crash mid-write)

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.
SetupLoss on host crashRestart speedUse for
NoneEverythingInstant (empty)Pure caches you can rebuild
RDB onlySince last snapshot (minutes)FastCaches; tolerant data
AOF everysec (+ RDB)~1 secondFast, with RDB preambleThe default for data you care about
AOF always~NothingFastWhen losing a second matters, and you accept slower writes

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:durability

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

On this page