Back in medieval Europe, the pope was probably the most powerful and influential individual alive. As the leader of the Church, every major politi
cal decision, from a kingdom dispute to a royal wedding, had to cross his desk and get his LGTM.
No wonder things got heated in Viterbo when the reigning pope died and the election of his successor triggered a legendary three-year saga. It got so bad that desperate townsfolk locked up the cardinals, put them on bread and water, and blew up the roof of the papal palace. People were done waiting for consensus.
The real blocker, though, was baked into the design of the election system. To reach consensus, a group of cardinals, each representing different interests, had to vote for a candidate and achieve a two-thirds supermajority. The point was to prevent the losing side from not recognizing the result and dividing the Church. So in systems terms, the conclave was optimized for safety, preventing it from diverging into conflicting outcomes.
The tradeoff, however, was a never-ending voting loop. The same rules that protected the system from a bad outcome were also keeping it from producing any outcome at all. In systems terms, it lacked liveness, or the ability to keep moving forward.
This tension between safety and liveness would emerge, some centuries later, at the heart of distributed systems. The question being, how can a system agree without splitting up or getting stuck?
Raft is a modern algorithm that takes care of this and is used widely across the industry in systems such as etcd, the database behind Kubernetes, as well as Kafka, CockroachDB and MongoDB.
How Raft works
Suppose a global payment platform records millions of transactions per minute in its database. They can’t afford an outage, a server crash, a flood or a network failure taking down the whole system, so they set up five replicas of their database on separate servers across different locations.
As expected, each database replica, or node, keeps its own copy of the log, which records the transaction operations in order. These logs need to stay consistent so that, once the same operations have been applied, each node returns the same data when queried.
To make this happen, the Raft algorithm puts one of the nodes in charge, called the leader. Its job is to maintain the source of truth for the order of operations and coordinate what gets committed to each database. When a new operation comes in, the leader appends it to its log and sends it to the other nodes, called followers.
Once a majority of nodes, including the leader, have appended the entry to their logs, the leader commits it and tells the followers to do the same. That’s how the cluster reaches consensus for each entry. And if for whatever reason a follower falls behind, the leader sends the missing log entries so it can catch up.
This is what gives us safety. The nodes agree on the same order of committed operations, so they don’t end up with conflicting versions of the data.
But what if the leader is the one that goes down? How would the system even know?
Periodically, the leader sends out pings, called heartbeats, to the other nodes basically saying: hey, I’m alive. But just like popes, leader nodes can die. Or just stop responding, rather. So when a follower node doesn’t receive a heartbeat within a timeout period, it becomes a candidate, votes for itself and asks the other nodes to vote for it, which Raft calls a leader election.
Each follower then votes for the first candidate that asks, as long as that candidate’s log is at least as up to date as its own. Once a candidate gets a majority, it becomes the new leader and starts sending out heartbeats and picks up where the old leader left off.
To reduce the chance of all the nodes becoming candidates at once, the timeout period is randomized for each node. This gives one candidate a chance to collect votes before the other nodes run as candidates themselves. But if the vote still splits and nobody wins, they run the election again with new randomized timeouts.
This is what gives us liveness. A way to move forward when the leader fails or an election gets stuck.
Luckily, nodes don’t have free will, take bribes or have political affiliations, so reaching consensus is a bit less complicated. But as we’ve seen, there’s still plenty that can go wrong.
Something cool about Raft is that it was designed as a more understandable alternative to Paxos, another consensus algorithm. In the words of its authors: “Unless developers have a deep understanding of the algorithm and can create intuitions about it, it will be difficult for them to retain its desirable properties in their implementation.”
So maybe a bunch of cardinals stuck trying to elect a pope gives us a place to start.



