Raft-like Election Algorithm & Term Succession
MongoDB utilizes a consensus election protocol modeled on Raft (election terms, heartbeat timeouts, and strict majority voting) to elect a primary in under 3 seconds.
Replica Set Raft Consensus & Heartbeat Election Lab
Simulate 3-node replica set heartbeats, failovers, oplog replication, and write concern guarantees.
1. Formal Definition & Architecture Intent
MongoDB's election algorithm is a variant of the Raft consensus protocol. Members continuously exchange heartbeats every 2 seconds. If a secondary fails to receive heartbeats from the primary for 10 seconds (electionTimeoutMillis), it declares an election, increments the election Term counter, and solicits votes from all peers. A candidate must secure a strict majority of votes to become Primary.
2. Explain Like I'm 5 (Mental Model)
Think of high-school class president elections. If the president goes missing for 10 seconds, a candidate stands up, raises their hand, and calls for a vote. If more than half the class votes for them, they instantly become the new president.
3. Real-World Architecture Example
Automatic failover sequence when Primary crashes unexpectedly.
{
"t0": "Primary loses network connectivity (0s)",
"t1": "Heartbeat timeout triggers after 10s",
"t2": "Secondary-1 increments term to 4 and requests votes",
"t3": "Secondary-2 votes YES (candidate has newest oplog)",
"t4": "Secondary-1 wins majority (2/3) and transitions to PRIMARY in 1.8s"
}The entire failover sequence completes in under 2 seconds without human intervention.
4. MQL Implementation Steps
rs.stepDown(60); // Primary resigns for 60s, triggering election
rs.status().term; // Current cluster election term
5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| Strict Majority | A candidate must win Math.floor(N/2) + 1 votes (e.g. 2 out of 3, or 3 out of 5). |
| Oplog Freshness | A node will refuse to vote for any candidate whose oplog timestamp is older than its own. |
6. Deep Dive: Split-Brain Prevention & Network Partition Isolation
Suppose a 5-node replica set is partitioned by a network split into 2 nodes (Side A) and 3 nodes (Side B). Even if the old primary is on Side A, Side A only has 2 votes (cannot reach majority 3/5). The primary on Side A automatically detects that it cannot see a majority and steps down to Secondary. Side B has 3 nodes, reaches majority, and safely elects a new primary.
// Verify heartbeat timeout setting (default 10000ms): rs.conf().settings.electionTimeoutMillis;
7. Architectural Trade-offs
- •Sub-3-second automated failover restores write availability during hardware crashes
- •Raft term counter prevents zombie former primaries from overwriting fresh data
- •Oplog freshness rule guarantees that the candidate with the most recent data wins
- •In-flight uncommitted client writes may throw network exceptions during the 2-second election window
- •Network partitions can isolate a primary, forcing client connection pool reconnects
8. Technical Interview Questions
A secondary will vote NO if: 1. The candidate's oplog entries are less up-to-date than the secondary's own oplog (Oplog Freshness rule), 2. The secondary has already voted for another candidate in the current election term, or 3. The candidate has a priority of 0 (configured as non-electable).
9. Staff-Level Ready Answer
"MongoDB uses a Raft-based consensus protocol. Heartbeats exchange every 2 seconds; if the primary is silent for 10 seconds, a secondary increments the term and requests votes. A candidate must win a strict majority (>50%) and possess the freshest oplog to become Primary."
10. Quick Knowledge Check
What is the default electionTimeoutMillis setting before a secondary initiates an election?