Follow the Leader
Raft is a leader-based algorithm. At any given time, exactly one node is the Leader, and all other nodes are Followers. The Leader is the sole entry point for all client requests and is responsible for managing log replication. This single-leader design is what makes Raft simpler than Paxos — there is never ambiguity about who is in charge.
The Three States
Every Raft node is always in one of three states:
- Follower: The default state. Followers passively receive messages from the leader and respond to vote requests from candidates. They do not initiate any communication.
- Candidate: A transitional state. A follower becomes a candidate when it suspects the leader has failed and initiates an election.
- Leader: The active state. The leader handles all client requests and sends periodic heartbeats to maintain authority.
The Election Process
- Heartbeat timeout: Each follower maintains an election timeout — a random duration between 150ms and 300ms (typical values). If a follower does not receive a heartbeat from the leader within this window, it assumes the leader is dead.
- Start an election: The follower increments its term (a monotonically increasing logical clock), transitions to Candidate, votes for itself, and broadcasts
RequestVoteRPCs to all other nodes. - Voting rules: A node grants its vote to a candidate only if: (a) it has not already voted in this term, and (b) the candidate's log is at least as up-to-date as the voter's log. This second condition is critical — it prevents a node with a stale log from becoming leader and overwriting committed entries.
- Win or retry: If the candidate receives votes from a quorum, it becomes the new leader and immediately sends heartbeats to suppress further elections. If no candidate wins (a split vote), all candidates time out and start a new election with a new term. The randomized timeouts make split votes rare and self-resolving.
Terms are the key safety mechanism. Each term has at most one leader. If a node receives a message with a higher term number than its own, it immediately reverts to Follower state, recognizing that a new election has occurred.