Consensus and leader election
Here's the problem underneath half of distributed systems: several machines must agree on one value — who is the leader, what's the next log entry — while some of them are crashed and the network is eating messages. That's consensus, it's provably hard, and the algorithm you should understand is Raft, which was explicitly designed to be understandable. Let's walk it.
Raft in plain terms
Every node is in one of three roles: Follower (default — quietly applying whatever the leader sends), Candidate (campaigning), or Leader (the one node that accepts writes).
Heartbeats and elections. The leader constantly whispers "still here" to followers. Each follower runs a timeout; if no heartbeat arrives in time, it concludes the leader is dead, promotes itself to candidate, increments the term number (a logical era — "we're now in term 7"), and requests votes. Win a majority, become leader. The clever detail: election timeouts are randomised, so when a leader dies, one follower's timer usually fires first and wins cleanly, instead of every follower campaigning simultaneously and splitting the vote forever.
Replicating writes. All writes flow through the leader. It appends each entry to its log, ships it to followers, and once a majority acknowledge, the entry is committed — durable and applied. Why majority is the magic threshold: any two majorities of the same cluster must overlap in at least one node. So even after a crash and a new election, at least one voter carries the committed history forward — nothing committed can ever be lost or contradicted. That overlap argument is the beating heart of the whole algorithm.
Three consequences to memorise
- Odd cluster sizes. 3 nodes tolerate 1 failure (majority = 2). 5 tolerate 2 (majority = 3). And 4 nodes? Majority is 3, so they tolerate exactly 1 failure — same as three nodes. The fourth machine buys you nothing but cost. Consensus clusters come in odd sizes for arithmetic, not aesthetics.
- No majority, no writes. A partition strands you with 2 of 5 nodes? Those 2 cannot elect a leader or commit anything — the system correctly refuses. This is CP behaviour from Lesson 6.01, chosen on purpose: better silent than split-brained.
- You'll operate this, not implement it. Raft lives inside etcd (Kubernetes' brain), Consul, ZooKeeper, Kafka's controller, and the failover machinery of nearly every managed database. You will almost certainly never write Raft — but you will stare at a 3-node etcd cluster that lost quorum, and you'll need to know exactly why it's refusing writes and why that's the correct behaviour.
Distributed locks: harder than they look, and here's the exact failure
A tempting shortcut for mutual exclusion across machines: grab a Redis key with a TTL as a lock. Now watch it break, step by step:
- Client A acquires the lock, TTL 10 seconds.
- A hits a long garbage-collection pause — the whole process freezes for 15 seconds. (GC pauses, VM migrations, and network stalls all do this; none are exotic.)
- The TTL expires. Redis, reasonably, frees the lock. Client B acquires it.
- A wakes up. A has no idea time passed. It proceeds, fully believing it holds the lock. Two clients now act "exclusively" at once.
The mitigation is a fencing token: the lock service hands out a monotonically increasing number with each grant (A gets 33, B gets 34), and the storage layer being protected checks it — rejecting any write bearing a token lower than one it has already seen. A's late writes (token 33) bounce off after B's (token 34) have landed. The uncomfortable moral: without fencing enforced at the resource, a distributed lock is a strong suggestion, not a guarantee — the resource itself must be the final arbiter.
Running a two-node consensus cluster "for redundancy." A majority of 2 is 2 — so if either node fails, no majority exists and the whole cluster halts. Two nodes are strictly worse than one: same tolerance (zero), doubled failure probability. Consensus redundancy starts at three.
The tradeoff sheet
- Problem solved
- A set of nodes needing to agree on one value or one leader, without a split brain where two of them believe they hold it.
- Rejected, and why
- A single node with manual failover is simpler and correct as long as a human is available. A lease in an existing database works well and moves the problem to that database.
- The trade
- A majority must be reachable for progress, so you have chosen consistency over availability during a partition, and you have added a quorum whose latency floor is the slowest member.
- First to break at 10x
- Latency before throughput. At 10x, the leader is the bottleneck by design, and cross-region members make every write pay the distance to a majority.
- What it does to the bill
- An odd number of nodes to run and monitor for something that stores very little. The expense is operational attention, not capacity.
- Day-2 operations
- Watch leader elections per hour — a cluster flapping between leaders is usually a network or GC problem wearing a consensus costume. Restarts must be one member at a time.
When this actually happened
Forty-three seconds of lost connectivity between coasts, and 24 hours and 11 minutes of degradation. Orchestrator, running Raft, correctly formed a quorum on the west coast and failed the clusters over — while the east coast had already accepted writes that never replicated. Everything behaved as designed; the design had not accounted for a partition healing after writes diverged.
Going deeper
- In Search of an Understandable Consensus Algorithm Ongaro & Ousterhout, 2014
The Raft paper. Written specifically to be followable, and it succeeds — read it before any Paxos treatment. - Paxos Made Simple Leslie Lamport, 2001
Famously still not simple, and worth reading once to understand what Raft was reacting to.