What is the difference between Redis persistence with RDB and AOF?
Compare Redis RDB snapshots and AOF logging: durability, data loss, restart speed and fsync trade-offs, plus why teams often run both together.
Expected Interview Answer
RDB takes point-in-time binary snapshots of the whole dataset at intervals, while AOF logs every write command so the dataset can be rebuilt by replaying the log; RDB favors fast restarts and compact files, AOF favors durability with less data loss.
RDB forks the process and dumps memory to a compact .rdb file on a schedule or manually with SAVE/BGSAVE, so a crash between snapshots loses recent writes. AOF appends each mutating command to a log and fsyncs it (default everysec), giving at most about one second of data loss, and periodically rewrites the log to keep it compact. Many production setups enable both: AOF for durability plus RDB snapshots for fast recovery and backups.
- RDB: compact single-file backups that are easy to copy and archive
- RDB: faster restart because loading a snapshot beats replaying a log
- AOF: minimal data loss with appendfsync everysec or always
- AOF: human-readable, replayable command log that is easy to inspect
- Both together give strong durability and quick recovery
AI Mentor Explanation
RDB is like photographing the full scoreboard at the end of every over: compact and quick to read back, but any runs scored between two photos are lost if the power cuts. AOF is like a scorer writing down every single delivery as it happens, so the entire innings can be reconstructed ball by ball even after a blackout.
Step-by-Step Explanation
Step 1
Enable RDB snapshots
Configure save rules (e.g. save 900 1) so Redis forks and writes a .rdb dump when enough keys change.
Step 2
Enable AOF logging
Set appendonly yes so every write command is appended to the AOF file for replay on restart.
Step 3
Choose fsync policy
Set appendfsync to everysec (balanced), always (safest, slower), or no (fastest, least durable).
Step 4
Rewrite the AOF
Redis periodically runs BGREWRITEAOF to compact the log into the shortest set of commands that recreate the data.
Step 5
Recover on restart
Redis loads AOF if enabled (more complete), otherwise the RDB snapshot, to rebuild the in-memory dataset.
What Interviewer Expects
- Clear distinction between snapshotting and command logging
- Understanding of data-loss windows for each mode
- Knowledge of appendfsync policies and their trade-offs
- Awareness that both can run together in production
- Reasoning about restart speed versus durability
Common Mistakes
- Claiming AOF is always slower or always faster without context
- Thinking you must pick only one of RDB or AOF
- Ignoring the fork memory cost of BGSAVE on large datasets
- Assuming RDB gives zero data loss
- Confusing AOF rewrite with disabling persistence
Best Answer (HR Friendly)
“Redis can save its data two ways: RDB takes occasional full snapshots that are small and load fast, while AOF keeps a running log of every change so almost nothing is lost in a crash. Teams often use both to balance quick recovery with strong durability.”
Code Example
# RDB: snapshot when 1+ key changes in 900s, 10+ in 300s
save 900 1
save 300 10
dbfilename dump.rdb
# AOF: log every write and fsync once per second
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
# Trigger a manual AOF rewrite (compaction)
# redis-cli BGREWRITEAOFFollow-up Questions
- What is the data-loss window with appendfsync everysec versus always?
- How does BGSAVE use fork and copy-on-write memory?
- Why might a large dataset make RDB snapshots expensive?
- How does AOF rewrite keep the log file small?
- When would you run both RDB and AOF at the same time?
MCQ Practice
1. Which persistence mode logs every write command for replay on restart?
AOF (Append Only File) records each mutating command and replays them to rebuild the dataset, unlike RDB which stores snapshots.
2. What is the typical maximum data loss with appendfsync everysec?
With everysec, Redis fsyncs the AOF once per second, so a crash loses at most roughly one second of writes.
3. Which mode generally produces the most compact backup file?
RDB stores a compact binary snapshot, which is usually smaller than an AOF command log and faster to load on restart.
Flash Cards
What does RDB stand for and store? — Redis Database file: a compact point-in-time binary snapshot of the whole dataset.
What does AOF store? — Append Only File: a log of every write command, replayed on restart to rebuild the data.
Default appendfsync policy? — everysec — fsync once per second, at most ~1s of data loss.
Why enable both RDB and AOF? — AOF gives strong durability; RDB gives fast restarts and compact backups.
What does BGREWRITEAOF do? — Compacts the AOF into the shortest command set that recreates the current dataset.
Continue Learning
Related Interview Questions
What is the difference between Redis as a cache and as a primary datastore?
medium
How does Redis handle key expiration and TTL?
medium
Why can a Redis background save cause a memory spike and a latency pause, and how do you control it?
hard
What happens between starting a Redis process and it serving traffic, and how do you make restarts safe?
hard