Consensus made easier to understand: one leader, a replicated log, and elections.
Raft is a way for a group of servers to agree on the same list of changes (a log), even when some servers crash. It was designed to be easier to understand than Paxos.
Imagine three or five database servers that must all hold the same data. If two clients send different changes to different servers at the same moment, the copies would disagree. Raft solves this by making sure every server applies the same changes in the same order. This idea is called a replicated state machine.
Raft splits the hard problem into three smaller ones: leader election (who is in charge?), log replication (how does the leader share changes?) and safety (how do we never lose a committed change?).
Every server is always in one of three roles: follower (passive, answers the leader), candidate (asking for votes) or leader (sends changes and heartbeats). Time is cut into numbered terms. A term has at most one leader. A higher term always beats a lower one.
| Step | What happens | Why |
|---|---|---|
| Append | The leader adds the command to its own log with the current term | The leader decides the order |
| Replicate | It sends AppendEntries to followers. Each follower checks that its previous entry matches | A mismatch means the logs differ, so the leader backs up and fixes it |
| Commit | When a majority stores the entry, the leader marks it committed | A majority can never be forgotten by a future leader |
| Apply | Each server applies committed entries to its state machine in order | All servers end in the same state |
| Tell the client | The leader replies after commit | The client knows the change is safe |
| Raft | Paxos | |
|---|---|---|
| Main idea | Strong leader and a replicated log | Agree on one value at a time, leader is optional |
| Understanding | Built to be teachable | Famously hard to follow |
| Full system | The paper describes the whole thing, including membership changes | Basic Paxos leaves out much. Multi-Paxos is under-specified |
| Use in practice | etcd, Consul, CockroachDB, TiKV | Google Chubby and Spanner (Multi-Paxos styles) |
Each square is a log entry and the number is the term it was written in. Solid = committed (on a majority). Dashed = not committed yet.
A simplified model: messages arrive instantly and election timers are fired by you (real servers use random timers of about 150 to 300 ms). The rules for voting, terms and commits follow the Raft paper.
| Term | Meaning in easy words |
|---|---|
| Replicated state machine | Many servers that apply the same commands in the same order and so end up identical |
| Log | The ordered list of commands each server keeps |
| Term | A numbered period of time with at most one leader. A higher term wins |
| Leader | The one server that accepts client changes and copies them to others |
| Follower | A passive server that answers the leader and votes |
| Candidate | A server that is asking for votes to become leader |
| Election timeout | How long a follower waits without a heartbeat before it starts an election |
| Heartbeat | A regular message from the leader that says it is still alive |
| AppendEntries | The message a leader uses to send new log entries (or an empty heartbeat) |
| Quorum / majority | More than half of the servers. 3 of 5, or 2 of 3 |
| Committed | An entry stored on a majority. It will never be lost or changed |
| Choice | We gain | We lose |
|---|---|---|
| More servers (5 instead of 3) | Survive more failures | More messages and slower commits |
| Short election timeout | Fast recovery from a dead leader | False elections when the network is slow |
| Long election timeout | Calm cluster | A longer outage when the leader really dies |
| Read from followers | Spread read load | Possibly stale data |
| Read through the leader | Fresh data | More load on one server |
| Spread across regions | Survive a whole region failing | Every write pays the long distance |