15 - Distributed Consensus & Leader Election: Raft and Paxos

Executive Summary: Distributed consensus is the fundamental problem of getting a set of independent, unreliable computers to agree on a single state or sequence of events—even when networks drop packets and servers crash at any time. While Leslie Lamport's Paxos (1998) was notoriously difficult to understand and implement in industry, Diego Ongaro and John Ousterhout introduced Raft (2014), decomposing consensus into three understandable, independent sub-problems: Leader Election, Log Replication, and Safety. Raft now underpins Kubernetes (etcd), Kafka (KRaft), CockroachDB, and Consul.


🏢 Real-world case studies: how Google Chubby, Raft and etcd (under Kubernetes) provide consensus, in REAL_WORLD_EXAMPLES.md.

⏱️ Prerequisites: What You Need to Know First

To get the most out of this module, make sure you understand:


🧒 The 30-Second Version (ELI5)

Imagine a committee of 5 judges making decisions:


🍕 Everyday Analogy

Concept Parliamentary Committee Analogy Technical Reality in Raft / Paxos
Node A committee member sitting at the table An independent server / process in the cluster
Leader The elected Chairperson who writes the meeting minutes The single node that accepts client writes and manages the log
Follower Passive committee members listening to the Chairperson Passive nodes that append log entries sent by the leader
Candidate A member standing up to run for Chairperson A follower whose election timer expired, seeking votes
Term Election term (e.g. 2024–2028 term) Monotonically increasing logical epoch number (1, 2, 3 …)
Heartbeat Chairperson tapping the microphone every 10 seconds: "I'm here" Empty AppendEntries RPC sent every 50 ms to prevent elections
Election Timeout Member's internal timer: "If no speech in 3 mins, I'll run" Randomized timer (150–300 ms) preventing split-vote deadlocks
Quorum Floor(N/2) + 1 votes needed to pass a law Strict majority required to elect a leader or commit a log entry
Split-Brain Two people both declaring they are Chairperson and giving conflicting orders Catastrophic failure where two nodes accept conflicting writes

🧩 The Problem It Solves

The Nightmare of Split-Brain

In a distributed database, if multiple servers believe they are the active Leader simultaneously:

What Consensus Guarantees

Consensus algorithms like Raft and Paxos provide mathematical proof of Safety and Liveness:

  1. Election Safety: At most one leader can be elected in any given term.
  2. Leader Append-Only: A leader never overwrites or truncates its own log entries; it only appends new entries.
  3. Log Matching Property: If two logs contain an entry with the same index and term, then the logs are identical in all entries up through the given index.
  4. Leader Completeness: If a log entry is committed in a given term, that entry will be present in the logs of the leaders for all higher-numbered terms.
  5. State Machine Safety: If a server has applied a log entry at a given index to its state machine, no other server will ever apply a different log entry for the same index.

🖼️ Picture It: Architecture & Flow

High-Resolution Raft Consensus Animation

Below is an animated trace of a Raft 3-node cluster handling a leader crash, randomized timer timeout, quorum vote, and clean recovery without split-brain:

Raft Distributed Consensus


Raft State Machine Lifecycle

stateDiagram-v2
    [*] --> Follower: Server starts up

    Follower --> Candidate: Times out (No heartbeat received)<br/>Increments Term, votes for self
    
    Candidate --> Candidate: Election times out (Split vote)<br/>Starts new election with Term + 1
    Candidate --> Follower: Discovers Leader or newer Term
    Candidate --> Leader: Receives votes from majority of servers

    Leader --> Follower: Discovers server with higher Term
    Leader --> [*]: Shutdown / Crash
    Follower --> [*]: Shutdown / Crash
    Candidate --> [*]: Shutdown / Crash

    note right of Leader
        Broadcasts AppendEntries heartbeats
        every 50ms to suppress elections.
    end note

🔢 Walk-Through with Real Numbers

Let's calculate the cluster sizing, fault tolerance, and failover latency for an etcd Kubernetes control plane:

1. Quorum Sizing Formula: Surviving F Node Failures

To survive F node crashes, a consensus cluster requires a total of:

N = 2F + 1 nodes

And the majority quorum size required to proceed is:

Q = ⌊ N/2 ⌋ + 1 nodes
Total Nodes (N) Crashes Tolerated (F) Quorum Needed (Q) Why Even Numbers (N=4, N=6) are Bad
1 0 1 Single point of failure.
3 1 2 Standard minimal production cluster.
4 1 3 Antipattern: Requires 3 votes, tolerates only 1 failure (same as 3 nodes!), but adds 33% more network overhead!
5 2 3 Industry Gold Standard: Survives 2 simultaneous node crashes across 3 Availability Zones.
7 3 4 Used in high-resilience global control planes.

