Cluster failover in Redis - Time & Space 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.
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.
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.
As the number of slots to reassign grows, the number of operations grows roughly the same.
| Input Size (slots) | Approx. Operations |
|---|---|
| 10 | 10 slot updates |
| 100 | 100 slot updates |
| 1000 | 1000 slot updates |
Pattern observation: The time grows linearly with the number of slots reassigned.
Time Complexity: O(n)
This means the failover time increases in direct proportion to the number of slots that need to be reassigned.
[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.
Understanding how failover time scales helps you explain system reliability and responsiveness in real-world Redis clusters.
"What if the cluster used fewer slots but more nodes? How would that affect the failover time complexity?"
Practice
Solution
Step 1: Understand cluster failover concept
Cluster failover automatically promotes a replica to master when the current master fails.Step 2: Identify the main purpose
This process ensures Redis availability without manual intervention.Final Answer:
To switch a replica to master if the current master fails -> Option DQuick Check:
Failover = switch replica to master [OK]
- Confusing failover with backup
- Thinking failover increases replicas
- Assuming failover clears data
Solution
Step 1: Recall Redis failover command syntax
The correct command to trigger failover isCLUSTER FAILOVER.Step 2: Check options for syntax correctness
Options B, C, and D are invalid Redis commands or incorrect order.Final Answer:
CLUSTER FAILOVER -> Option AQuick Check:
Correct command = CLUSTER FAILOVER [OK]
- Swapping command words order
- Adding invalid keywords
- Using non-existent commands
CLUSTER FAILOVER FORCE on a replica?Solution
Step 1: Understand FORCE option in failover
FORCE forces the replica to become master immediately, ignoring cluster state.Step 2: Analyze other options
Waiting for master recovery is default without FORCE; FORCE does not cause errors or data deletion.Final Answer:
The replica immediately becomes master regardless of cluster state -> Option CQuick Check:
FORCE = immediate failover [OK]
- Thinking FORCE waits for master
- Assuming FORCE causes errors
- Believing FORCE deletes data
CLUSTER FAILOVER TAKEOVER on a replica but got an error. What is the most likely cause?Solution
Step 1: Understand TAKEOVER option requirements
TAKEOVER forces failover even if the master is alive but requires cluster connectivity.Step 2: Identify error cause
If the replica is disconnected, the command fails with an error.Final Answer:
The replica is not connected to the cluster -> Option BQuick Check:
Disconnected replica causes TAKEOVER error [OK]
- Assuming TAKEOVER is unsupported
- Ignoring replica connection status
- Thinking syntax is wrong
Solution
Step 1: Review failover options and safety
DefaultCLUSTER FAILOVERwaits for safe conditions, avoiding data loss.Step 2: Compare with FORCE and TAKEOVER risks
FORCE and TAKEOVER can cause data loss by ignoring cluster state or master status.Step 3: Consider manual disconnection risks
Manually disconnecting master is risky and not recommended for safe failover.Final Answer:
Use CLUSTER FAILOVER without options to allow safe automatic failover -> Option AQuick Check:
Safe failover = default CLUSTER FAILOVER [OK]
- Using FORCE or TAKEOVER without understanding risks
- Manually disconnecting master unnecessarily
- Assuming all failover options are equally safe
Ready to prove it?
Pass the test and this pattern moves unknown
