Notesdistributed🌱 seedling
Raft
Also: Raft Consensus · Raft Algorithm · Multi-Raft
A leader-based consensus algorithm designed for understandability: elect one leader per term by majority vote, have it replicate a log, and commit entries once a majority stores them. Equivalent to Multi-Paxos in fault tolerance and performance.
Also in: Consensus › Log replication · Consensus › Safety
Why it exists
Paxos is hard to learn and leaves much of a practical system unspecified. Raft splits the problem into leader election, log replication and safety, and enforces a strong leader to cut down the states a reader has to consider.
How it works
- States: follower, candidate, leader. A follower that hears no heartbeat for its randomized election timeout (e.g. 150–300 ms) becomes a candidate for a new term.
- Votes: one per term, only for a candidate whose log is at least as up to date (later last term, or same term and no shorter). A majority wins.
- AppendEntries carries new entries plus the (index, term) before them; followers reject on mismatch, and conflicting uncommitted entries get overwritten. Result: Log Matching.
- Commit: an entry from the current term stored on a majority is committed, along with everything before it. Older-term entries are never committed by counting alone; new leaders commit a no-op.
- Reads: confirm leadership with a majority heartbeat (ReadIndex), or use a lease that assumes bounded clock skew.
- Extensions: PreVote, CheckQuorum, joint-consensus or single-server membership changes.
Trade-offs
| Gains | Costs |
|---|---|
| clear spec, many solid implementations | leader is a throughput and latency bottleneck |
| tolerates f failures with 2f + 1 nodes | every write needs a majority fsync |
| strong leader simplifies log handling | elections block writes; partial connectivity can cause churn |
In practice
- etcd: 100 ms heartbeat, 1,000 ms election timeout by default; PreVote on by default.
- Kafka KRaft, CockroachDB and TiKV (one Raft group per range), Consul, RabbitMQ quorum queues.
Pitfalls
- Acknowledging before fsync: the guarantees assume votes and log entries are durable before replying.
- Election timeouts near the network's worst round trip or GC pauses cause leader churn.
Connections
- Part of
- Consensus
- Learn first
- Quorum
- Contrasts with
- Paxos
- In real systems
- etcdCockroachDBApache KafkaNeo4jRabbitMQ
Recall
These cards are also in your review deck, scheduled with spaced repetition.