Persistence: RDB vs. AOF

Difficulty: Intermediate

Question

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

AspectRDBAOF (everysec)
Data loss on crashSince last snapshot (minutes)About 1 second
File sizeCompact binaryLarger, needs rewrites
Restart timeFastSlower for large logs (faster with RDB preamble)
Runtime costPeriodic forkContinuous appends plus periodic rewrite fork
BackupsEasy: copy one fileCopy 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.
  1. BGSAVE or save rule fires: Triggered by time and change thresholds.
  2. Parent calls fork(): Child shares memory pages with the parent.
  3. Child serialises dataset: Writes a temporary RDB file.
  4. Copy-on-write for changed pages: Only pages the parent modifies get copied.
  5. 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.

Key points

  • RDB = periodic forked snapshot; compact, fast restart, loses recent writes.
  • AOF = log of writes; appendfsync everysec loses about one second at most.
  • Fork uses copy-on-write; write-heavy instances need memory headroom during saves.
  • Redis 7+ uses multi-part AOF with an RDB-format base file and a manifest.
  • If both are on, Redis restores from AOF at startup.

Common follow-up questions

  • What is copy-on-write and why does it matter for BGSAVE?
  • What does an AOF rewrite do and why is it needed?
  • Can Redis lose acknowledged writes even with appendfsync always?
  • How would you back up a Redis instance without affecting latency?

Interview tips

  • Frame the answer as a durability vs performance trade-off with concrete loss windows.
  • Mention fork and copy-on-write; it separates people who have run Redis from people who have read about it.
  • Say that for a pure cache you may not need persistence at all; it shows judgement.

Concepts covered

RDB, AOF, appendfsync, Copy-on-Write, AOF Rewrite

More Redis & Caching questions