Skip to content

Consensus & Leader Election

Consensus is the problem of getting multiple servers to agree on a value. Leader election is a common way to achieve consensus — one server is the leader, others follow.


sequenceDiagram
participant L as 🏆 Leader (Server 1)
participant F2 as 📋 Follower (Server 2)
participant F3 as 📋 Follower (Server 3)
participant F4 as 📋 Candidate (Server 4 — disconnected)
Note over L,F4: Term 1 — Normal Operation
L->>F2: Heartbeat (append entries)
L->>F3: Heartbeat (append entries)
F2-->>L: ✅ Ack
F3-->>L: ✅ Ack
Note over L,F4: Term 2 — Leader Fails ❌
F2->>F2: No heartbeat from leader (timeout)
F2->>F2: Start election
F2->>F3: Request vote (term 2)
F2->>F4: Request vote (term 2) — no response
F3-->>F2: ✅ Vote granted
Note over F2: ✅ Elected leader for term 2!

  1. Leader election: Servers vote for a leader. The candidate with majority votes wins.
  2. Log replication: The leader accepts writes and replicates them to followers.
  3. Commit: When a majority of followers acknowledge a write, it’s committed.
  4. Safety: If the leader fails, a new leader is elected with the latest committed data.

A quorum is the minimum number of nodes that must agree for a decision to be valid.

With 3 nodes: quorum = 2 (majority)
With 5 nodes: quorum = 3 (majority)

Why quorum matters:

  • Leader needs quorum votes to be elected
  • Writes need quorum acks to be committed
  • Reads need quorum responses to be consistent

AspectRaftPaxos
Understandability✅ Designed for understandability❌ Famous for being hard to understand
LeaderStrong leader (elected)Multiple proposers possible
ImplementationUsed in etcd, Consul, MongoDBUsed in Google Chubby, Spanner
Log consistencySequential log replicationMultiple values can be proposed
Real-world usageVery commonCommon (often hidden behind abstractions)

SystemConsensus AlgorithmPurpose
etcd / ConsulRaftService discovery, distributed config
Apache ZooKeeperZab (similar to Paxos)Coordination, leader election
MongoDB (replica set)RaftPrimary election, write consistency
KafkaKRaft (Raft-based)Controller election, metadata management

  • Consensus = getting multiple servers to agree on who the leader is and what data is correct.
  • Raft is the most popular algorithm — it’s designed to be understandable.
  • A quorum (majority) must agree for things to happen. 3-node cluster needs 2/3 votes.