Skip to content
LESSON 42 / 50Advanceddistributed

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.

Interactive Engine Simulator

Replica Set Raft Consensus & Heartbeat Election Lab

Simulate 3-node replica set heartbeats, failovers, oplog replication, and write concern guarantees.

Consensus Term
Term 1
Monotonically increasing
Voting Quorum
2 / 3 Majority
Strict floor(N/2) + 1
Heartbeat Interval
2000 ms
10s election timeout
Write Concern
node-1PRIMARY
Host:mongo-node-1:27017
Oplog Sequence:#1045
Voted Term:Term 1
Accepts all driver write and read traffic (ReadPreference: primary).
node-2SECONDARY
Host:mongo-node-2:27017
Oplog Sequence:#1045
Voted Term:Term 1
Tails primary local.oplog.rs in background thread.
node-3SECONDARY
Host:mongo-node-3:27017
Oplog Sequence:#1045
Voted Term:Term 1
Tails primary local.oplog.rs in background thread.
REPL_LOG:Replica set stable on Term 1. Primary node-1 actively serving writes.

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.

Sample BSON Documents:
Collection: db.election_timeline
{
  "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

Step 1: Step down primary manually to trigger controlled failover
rs.stepDown(60); // Primary resigns for 60s, triggering election
Allows graceful zero-downtime rolling maintenance.
Step 2: Inspect election term and member priorities
rs.status().term; // Current cluster election term
Monotonically increasing integer term tracking cluster generations.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
Strict MajorityA candidate must win Math.floor(N/2) + 1 votes (e.g. 2 out of 3, or 3 out of 5).
Oplog FreshnessA 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;
Production Warning: Network latency spikes between data centers can trigger false-positive failovers if timeout is too short.

7. Architectural Trade-offs

Advantages
  • •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
Disadvantages & Gotchas
  • •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

Q1: Under MongoDB's election protocol, why will a secondary reject a candidate's request for a vote?

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?