2. Failover Latency Calculation


⚙️ How It Works Under the Hood

1. The Three Roles of a Raft Node

  1. Follower:
    • Completely passive. Responds to incoming RPCs from leaders and candidates.
    • Maintains an internal countdown timer (election_timeout). Every time it receives an AppendEntries heartbeat from a valid leader, it resets this timer to a fresh random value.
  2. Candidate:
    • Triggered when election_timeout reaches zero.
    • Increments its current_term, votes for itself, and broadcasts RequestVote RPCs to all peers.
  3. Leader:
    • Handles all client read and write requests.
    • Converts client writes into log entries, appends them locally, and broadcasts AppendEntries RPCs to replicate them to all followers.
    • Once an entry is confirmed written on a majority of nodes, the leader commits the entry and applies it to its local state machine.

2. Why Randomized Election Timeouts Prevent Split-Votes

If all followers shared the exact same election timeout (e.g. exactly 150 ms):

The Raft Solution: Randomize timeouts over a spread (e.g., 150 ms… 300 ms). Almost certainly, one single follower (say Node B at 162 ms) will time out before anyone else, transition to Candidate, and collect votes from the other followers before their timers expire.


3. Log Replication & The "Leader Completeness" Invariant

How does Raft guarantee that a newly elected leader has all committed data?


⚖️ Trade-Offs & Decision Matrix

Dimension Raft / Multi-Paxos (Leader-Based) Leaderless Dynamo / Gossip Two-Phase Commit (2PC)
Consistency Strict Linearizability (CP) Eventual Consistency (AP) Strict Atomic Commit (CP)
Fault Tolerance Survives F crashes out of 2F+1 Survives any node loss Zero crash tolerance (coordinator crash blocks all)
Write Bottleneck Single Leader node Distributed across all nodes Coordinator is single point of failure
Throughput Moderate (~10k–50k writes/sec) Massive (>500k writes/sec) Low (heavy synchronous locking)
Best Used For Cluster metadata, locks, master elections Large-scale metrics, social feeds Cross-shard atomic transactions

🚫 Common Mistakes & Production Pitfalls

  1. Deploying Clusters with Even Numbers of Nodes (N=4 or N=6):

    • Pitfall: A team deploys 4 etcd nodes across 2 data centers (2 in DC-1, 2 in DC-2), thinking 4 nodes is safer than 3.
    • Result: A network partition splits the DCs. DC-1 has 2 nodes; DC-2 has 2 nodes. Neither DC has a majority (> 4/2 = 3 votes). The entire cluster locks up and refuses all writes! Always deploy odd numbers: 3, 5, or 7.
  2. Storing Bulk Application Data in etcd/ZooKeeper:

    • Pitfall: Using etcd to store user avatars or large JSON blobs.
    • Result: Every byte written to etcd must be replicated sequentially via Raft consensus across the cluster and stored in memory. Raft log sizes explode, disk fsync times spike, heartbeats miss their deadlines, and the cluster falls into continuous leader-election thrashing. Consensus stores should only hold tiny metadata (flags, configs, leases).
  3. Disk I/O Starvation Causing Leader Drops:

    • Pitfall: Running etcd on shared magnetic disks or low-IOPS cloud volumes alongside heavy background database writes.
    • Result: When disk fsync() takes >50 ms, the leader cannot flush its log in time to send heartbeats, triggering false election timeouts. Always use dedicated NVMe SSDs for consensus stores.

💻 Try It Yourself: Python Simulation

The companion simulation script demo.py implements:

  1. A multi-node Raft state machine with Follower, Candidate, and Leader roles.
  2. Heartbeat tick cycles resetting follower randomized timers.
  3. Live Leader crash simulation triggering election timeouts.
  4. Candidate vote gathering with majority quorum checks (2/3 votes).
  5. Old leader reboot and safe demotion to Follower upon discovering newer terms.

Run it locally in your terminal:

python demo.py

🌍 Where You'll See It in Production


🎯 Top Interview Questions & Deep Answers

Q1: "Why do consensus systems like Raft and Paxos require an odd number of nodes (3, 5, 7)?"

Interviewer Goal: Testing mathematical understanding of majority quorums and hardware economics.

Model Answer: "Consensus algorithms require a strict majority quorum:

Q = ⌊ N/2 ⌋ + 1

