Automatic failover behavior in MongoDB - Time & Space Complexity
When MongoDB automatically switches to a new primary after a failure, it runs a process called failover.
We want to understand how the time this process takes grows as the number of servers increases.
Analyze the time complexity of the automatic failover election process in a MongoDB replica set.
// Simplified election process
rs.initiate();
// Members vote for a new primary
rs.stepDown();
// Election votes collected
// New primary elected
This code triggers an election where members vote to pick a new primary after the old one steps down.
In the election process, each member sends and receives votes.
- Primary operation: Each member communicates with every other member to collect votes.
- How many times: This happens once per member, so roughly once per server in the set.
As the number of servers grows, the number of vote messages grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | About 9 vote messages |
| 5 | About 25 vote messages |
| 10 | About 100 vote messages |
Pattern observation: The number of vote messages grows roughly with the square of the number of servers.
Time Complexity: O(n^2)
This means the communication work grows quickly as you add more servers, because each talks to many others.
[X] Wrong: "Failover time grows linearly with the number of servers because each server votes once."
[OK] Correct: Each server must communicate with many others, so the total messages grow faster than just one per server.
Understanding how failover scales helps you explain system reliability and responsiveness in real-world setups.
"What if the election used a leader to collect votes instead of all-to-all communication? How would the time complexity change?"