0
0
Redisquery~5 mins

Combined RDB + AOF in Redis - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Combined RDB + AOF
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

As the number of keys grows, the time to save grows too.

Input Size (n)Approx. Operations
10About 10 key writes
100About 100 key writes
1000About 1000 key writes

Pattern observation: The time grows roughly in direct proportion to the number of keys.

Final Time Complexity

Time Complexity: O(n)

This means the saving time grows linearly with the amount of data stored.

Common Mistake

[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.

Interview Connect

Understanding how Redis saves data helps you explain performance impacts clearly and shows you know how data size affects operations.

Self-Check

"What if Redis used only AOF without RDB snapshots? How would the time complexity change?"