Why persistence matters in Redis - Performance Analysis
When using Redis, persistence means saving data so it is not lost if the server stops.
We want to understand how saving data affects the time it takes to run commands.
Analyze the time complexity of saving data with Redis persistence commands.
SET user:1 "Alice"
SET user:2 "Bob"
SAVE
SET user:3 "Carol"
BGSAVE
This code sets some keys and triggers saving data to disk synchronously and asynchronously.
Look for operations that repeat or take time based on data size.
- Primary operation: Saving the entire dataset to disk (SAVE or BGSAVE)
- How many times: Once per SAVE or BGSAVE command, but the cost depends on data size
Saving data takes longer as the amount of data grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 keys | Small, quick save |
| 100 keys | About 10 times longer save |
| 1000 keys | About 10 times longer save |
Pattern observation: The time to save grows roughly with the amount of data, so more data means more time.
Time Complexity: O(n)
This means saving data takes time proportional to how much data you have.
[X] Wrong: "Saving data always takes the same short time regardless of data size."
[OK] Correct: Saving writes all data to disk, so more data means more work and longer time.
Understanding how persistence affects performance helps you design systems that keep data safe without slowing down too much.
"What if Redis used incremental saving instead of saving all data at once? How would the time complexity change?"