Skip to content

Lesson 9 of 13 · 16m

Consensus Is Not Election

Raft's real job is getting a cluster to commit log entries in one order despite crashes — election is just how it buys the right to try.

Consensus is usually introduced as "electing a leader." That framing inverts the system. The actual problem is state machine replication: getting every node to apply the same commands in the same order, so a set x = 3 on the leader is a set x = 3 on every follower, even after crashes, restarts, and half the cluster going silent. Election is the mechanism Raft uses to buy the right to append entries. The safety rules are where the correctness lives.

Raft (Ongaro & Ousterhout, 2014 — literally titled "In Search of an Understandable Consensus Algorithm," a dig at Paxos) divides time into terms. Each term has at most one leader, elected by majority vote. Two fields — currentTerm and votedFor — must be persisted to disk before a node replies to a vote request or an append. Skip the fsync and a restarted node double-votes in the same term, and two leaders can believe they won the same election. The protocol's correctness leans on durable state, not on cleverness.

The rules that actually keep you safe

The election restriction (§5.4.1): a candidate's log must be at least as up-to-date as the voter's, compared by (lastTerm, lastIndex). A node whose log is stale cannot become leader — otherwise it would win, hold a majority, and overwrite committed entries. This is why "the most up-to-date replica wins" is a rule and not a heuristic.

The commit rule (§5.4.2): an entry is committed once stored on a majority — but a leader may only advance its commit index by counting replicas for an entry from its current term. Entries from older terms become committed only indirectly, when a current-term entry lands on a majority behind them. This is the subtlest rule in the paper and the one that prevents a stale leader from finalizing half-replicated entries from a dead term.

Joint consensus for membership changes (§6): flipping a 3-node cluster to 5 nodes member-by-member can instantiate two disjoint majorities — the old quorum and the new quorum each electing a leader during the cutover. Raft enters a joint configuration (decisions need majorities of both quorums) and only then moves to the new set. If your "add a node" runbook does not go through joint consensus, it can manufacture split-brain on a healthy network.

Where you already run it

etcd is the Raft deployment everyone has — Kubernetes' entire state lives in it. raftpb carries the wire types, wal/ persists entries and hard state, snap/ holds snapshots for followers that fell too far behind. CockroachDB runs a Raft group per range — tens of thousands of independent consensus instances in one cluster, which is why its range-lease machinery exists. TiKV does the same. Consul uses Raft for its catalog. ZooKeeper predates Raft and runs ZAB, its own atomic broadcast — same job, different paper.

The wrong mental model

Two of them. "Consensus equals replication" — consensus decides a value or an order once; state machine replication uses consensus per log entry, and the replicated state machine is the product, not the protocol. And "majority means majority of running nodes" — no, majority of configured members. In a 3-node cluster, losing two leaves one perfectly healthy node that can do nothing, because 1 is not a majority of 3. Quorum loss is unavailability by design, and that is the trade the CAP lesson priced.

What to actually do

Run consensus clusters in odd sizes — 3 or 5 — spread across failure domains; two nodes in one rack is a partition waiting to be named. Prefer embedded consensus (etcd, CockroachDB) over assembling ZooKeeper recipes. Watch etcd_server_leader_changes_seen_total and election latency — leader churn is a symptom of starvation or network degradation, and a cluster that elects weekly is telling you something. And treat quorum members as pets: they are the part of your system that cannot be reimaged casually, because losing their wal is losing the vote history the safety rules depend on.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.