Explain RDB vs. AOF persistence in Redis. What are the trade-offs, and which would you use in production?
Answer
RDB writes a compact point-in-time snapshot of the whole dataset to disk at intervals, while AOF (Append Only File) logs every write command so the dataset can be rebuilt by replaying it. RDB gives fast restarts and small files but can lose the writes since the last snapshot; AOF with the default appendfsync everysec loses at most about one second of writes but produces larger files. Most production setups that need durability enable both.
How RDB snapshots work
BGSAVE (or a save rule such as save 3600 1 300 100 60 10000) makes Redis fork() a child process.
The child writes the dataset to dump.rdb while the parent keeps serving clients. The OS uses copy-on-write, so only pages modified during the snapshot are duplicated.
On a write-heavy instance that copy-on-write can approach the dataset size, which is why you leave memory headroom.
SAVE does the same thing synchronously and blocks the server; avoid it in production.
How AOF works
Every write is appended to the AOF buffer and flushed according to appendfsync:
always: fsync on every write; safest, slowest.
everysec (default): fsync once per second in a background thread; at most about 1 second of loss.
no: let the OS decide (typically up to ~30 seconds on Linux).
The log grows forever, so Redis rewrites it in the background (BGREWRITEAOF, or automatically via auto-aof-rewrite-percentage and auto-aof-rewrite-min-size).
Since Redis 7, AOF is multi-part: a base file (RDB format by default, thanks to aof-use-rdb-preamble yes) plus incremental files, tracked by a manifest in appenddirname. Rewrites no longer need to buffer and merge writes in memory.
Side-by-side comparison
Aspect
RDB
AOF (everysec)
Data loss on crash
Since last snapshot (minutes)
About 1 second
File size
Compact binary
Larger, needs rewrites
Restart time
Fast
Slower for large logs (faster with RDB preamble)
Runtime cost
Periodic fork
Continuous appends plus periodic rewrite fork
Backups
Easy: copy one file
Copy the whole AOF directory
What to use in production
Pure cache that can be rebuilt from the database: persistence can be off or RDB only, to warm the cache faster after restart.
Sessions, rate limit counters, queues: AOF everysec, often with RDB as well for backups.
Anything where Redis is the only copy of money-like data: question the design. Even appendfsync always does not protect you from losing writes that were acknowledged by a primary but not yet replicated before failover.
Gotchas
Fork latency grows with memory size; on large instances check latest_fork_usec in INFO stats.
Transparent Huge Pages make copy-on-write expensive; Redis warns you to disable THP.
If both are enabled, Redis loads the AOF on startup because it is the more complete record.
Default config in recent versions has RDB save rules on and appendonly no.
How a BGSAVE snapshot is produced. The parent keeps serving traffic while the forked child writes the snapshot.
BGSAVE or save rule fires: Triggered by time and change thresholds.
Parent calls fork(): Child shares memory pages with the parent.
Child serialises dataset: Writes a temporary RDB file.
Copy-on-write for changed pages: Only pages the parent modifies get copied.
Atomic rename to dump.rdb: Old snapshot is replaced in one step.
Code examples
A typical durable configuration
# redis.conf
# RDB: snapshot if 1 change in 1h, 100 in 5m, or 10000 in 1m
save 3600 1 300 100 60 10000
dbfilename dump.rdb
# AOF
appendonly yes
appendfsync everysec
aof-use-rdb-preamble yes # base file in compact RDB format
auto-aof-rewrite-percentage 100 # rewrite when AOF doubles
auto-aof-rewrite-min-size 64mb
# Check persistence health
redis-cli INFO persistence | grep -E 'rdb_last_bgsave_status|aof_enabled|aof_last_write_status'
This combines periodic snapshots for backups with an everysec AOF for durability, and the INFO check confirms the last save and AOF write succeeded.