Persistence and performance trade-off in Redis - Time & Space Complexity
When Redis saves data to disk, it can slow down because writing takes time.
We want to see how this saving affects how long commands take as data grows.
Analyze the time complexity of this Redis persistence example.
SET key value
SAVE
GET key
This code sets a value, saves all data to disk, then gets the value back.
Look for repeated or costly steps.
- Primary operation: The SAVE command writes all data to disk.
- How many times: SAVE writes every key-value pair once per call.
As data grows, saving takes longer because more data is written.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Writes 10 items to disk |
| 100 | Writes 100 items to disk |
| 1000 | Writes 1000 items to disk |
Pattern observation: The time to save grows roughly in direct proportion to data size.
Time Complexity: O(n)
This means saving data takes longer as you have more data, growing in a straight line.
[X] Wrong: "Saving data is always fast and does not depend on data size."
[OK] Correct: Saving writes all data to disk, so more data means more work and slower saving.
Understanding how saving data affects speed helps you explain trade-offs in real systems.
"What if Redis used incremental saving instead of full saving? How would the time complexity change?"
Practice
Solution
Step 1: Understand Redis persistence options
Redis offers RDB snapshots and AOF logging for persistence.Step 2: Identify method saving data at intervals
RDB snapshots save data periodically, while AOF logs every write.Final Answer:
RDB snapshots -> Option CQuick Check:
Persistence by snapshots = RDB snapshots [OK]
- Confusing AOF with snapshots
- Thinking no persistence saves data
- Assuming in-memory is persistent
Solution
Step 1: Identify AOF enabling option
The option to enable AOF is 'appendonly yes'.Step 2: Differentiate from snapshot options
'save 900 1' relates to RDB snapshots, not AOF.Final Answer:
appendonly yes -> Option AQuick Check:
Enable AOF = appendonly yes [OK]
- Using 'appendonly no' disables AOF
- Confusing 'save' with AOF setting
- Misreading unrelated config options
Solution
Step 1: Understand RDB snapshot interval
Snapshots save every 60 seconds, so up to 60 seconds loss if using RDB only.Step 2: Consider AOF 'appendfsync everysec'
This setting syncs AOF every second, limiting loss to up to 1 second.Step 3: Combine effects
With both RDB and AOF enabled, Redis prioritizes AOF for recovery of recent writes, so maximum data loss is 1 second.Final Answer:
Up to 1 second of data loss -> Option AQuick Check:
AOF everysec = up to 1s loss [OK]
- Thinking RDB interval dominates with AOF enabled
- Ignoring AOF sync policy
- Confusing 'everysec' with immediate sync
Solution
Step 1: Check if config changes take effect immediately
Changing 'appendonly yes' requires Redis restart to activate.Step 2: Rule out other causes
Disk full or corruption would cause errors, snapshots don't affect AOF creation.Final Answer:
Redis was not restarted after config change -> Option DQuick Check:
Config changes need restart [OK]
- Assuming snapshots affect AOF file
- Ignoring need to restart Redis
- Blaming disk issues without checking
Solution
Step 1: Understand data loss vs performance trade-off
'appendfsync always' is safest but slowest; 'everysec' balances safety and speed.Step 2: Combine AOF and snapshots for better durability
Using both reduces data loss and improves recovery options.Step 3: Evaluate options
Enable AOF with 'appendfsync everysec' and RDB snapshots every 5 minutes balances minimal data loss with acceptable performance cost.Final Answer:
Enable AOF with 'appendfsync everysec' and RDB snapshots every 5 minutes -> Option BQuick Check:
Balanced persistence = AOF everysec + snapshots [OK]
- Choosing 'appendfsync always' for performance
- Disabling persistence for safety
- Relying on snapshots alone for minimal loss
Ready to prove it?
Pass the test and this pattern moves unknown
