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?
Keep reading
Related articles from the SudoSecurity blog.
What is Platform as a Service? The Sweet Spot Between "Too Easy" and "Too Much Work"
Platform as a Service sits between raw servers and fully managed software. You push code, the platform runs it. No Nginx config, no OS patches, no 3 AM server alerts. Here is what PaaS actually gives you, what it costs you in control, and how to decide if it is the right layer for your application.
The World now is Serverless and Container based
A new report from Datadog reveals that serverless computing could be entering the mainstream. This has already been the case in my world for the past few years. Results show that more than half of all organizations are using serverless on one of the three major public cloud providers: Amazon,
AWS Emulator Puts AWS on Your Laptop. Keep Your IAM Tests Off It.
AWS released AWS Emulator on November 11, a free local server that answers AWS API calls on 127.0.0.1 with per-run keys and fresh certificates.
A 257-Byte Password Is the Key: Inside the OpenSSH musl Bypass
CVE-2026-123158 lets an attacker log into a locked account on musl-based systems like Alpine by sending a password nobody set. The trigger is one number, 257, and the reason is a C library returning a single asterisk. I reproduced the exact boundary against musl 1.2.4 and read the fix.