Adding an even node never increases fault tolerance, but it increases the number of votes required:


Q2: "What is the role of 'Terms' in Raft, and how do they resolve conflicting leadership claims?"

Interviewer Goal: Checking deep knowledge of Raft's logical clock mechanics.

Model Answer: "In Raft, time is divided into arbitrary numbered epochs called Terms (Term 1, Term 2, etc.), which act as a logical clock:

  1. Each term begins with an election where one or more candidates attempt to become leader.
  2. Nodes exchange their current_term on every single RPC. If a node discovers that another node's term is strictly greater than its own, it immediately updates its term and steps down to Follower.
  3. If an old leader experiences a network partition and continues trying to send heartbeats with Term 1, when it reconnects to the cluster, followers will see Term 1, reject the RPC, and reply with Term 2. The old leader sees Term 2 > Term 1 and immediately relinquishes leadership. Terms guarantee that stale leaders from previous epochs are cleanly and instantaneously neutralized."

Q3: "What is the difference between Paxos and Raft?"

Interviewer Goal: Assessing historical perspective and theoretical grasp of distributed algorithms.

Model Answer: "Both Paxos and Raft are mathematically equivalent in their safety guarantees and fault tolerance (2F+1 nodes to survive F crashes under an asynchronous network model with crash-recovery).

The primary differences are architectural and pedagogical:

  1. Understandability & Decomposition: Basic Paxos only achieves consensus on a single value. Multi-Paxos extends this to a log, but leaves leadership mechanisms, log compaction, and membership changes unspecified. Raft was explicitly designed by Ongaro & Ousterhout to be understandable, cleanly decomposing consensus into three rigid sub-problems: Leader Election, Log Replication, and Safety.
  2. Strong Leadership: In Multi-Paxos, multiple nodes can propose values concurrently, leading to dueling proposer conflicts unless complex leases are added. In Raft, log entries only ever flow in one direction: strictly from the Leader to Followers.
  3. Log Gaps: Multi-Paxos allows out-of-order log commits, leaving temporary 'holes' in the log that must be filled. Raft enforces the Log Matching Property: logs never have holes, simplifying snapshotting and recovery."

Q4: "How does a Raft leader handle read requests without serving stale data (linearizable reads)?"

Interviewer Goal: Testing advanced edge-case handling in consensus protocols.

Model Answer: "A naive Raft implementation would simply read from the leader's local state machine. However, this violates linearizability if the leader was partitioned off from the cluster and a new leader was elected in a higher term (a 'zombie leader'). The zombie leader might serve stale data before discovering it was deposed.

To guarantee linearizable reads without serving stale data:

  1. Read-Index Protocol: Before returning a read, the leader records its current commit index (ReadIndex) and sends a round of heartbeat RPCs to a majority of nodes. If the majority confirms it is still the leader, it knows no new term has started, and it can safely serve the read once its state machine applies up to ReadIndex.
  2. Lease Reads: The leader relies on a bounded time lease. As long as the leader received heartbeats from a majority within the last Tlease milliseconds, it assumes no new election could have occurred (assuming bounded clock drift). This avoids the network round-trip for reads, drastically lowering read latency."

Q5: "What is Log Compaction / Snapshotting in Raft, and why is it necessary?"

Interviewer Goal: Testing operational longevity and memory management of consensus systems.

Model Answer: "In a long-running system, the write-ahead log grows indefinitely. Without compaction, servers would run out of disk space, and a rebooting node would take hours or days to replay millions of log entries from the beginning of time.

Log Compaction (Snapshotting):


✅ Check Yourself: Flash Quiz

1. In a 5-node Raft cluster, how many nodes can crash simultaneously while allowing the cluster to continue processing writes?
2 nodes. The formula is N = 2F + 1. For N = 5, F = 2. The quorum size is ⌊ 5/2 ⌋ + 1 = 3 nodes. If 2 nodes crash, 3 remain, which satisfies the quorum. If a 3rd node crashes, only 2 remain, and the cluster halts writes to preserve safety.
2. Why does Raft use randomized election timeouts instead of a fixed duration?
To prevent split-vote deadlocks. If all followers had the same timeout, they would all become candidates at the exact same millisecond upon a leader crash, vote for themselves, split the vote equally, and repeat indefinitely. Randomization ensures one candidate times out first and wins.
3. Can a candidate missing committed entries ever be elected Leader in Raft?
No. Followers strictly reject vote requests if the candidate's log is less up-to-date than their own. Because committed entries reside on a majority of nodes, any winning candidate must receive a vote from at least one node holding the latest committed entry.