0%
Distributed Systems: Consensus, Clocks, and Replication
Foundations
Time and Order
Replication
Consistency Models
Transactions and Operations
Raft Leader Election
Raft was designed by Diego Ongaro and John Ousterhout and published in 2014 with an unusual goal: understandability. Paxos had been the standard consensus algorithm for decades, but engineers found it hard to learn and harder to implement correctly, and the papers left many practical details unspecified. Raft solves the same problem, replicated log consensus under crash faults, but decomposes it into pieces that can be understood separately: leader election, log replication and safety. Today Raft underlies etcd, Consul, CockroachDB, TiKV, Kafka KRaft, MongoDB replication in a modified form and dozens of other systems. This lesson covers the first piece, leader election, and the next lesson covers log replication and the safety argument.
Strong leadership
Raft is a strongly leader based protocol. At any time, each server is in one of three states: follower, candidate or leader. In normal operation there is exactly one leader and all other servers are followers. Followers are passive: they respond to requests from leaders and candidates but issue none of their own. The leader handles all client requests, appends commands to its log and replicates them to followers. Log entries flow only from leader to followers, never the other way. This asymmetry is what makes Raft simpler than symmetric protocols: most of the complexity lives in electing a leader and handling the transition between leaders, while steady state operation is straightforward.
Terms
Raft divides time into terms of arbitrary length, numbered with consecutive integers. Each term begins with an election in which one or more candidates try to become leader. If a candidate wins, it serves as leader for the rest of the term. If the election ends with a split vote, the term ends with no leader, and a new term begins shortly after. Raft guarantees at most one leader per term. Terms act as a logical clock: every server stores a current term number, which only increases, and includes it in every message. Terms are exactly the ballots of the previous lesson under a new name.
The term rules
Three simple rules keep terms consistent. If a server receives a message with a higher term than its own, it updates its current term to that value and, if it was a leader or candidate, immediately reverts to follower. If a server receives a request with a lower term than its own, it rejects the request. And a server persists its current term to stable storage before responding to any message, so that it never goes backward after a restart. These rules ensure that stale leaders discover they are stale as soon as they talk to anyone more current, and that their messages are ignored until they do.
Only two RPCs
The core Raft protocol needs only two remote procedure calls. RequestVote is sent by candidates during elections to ask for votes. AppendEntries is sent by leaders to replicate log entries and, when empty, as a heartbeat that asserts leadership. A third RPC, InstallSnapshot, is used to send snapshots to followers that are far behind. This small surface is part of the understandability goal: an implementer can hold the whole protocol in mind, and the paper presents it in a single page summary that has become one of the most reproduced figures in systems research.
Persistent and volatile state
Each server keeps a little state on stable storage: its current term, the candidate it voted for in the current term, if any, and its log. This state must be written durably before responding to RPCs, since forgetting a vote could let a server vote twice in one term and allow two leaders. Other state is volatile: the commit index, the index of the last applied entry and, on leaders, per follower bookkeeping called nextIndex and matchIndex. Volatile state is rebuilt after a restart from the log and from communication with other servers, which keeps the persistent footprint small and the recovery logic simple.
Why one leader simplifies everything
Having a single leader means there is a single place that decides the order of log entries, so followers never need to resolve conflicts among concurrent proposals. Client requests have a single destination, so there is no question of which server should accept a write. Reads can be served by the leader with simple rules. The cost is that the leader is a throughput bottleneck and that there is a period of unavailability whenever it fails, until a new leader is elected. For coordination services and per shard replication, these costs are acceptable, and the gain in simplicity is large.
Where Raft sits among protocols
Raft is closely related to Viewstamped Replication, which Brian Oki and Barbara Liskov published in 1988 and which also uses a strong primary and view numbers. It is also equivalent in power to Multi Paxos, which the later Paxos lesson covers. Raft made specific design choices, such as requiring the leader to have every committed entry and using randomized timeouts, that remove whole categories of edge cases. Those choices make Raft slightly less flexible than Paxos in exchange for being far easier to implement and verify, and the Raft paper includes a user study in which students learned Raft more readily than Paxos.
The rest of this lesson
The following sections walk through an election step by step, explain the election restriction that ensures a new leader has every committed entry, examine how Raft keeps elections from stalling and finally look at the practical details that production implementations add, including prevote, check quorum and leadership transfer. Throughout, keep in mind the two goals from the previous lesson: safety must never depend on timing, and liveness should hold whenever the network is stable enough.
A day in the life of a Raft cluster
Most of the time, a Raft cluster is boring, which is exactly the goal. The leader receives client commands, appends each to its log, sends AppendEntries to followers carrying the new entries and, once a majority has stored them, marks them committed, applies them and replies to the client. Between commands, it sends empty AppendEntries as heartbeats every fifty to a hundred milliseconds. Followers append what they receive, acknowledge and reset their election timers. Terms stay the same for weeks. The interesting parts of Raft, elections and log repair, run only when something fails or an operator moves leadership, and good monitoring treats any term change as an event worth noticing.
Reading the Raft figure
Engineers implementing Raft usually work from the one page summary in the paper, which lists the state each server keeps and, for each RPC, its arguments, results and receiver rules. A useful exercise is to read every rule and ask what would break if it were removed. Dropping the rule that a server adopts higher terms lets deposed leaders keep acting. Dropping the persistence rule allows double votes. Dropping the log comparison in RequestVote lets a behind server overwrite committed entries. Each line in the figure prevents a specific failure, and almost every Raft bug found in the wild violates one of them.
What Raft does not solve
Raft agrees on a log. It does not by itself provide linearizable reads, exactly once client semantics, membership changes without care, snapshot scheduling or protection against corrupted disks. Each is either an extension in the dissertation or left to the application. When a team says their system uses Raft, the right follow up question is which of these extensions they implemented and how.
A note on vocabulary
Different systems rename the same Raft ideas. ZooKeeper calls terms epochs and its protocol Zab, MongoDB calls them election terms within replica sets, and Kafka KRaft calls the leader the active controller. The mechanics described in this lesson carry over directly, so learning Raft vocabulary once makes the documentation of all of these systems easier to follow and compare.
Raft servers are followers, candidates or the leader. Time is divided into numbered terms with at most one leader each, every message carries a term, higher terms win and the current term and vote are persisted before replying.