Combined RDB + AOF in Redis - Time & Space Complexity
When Redis saves data, it can use two methods together: RDB snapshots and AOF logs.
We want to understand how the time to save data grows as the data size grows.
Analyze the time complexity of this Redis saving process using both RDB and AOF.
SAVE # creates a snapshot (RDB)
BGREWRITEAOF # rewrites the append-only file (AOF)
This code triggers a full snapshot and a background rewrite of the AOF file to save data safely.
Look at what repeats when saving data.
- Primary operation: Writing all data keys to disk during snapshot and rewriting AOF.
- How many times: Each key is processed once per save or rewrite.
As the number of keys grows, the time to save grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 key writes |
| 100 | About 100 key writes |
| 1000 | About 1000 key writes |
Pattern observation: The time grows roughly in direct proportion to the number of keys.
Time Complexity: O(n)
This means the saving time grows linearly with the amount of data stored.
[X] Wrong: "Saving data with RDB and AOF together is always instant regardless of data size."
[OK] Correct: Both methods write all data keys, so saving time increases as data grows.
Understanding how Redis saves data helps you explain performance impacts clearly and shows you know how data size affects operations.
"What if Redis used only AOF without RDB snapshots? How would the time complexity change?"
Practice
Solution
Step 1: Understand RDB persistence
RDB creates snapshots periodically, allowing Redis to start quickly by loading a compact file.Step 2: Understand AOF persistence
AOF logs every write operation, ensuring data durability by replaying commands after a restart.Final Answer:
It combines fast startup with high data durability. -> Option BQuick Check:
Combined RDB + AOF = Fast startup + durability [OK]
- Thinking combined persistence disables data saving
- Confusing RDB with AOF roles
- Assuming combined persistence reduces memory usage
Solution
Step 1: Identify RDB snapshot setting
The 'save 900 1' means save snapshot if at least 1 change in 900 seconds.Step 2: Identify AOF setting
'appendonly yes' enables AOF persistence logging every write.Final Answer:
appendonly yes and save 900 1 -> Option DQuick Check:
Enable both RDB and AOF with save + appendonly yes [OK]
- Setting appendonly no disables AOF
- Empty save disables RDB snapshots
- Confusing syntax for save option
Solution
Step 1: Understand RDB snapshot timing
RDB snapshots happen periodically, so recent writes after last snapshot are not saved there.Step 2: Understand AOF recovery
AOF logs every write, so Redis replays AOF to restore all writes including recent ones.Final Answer:
Data is restored from AOF log, including recent writes after last snapshot. -> Option AQuick Check:
AOF recovers recent writes missed by RDB [OK]
- Assuming only RDB restores data
- Thinking Redis starts empty after crash
- Believing Redis merges snapshot and AOF causing duplicates
Solution
Step 1: Check AOF role in recovery
AOF logs all writes and is used to restore recent data after restart.Step 2: Identify cause of missing recent data
If AOF is corrupted or disabled, recent writes after last snapshot are lost.Final Answer:
AOF file was corrupted or disabled during restart. -> Option CQuick Check:
Missing recent data usually means AOF problem [OK]
- Assuming Redis can't combine RDB and AOF
- Ignoring AOF file corruption possibility
- Blaming wrong port for data loss
Solution
Step 1: Understand RDB snapshot interval
Snapshots every 15 minutes reduce blocking frequency and provide periodic backups.Step 2: Understand AOF 'everysec' fsync
'everysec' fsync writes AOF to disk every second asynchronously, balancing durability and performance.Step 3: Combine effects
This combination offers fast recovery from snapshots and durability from AOF with low performance cost.Final Answer:
This setup balances fast recovery and durability with minimal performance impact. -> Option AQuick Check:
RDB + AOF everysec = balanced durability and speed [OK]
- Thinking 'everysec' fsync is synchronous after every command
- Assuming RDB snapshots block Redis for long every time
- Believing this setup disables AOF durability
Ready to prove it?
Pass the test and this pattern moves unknown
