0
0
Redisquery~5 mins

Cluster failover in Redis - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Cluster failover
O(n)
Understanding Time Complexity

When a Redis cluster fails over, it switches the role of a replica to master to keep the system running.

We want to understand how the time to complete this failover changes as the cluster size grows.

Scenario Under Consideration

Analyze the time complexity of the following Redis cluster failover commands.


CLUSTER FAILOVER
CLUSTER REPLICATE <new_master_node_id>
CLUSTER SETSLOT <slot> NODE <new_master_node_id>
CLUSTER ADDSLOTS <slot>
    

This snippet shows commands used during failover to promote a replica and update cluster slot ownership.

Identify Repeating Operations

During failover, the main repeating operation is updating slot ownership across nodes.

  • Primary operation: Assigning slots to the new master node.
  • How many times: Once per slot that the failed master owned, which can be many.
How Execution Grows With Input

As the number of slots to reassign grows, the number of operations grows roughly the same.

Input Size (slots)Approx. Operations
1010 slot updates
100100 slot updates
10001000 slot updates

Pattern observation: The time grows linearly with the number of slots reassigned.

Final Time Complexity

Time Complexity: O(n)

This means the failover time increases in direct proportion to the number of slots that need to be reassigned.

Common Mistake

[X] Wrong: "Failover time stays the same no matter how many slots are reassigned."

[OK] Correct: Each slot reassignment requires a command, so more slots mean more work and longer time.

Interview Connect

Understanding how failover time scales helps you explain system reliability and responsiveness in real-world Redis clusters.

Self-Check

"What if the cluster used fewer slots but more nodes? How would that affect the failover time complexity?"