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?"