Distributed Consensus and the Raft Algorithm

Distributed consensus is the problem of getting multiple independent servers to agree on a single sequence of values or state — even when machines crash, restart, or suffer network delays and partitions. Consensus is the engine powering leader election, distributed locks, configuration stores (etcd in Kubernetes, Consul), and strongly consistent databases (CockroachDB, TiKV, Cockroach, Kafka KRaft).

Because Paxos was notoriously difficult to understand and implement cleanly, Diego Ongaro and John Ousterhout designed Raft (2014) to provide the exact same fault-tolerance guarantees while decomposing consensus into two understandable parts: Leader Election and Log Replication.

How Raft Works: Leader Election & Log Replication

Every node in a Raft cluster is in one of three states: Leader (handles all client writes and sends heartbeats), Follower (passive replica that obeys the leader), or Candidate (a follower requesting votes after the leader goes silent). Time is divided into monotonically increasing Terms (logical election epochs).

Raft consensus two-phase flow on a 5-node cluster: in Leader Election, Candidate Node 1 wins 3 of 5 majority votes (self plus Nodes 2 and 3) to become Leader even while Nodes 4 and 5 are offline; in Log Replication, the Leader appends SET x = 9 to log[5], replicates via AppendEntries to Followers, and commits once a majority (3 of 5) persists it Raft consensus two-phase flow on a 5-node cluster: in Leader Election, Candidate Node 1 wins 3 of 5 majority votes (self plus Nodes 2 and 3) to become Leader even while Nodes 4 and 5 are offline; in Log Replication, the Leader appends SET x = 9 to log[5], replicates via AppendEntries to Followers, and commits once a majority (3 of 5) persists it
Raft breaks consensus into two halves: (1) electing one leader via majority quorum, and (2) committing log entries once a majority persists them · Made with Akriti

Leader Election (Majority Quorum)

  1. Heartbeat timeout: The Leader sends periodic AppendEntries heartbeats (every ~50–150ms) to all followers. If a follower receives nothing within its randomized election timeout (150–300ms), it assumes the leader crashed.
  2. Request votes: The follower increments its Term (e.g., Term 1 → Term 2), votes for itself, becomes a Candidate, and broadcasts RequestVote RPCs to every peer.
  3. Win a majority (N / 2 + 1): Each node grants at most one vote per term (and only if the candidate’s log is at least as up-to-date as its own). In a 5-node cluster, collecting 3 votes elects the new Leader immediately — even if 2 nodes are dead!
  4. Split-vote recovery: Because election timeouts are randomized, split votes are rare; if one happens, nodes time out at different instants and start Term 3 cleanly.

Log Replication (Committed Only at Majority)

  1. Client write arrives at Leader: Client sends SET x = 9. The Leader appends the entry to its local write-ahead log (log[5], uncommitted).
  2. Replicate to followers: Leader sends AppendEntries(log[5]) to all followers in parallel.
  3. Commit on majority: The instant a majority (3 of 5: Leader + 2 followers) confirms the entry is durably written to disk, the Leader marks log[5] committed, applies x = 9 to its state machine, and returns OK to the client.
  4. Lagging nodes catch up: Offline nodes (Nodes 4 & 5) automatically receive missed log entries once they reconnect — Raft’s log-matching check guarantees every node eventually holds the exact same log in the exact same order.

Why Majority Quorums Prevent Split-Brain

Suppose a network partition splits a 5-node cluster into {N1, N2} (2 nodes) and {N3, N4, N5} (3 nodes):

  • Any two majorities in an N-node cluster must overlap by at least one node.
  • {N3, N4, N5} has a quorum (3/5), so it can elect a new leader and keep accepting writes.
  • {N1, N2} only has 2/5 — it cannot win an election or commit new writes! Even if N1 used to be the old leader, its writes stall because it cannot get 3 ACKs, completely preventing split-brain (two leaders accepting conflicting writes).
  • In CAP theorem terms, Raft is strictly CP — the minority partition sacrifices availability to guarantee linearizable consistency.

Why Consensus Clusters Always Use Odd Sizes (3, 5, 7)

Cluster Size (N) Quorum Needed Failures Tolerated Recommendation
3 nodes 2 1 node Smallest production HA cluster (survives 1 crash or planned reboot)
4 nodes 3 1 node Avoid! Same fault tolerance as 3 nodes, higher latency and bigger quorum
5 nodes 3 2 nodes Standard production sweet spot (survives 1 planned maintenance + 1 unexpected crash)
7 nodes 4 3 nodes Large multi-AZ control planes; beyond 7, replication fan-out slows writes

Raft vs. Paxos vs. Zab

  • Raft (etcd, CockroachDB, TiKV, Consul, Kafka KRaft): Strong leader, contiguous log replication, explicit randomized election timeouts — designed for understandability and clean production engineering.
  • Multi-Paxos (Google Spanner, Chubby): The classic theoretical foundation. Very flexible, but the original paper left many practical details (log compaction, membership changes, leader leases) unspecified, leading to varied implementations.
  • Zab (Apache ZooKeeper): Atomic broadcast protocol conceptually very close to Raft (primary-backup with epoch numbers and majority quorums), powering legacy Hadoop/Kafka/HBase coordination.

FAQs

Why don’t we run Raft across 100 database nodes? Every single write in Raft must round-trip to a majority of nodes in the consensus group (51 nodes out of 100!), which destroys throughput and latency. Instead, distributed SQL databases like CockroachDB and Google Spanner split data into thousands of small shards (see Partitioning Strategies), and each shard runs its own tiny 3-node or 5-node Raft group (Multi-Raft).
What happens if an old leader doesn’t realize it was partitioned and receives a read? Serving a local read from a stale partitioned leader would violate linearizability! To guarantee strongly consistent reads, Raft leaders either perform a lightweight ReadIndex quorum heartbeat check (confirming they are still leader) or use Leader Leases bounded by clock-drift guarantees.
Does Raft handle malicious or hacked nodes (Byzantine failures)? No. Raft, Paxos, and Zab assume crash-recovery failures (nodes may crash, restart, or drop packets, but they don’t intentionally lie or forge messages). Handling malicious actors requires Byzantine Fault Tolerant (BFT) protocols like PBFT or Tendermint, which need 3f + 1 nodes and far more messaging overhead.
Where is Raft used in Kubernetes? Kubernetes stores all cluster state (pods, deployments, secrets, and service discovery endpoints) inside etcd, a distributed key-value store powered directly by Raft.