Raft is a consensus algorithm: it makes a cluster of servers agree on the same ordered sequence of commands, even as machines crash and restart. Its trick is to be *understandable* — it breaks consensus into two clean pieces, leader election and log replication, glued together by one rule: nothing counts until a majority has written it down.
Terms and roles
Time is divided into terms — numbered periods that only ever increase. Each term begins with an election. Every server is in one of three states: follower (passive, default), candidate (running for leader), or leader (handles all client requests). A term has *at most one* leader.
Leader election
1Followers expect regular heartbeats from the leader. If a randomized election timeout passes with none, a follower becomes a candidate.
2It increments the term, votes for itself, and sends RequestVote to everyone.
3Each server votes at most once per term, and only for a candidate whose log is at least as up-to-date as its own.
4A candidate that collects votes from a majority becomes leader and starts sending heartbeats.
5Randomized timeouts make split votes rare — if one happens, the term ends with no leader and a fresh election begins.
Log replication
1Clients send commands to the leader, which appends each as an entry to its log.
2The leader sends AppendEntries to followers to copy the entry.
3Once a majority have stored the entry, the leader marks it committed and applies it to its state machine.
4The leader tells followers the new commit index; they apply committed entries in the same order.
Why majority quorum is the whole game
Any two majorities of the same cluster must overlap in at least one server. So a committed entry — stored on a majority — is guaranteed to be present in any future majority that elects the next leader. That overlap is why the cluster can never lose committed history or disagree about it.
Majority of all nodes, not of the survivors
A 5-node cluster needs 3 votes to act and tolerates 2 failures. With only 2 nodes alive it cannot form a majority, so it stops accepting writes rather than risk a split-brain. Availability is deliberately sacrificed to keep consistency.