RDB configuration (save intervals) in Redis - Time & Space Complexity
We want to understand how the time Redis takes to save data changes as the amount of data grows.
Specifically, how the save intervals in RDB configuration affect this time.
Analyze the time complexity of Redis saving snapshots based on configured save intervals.
# Example Redis config snippet for RDB save intervals
save 900 1
save 300 10
save 60 10000
# This means:
# Save after 900 seconds if at least 1 key changed
# Save after 300 seconds if at least 10 keys changed
# Save after 60 seconds if at least 10000 keys changed
This configuration controls when Redis creates a snapshot of the database based on time and number of changes.
Redis periodically checks if conditions to save are met.
- Primary operation: Copying all keys to create a snapshot (RDB save)
- How many times: Depends on how often save conditions trigger, but each save copies all keys once
When Redis saves, it copies all keys to disk. The time depends on total keys.
| Input Size (n keys) | Approx. Operations (copy keys) |
|---|---|
| 10 | 10 copies |
| 100 | 100 copies |
| 1000 | 1000 copies |
Pattern observation: The time to save grows linearly with the number of keys.
Time Complexity: O(n)
This means saving takes longer as the number of keys grows, roughly proportional to the total keys.
[X] Wrong: "Saving time depends only on the number of changed keys."
[OK] Correct: Redis saves the entire dataset snapshot, so time depends on total keys, not just changed ones.
Understanding how save intervals affect performance helps you explain trade-offs in data durability and speed.
What if Redis used incremental saving instead of full snapshots? How would the time complexity change?
Practice
save configuration option control?Solution
Step 1: Understand the purpose of
Thesavein Redissaveoption defines intervals for automatic snapshot saving.Step 2: Identify what
It controls when Redis saves data snapshots based on time and changes.savecontrolsFinal Answer:
When Redis automatically saves snapshots of the dataset -> Option CQuick Check:
RDB save intervals = automatic snapshot timing [OK]
save sets snapshot save timing [OK]- Confusing
savewith memory limits - Thinking
savesets connection ports - Assuming
savecontrols authentication
Solution
Step 1: Recall Redis
The syntax issavesyntaxsave <seconds> <changes>without commas or semicolons.Step 2: Match the correct order
Time in seconds comes first, then number of changes.Final Answer:
save 300 10 -> Option BQuick Check:
Correct syntax = save seconds changes [OK]
- Swapping seconds and changes order
- Using commas or semicolons instead of spaces
- Omitting one of the two required values
save 900 1save 300 10What happens if 5 keys change within 400 seconds?
Solution
Step 1: Analyze each save interval
First interval: save every 900 seconds if at least 1 change.
Second interval: save every 300 seconds if at least 10 changes.Step 2: Check conditions for 5 changes in 400 seconds
5 changes < 10, so second interval not triggered.
900 seconds not reached yet, so first interval not triggered either.Final Answer:
Redis does not save a snapshot because 10 changes were not reached -> Option AQuick Check:
5 changes in 400s does not meet any save condition [OK]
- Assuming 5 changes triggers 10-change interval
- Ignoring the time condition for the first interval
- Thinking Redis saves immediately after changes
save 60,5Why does Redis ignore this setting?
Solution
Step 1: Check syntax of
Redis requires space-separated values, not commas.savelineStep 2: Understand why commas cause ignoring
Commas make the line invalid, so Redis skips it.Final Answer:
Because commas are invalid; spaces must separate values -> Option AQuick Check:
Use spaces, not commas in save intervals [OK]
- Thinking commas are allowed separators
- Believing time must be in milliseconds
- Assuming low changes disables saving
- 5 keys changed in 60 seconds
- OR 100 keys changed in 900 seconds
Which configuration lines achieve this?
Solution
Step 1: Match time and changes correctly
Format issave <seconds> <changes>.
So 60 seconds and 5 changes issave 60 5.
900 seconds and 100 changes issave 900 100.Step 2: Verify both conditions are separate lines
Each condition must be on its ownsaveline.Final Answer:
save 60 5 save 900 100 -> Option DQuick Check:
Time first, changes second per line [OK]
- Swapping seconds and changes
- Putting both intervals on one line
- Using commas or semicolons instead of spaces
Ready to prove it?
Pass the test and this pattern moves unknown
