How Raft Consensus Works: Leader Election, Log Replication and Quorums
How etcd, Kafka and modern databases keep many servers agreeing on one truth, even when machines crash

Five servers, one source of truth. One loses power, another drops off the network for a few seconds, and every client still sees the same data in the same order. The machinery behind that in Kubernetes, Kafka and many modern databases is a consensus algorithm, and the most popular one today is Raft.
This guide explains how Raft consensus works: how it elects a leader with randomized timers, replicates a log, stays safe when servers crash, and why your etcd cluster should have three or five members rather than four.
Why distributed systems need consensus
One copy of an important service is a single point of failure. The standard fix is a replicated state machine: several servers each keep the same state (say, a key-value store) plus a log of commands like set x to 3. If every server applies the same commands in the same order, every copy ends up identical.
The hard part is agreeing on that order while machines crash, restart and lose packets. That agreement is what a consensus algorithm provides. The Raft project site sums up the contract: the cluster keeps working as long as a majority of servers are up, and if too many fail it stops making progress rather than returning a wrong answer.
The classic answer was Leslie Lamport's Paxos, which is famously hard to follow. Diego Ongaro and John Ousterhout at Stanford designed Raft with understandability as the top goal; their paper, In Search of an Understandable Consensus Algorithm, won a Best Paper Award at USENIX ATC 2014. In their user study, 33 of 43 students scored better on Raft questions than on Paxos questions after learning both.
The building blocks: roles, terms and two RPCs
- Three roles. At any moment a server is a leader, a follower or a candidate. Normally there is exactly one leader and everyone else is a passive follower. Clients talk only to the leader.
- Terms. Time is divided into numbered terms. Each term starts with an election, and at most one leader can win any term. Terms act as a logical clock: a server that sees a higher term than its own updates immediately and, if it was leading, steps down.
- Two messages. The core algorithm needs only two remote procedure calls.
RequestVoteis sent by candidates asking for votes.AppendEntriesis sent by the leader to replicate log entries, and an empty one doubles as a heartbeat.
Leader election with randomized timers
Every follower runs an election timer. While heartbeats keep arriving, the timer keeps resetting. If it runs out, the follower assumes the leader is gone and starts an election:
- It increments its term and becomes a candidate.
- It votes for itself and sends
RequestVoteto every other server. - Each server grants at most one vote per term, first come, first served.
- A candidate that collects votes from a majority of the full cluster becomes leader and immediately starts sending heartbeats.
What stops two candidates from splitting the vote forever? Randomness. Each server picks its timeout at random from a range; the paper suggests something like 150 to 300 milliseconds. Usually one server times out first and wins before the others wake up; after a split vote, candidates simply wait a fresh random interval and retry.
It takes surprisingly little randomness. In the authors' tests, identical timeouts led to elections taking over ten seconds, while just 5 ms of spread cut median downtime to 287 ms.
# Simplified follower loop (illustrative pseudocode)
timeout = random_between(150, 300) # milliseconds
on heartbeat_or_append(term):
if term >= current_term:
current_term = term
reset_timer(timeout)
on timer_expired():
current_term += 1
become(CANDIDATE)
vote_for(self)
send_to_all(RequestVote(current_term, last_log_index, last_log_term))
Log replication: how writes become committed
Once elected, the leader handles every client write the same way. It appends the command to its own log, sends it to followers with AppendEntries, and waits. As soon as the entry is stored on a majority of servers it is committed: the leader applies it, answers the client, and tells followers the new commit point in later messages so they can apply it too.
So a five-server cluster needs only three copies (the leader plus two followers) to finish a write, and one or two slow machines don't hold anyone up.
The consistency check
Every AppendEntries message carries the index and term of the entry just before the new ones. A follower without a matching entry at that position rejects it. The leader backs up one entry and retries until it finds where the two logs agree; the follower then drops any conflicting entries after that point and copies the leader's. Data only ever flows from leader to followers.
If this sounds familiar, it should: a Raft log plays the same role across machines that a write-ahead log plays inside one database, as covered in our guide to how database transactions work.
The safety rules that make Raft correct
What stops a follower that missed some committed entries from winning an election and overwriting them? Two extra rules.
- Election restriction. A
RequestVoteincludes the term and index of the candidate's last log entry, and a server refuses to vote for anyone whose log is less up to date than its own. Any winner needs a majority, and any committed entry already lives on a majority, so at least one voter would block a candidate that is missing it. - Only count replicas for your own term. A new leader never declares an older entry committed just because it sits on a majority of servers. It commits an entry from its current term, and everything before it becomes committed along with it.
Together these give Raft its core promise: once any server has applied a command at a given log position, no server will ever apply a different command there. Safety also never depends on clocks. Slow networks or skewed time can delay progress but can't produce wrong results, which is welcome given how unreliable computer clocks can be.
Quorums: why clusters have 3, 5 or 7 nodes
Every decision in Raft needs a majority, so quorum math decides how many failures you can survive. For N voting members, the majority is ⌊N/2⌋ + 1.
| Cluster size | Majority needed | Failures tolerated |
|---|---|---|
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 2 | 1 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 6 | 4 | 2 |
| 7 | 4 | 3 |
Adding a fourth member to a three-node cluster raises the majority without raising fault tolerance: you pay for a machine and get nothing back. That is why etcd's documentation recommends odd sizes and says a cluster probably shouldn't exceed seven nodes. Kubernetes' own guidance recommends five etcd members in production.
Timing matters too: message round trips should be far shorter than the election timeout, and Raft messages usually involve a disk write, so slow disks can delay heartbeats and trigger needless elections. It's the same lesson as a load balancer's health checks: tune the failure detector too aggressively and healthy nodes get kicked out.
Where you will find Raft in practice
Raft quietly underpins a lot of infrastructure. Kubernetes stores all cluster data in etcd, a Raft-based key-value store. Apache Kafka replaced ZooKeeper with KRaft, its own Raft-based metadata quorum, and Kafka 4.0 removed ZooKeeper support entirely. TiDB (through TiKV), YugabyteDB, Redpanda and NATS JetStream also use Raft for replication, and the Raft website lists dozens of open-source implementations in many languages.
Key takeaways
- Raft keeps a replicated log identical across servers so they behave like one reliable machine.
- A single leader handles writes; randomized election timeouts pick a new one quickly when it fails.
- An entry is committed once a majority stores it, so a five-node cluster survives two failures.
- The election restriction and the current-term commit rule stop committed data from being lost.
- Use odd cluster sizes; a fourth or sixth node adds cost without extra fault tolerance.
Frequently asked questions
Is Raft better than Paxos?
Raft offers the same fault tolerance as multi-Paxos with comparable performance. Its main advantage is clarity: it is easier to learn, explain and implement correctly, which is why many newer systems chose it.
What happens to writes while there is no leader?
They pause. Clients time out and retry, typically after a new leader is elected a few hundred milliseconds later. Committed data is not lost during the switch.
Can a Raft cluster keep working if the network splits?
Only the side holding a majority can elect a leader and commit writes. The minority side stops accepting writes until the network heals, which is exactly what prevents split brain.
Sources
- Ongaro and Ousterhout: In Search of an Understandable Consensus Algorithm (extended version)
- The Raft Consensus Algorithm project site
- etcd documentation: Frequently asked questions
- Kubernetes documentation: Operating etcd clusters for Kubernetes
- Instaclustr: Kafka 4.0 key changes, including the move to KRaft
- Wikipedia: Raft (algorithm)












Comments
No comments yet — be the first to share your thoughts.
Join the discussion