Redis persistence overview (RDB, AOF) - Time & Space Complexity
When Redis saves data to disk, it uses methods like RDB snapshots or AOF logs. We want to understand how the time it takes to save data changes as the amount of data grows.
How does saving more data affect the time Redis spends writing to disk?
Analyze the time complexity of saving data using RDB snapshots and AOF appends.
# RDB snapshot save
SAVE
# AOF append operation
APPEND key value
# AOF rewrite triggered
BGREWRITEAOF
This code shows Redis commands for saving data: RDB saves a full snapshot, AOF appends commands, and AOF rewrite compacts the log.
Look at what repeats when saving data.
- Primary operation: Writing each key-value pair to disk.
- How many times: For RDB, all keys are saved at once during snapshot. For AOF, each write command is appended individually.
As the number of keys grows, the time to save changes.
| Input Size (n keys) | Approx. Operations |
|---|---|
| 10 | Writes 10 keys to disk |
| 100 | Writes 100 keys to disk |
| 1000 | Writes 1000 keys to disk |
Pattern observation: The time to save grows roughly in direct proportion to the number of keys.
Time Complexity: O(n)
This means saving data takes time that grows linearly with how many keys Redis holds.
[X] Wrong: "Saving data time stays the same no matter how much data there is."
[OK] Correct: Saving means writing each key to disk, so more keys take more time.
Understanding how Redis persistence time grows helps you explain performance in real systems. It shows you how data size affects saving speed, a useful skill for database work.
"What if Redis used incremental snapshots instead of full snapshots? How would the time complexity change?"
Practice
Solution
Step 1: Understand Redis persistence goal
Redis persistence is designed to keep data safe even if Redis restarts or crashes.Step 2: Match purpose with options
Only To save data so it is not lost after a restart talks about saving data across restarts, which matches the goal.Final Answer:
To save data so it is not lost after a restart -> Option BQuick Check:
Persistence means saving data = A [OK]
- Confusing persistence with speed optimization
- Thinking persistence compresses data
- Assuming persistence encrypts data
Solution
Step 1: Recall persistence methods
RDB saves snapshots of the dataset at set intervals, while AOF logs every write operation.Step 2: Identify correct method for snapshots
Only RDB matches the description of saving snapshots periodically.Final Answer:
RDB (Redis Database) -> Option DQuick Check:
Snapshots = RDB [OK]
- Mixing AOF with snapshot saving
- Choosing both methods as snapshot savers
- Thinking AOF saves snapshots
Solution
Step 1: Understand recovery priority
When both RDB and AOF are enabled, Redis prefers AOF for recovery because it has more recent data.Step 2: Analyze options
Redis will use the AOF log if available, otherwise the RDB snapshot correctly states Redis uses AOF if available, else falls back to RDB.Final Answer:
Redis will use the AOF log if available, otherwise the RDB snapshot -> Option AQuick Check:
AOF if available else RDB = B [OK]
- Assuming Redis uses only RDB always
- Thinking Redis cannot recover with both enabled
- Believing AOF is ignored if RDB exists
Solution
Step 1: Check AOF file behavior
If AOF file size does not increase after writes, it suggests AOF is not active or enabled.Step 2: Identify cause from options
AOF persistence is disabled in the config states AOF persistence is disabled, which explains no file growth.Final Answer:
AOF persistence is disabled in the config -> Option CQuick Check:
No AOF growth means AOF disabled = C [OK]
- Assuming Redis does not support AOF
- Thinking AOF rewrite blocks all writes
- Confusing RDB usage with AOF file size
Solution
Step 1: Understand trade-offs
RDB snapshots are fast but risk losing data between snapshots. AOF logs every write but can slow restarts.Step 2: Analyze combined setup
Enabling both with AOF fsync every second balances minimal data loss and reasonable restart speed.Step 3: Evaluate other options
Only RDB risks data loss; only AOF can slow restarts; disabling persistence risks total data loss.Final Answer:
Enable both RDB snapshots and AOF with fsync every second -> Option AQuick Check:
Best mix for safety and speed = D [OK]
- Choosing only RDB and ignoring data loss risk
- Choosing only AOF and ignoring restart speed
- Disabling persistence entirely
Ready to prove it?
Pass the test and this pattern moves unknown
