Back to blog

Five nodes disagree about the truth: who decides?

Thắng Đoàn
Thắng Đoàn

FotoFlow runs a five-node database cluster: one leader takes writes, followers copy them. The leader goes silent. A crashed server, a slow server, and one behind a broken cable all look the same from outside: silence. If the followers guess wrong and promote one of themselves while the leader is alive, two leaders accept writes and the same print gets sold twice: split brain. A timeout can tell you a server is unreachable; it can never tell you the server is dead.

Nothing counts without a majority

The fix is one rule. A leader is only elected, and a write only confirmed, when more than half of all nodes agree. Cut off, the old leader can reach only itself: 1 of 5, so its writes wait and time out. The other four are a majority: they elect a leader and carry on. Any two majorities share at least one node, so two leaders can never both win. The minority side gives up availability so the data stays consistent: CAP in practice.

How the election works

This is the Raft algorithm. The leader sends heartbeats; every follower runs a timer of random length, and if it expires, that follower becomes a candidate. It starts a new term and asks the others for votes; a node votes once per term, and only for a candidate whose log is as complete as its own. Random timers keep split votes rare and settled within a second.

Writes follow the same rule. The leader appends print 7 to Ben to its log and sends it to the followers. A write is committed once a majority has logged it, never before. Every node applies the same log in the same order, so all end up in the same state: a replicated state machine.

When the network heals, the old leader sees a higher term, steps down at once, discards entries that never reached a majority, and copies the new leader's log: nothing confirmed is ever lost. Terms are a logical clock; only the number matters.

Cluster size, and the paused leader

A cluster of N nodes keeps working with floor((N-1)/2) of them down: three nodes survive one failure, five survive two. A fourth node is a trap: the majority becomes 3, so you still survive only one failure, with one more machine that can fail. Use odd sizes, and spread nodes across zones.

The nastier case: the leader freezes for twenty seconds in a garbage-collection pause. The followers elect a new leader and move on. The old one wakes up, sure it still leads, and writes straight over the new leader's work. A node cannot know it was paused; a check from a moment ago is never proof it still leads.

The fix does not depend on the leader noticing. Every request a leader sends carries its term number as a fencing token, and the storage remembers the highest token it has seen. A stale write with token 2 after the new leader's token 3 is refused. Every request a leader sends carries a fencing token, and the storage refuses lower tokens. The same applies to distributed locks: the lock service hands out an increasing number, and the resource checks it.

What consensus costs

Every write waits for a round trip to a majority: a millisecond or two inside one data center, 50 to 150 milliseconds cross-region. Writes all pass through one leader, and with no majority the cluster accepts no writes, on purpose. Spend consensus on the small, critical decisions, not on every like and page view. Almost nobody implements Raft themselves: you run etcd, ZooKeeper or Consul. Kubernetes keeps cluster state in etcd.

What this does not solve

Consensus keeps a cluster of machines agreeing. It says nothing about two requests racing inside one healthy database. That is transactions, the next post.

Your failover today: who decides the leader is dead, and what happens if they are wrong?
If your leader paused for 20 seconds, which shared resource would it corrupt on waking?

Five nodes, one truth, by vote.

Next in the series: Two likes land at the same time: who wins the race?

Sources

The Raft Consensus Algorithm

Designing Data-Intensive Applications

Wikipedia: CAP theorem

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.