Stage 3 · Distributed data · Hard · 22 min
Consensus and Raft
How a group of servers agrees on one order of writes, even when some fail. Follow Raft: a leader, an election by majority vote, and a log copied to followers.
What each interview level is expected to know
Learn first: CAP, PACELC and quorums
After this lesson you can:
- Follow a Raft election, and explain why a leader needs a majority.
- Explain how the leader copies its log and when an entry counts as committed.
- Predict what each side of a network split can do.
Start here
See it in 60 seconds
Electing a new leader
Raft keeps servers agreeing on one log: a leader sends heartbeats, a random timer picks who stands for leader, and a majority decides.
Scene 1 of 6 · A leader keeps watch
5 servers keep copies of one log; the leader, server 1, sends heartbeats to say it is alive.
- Server 1 — healthy — leader
- Server 2 — healthy
- Server 3 — healthy
- Server 4 — healthy
- Server 5 — healthy
- Votes for the new leader, of 5
- not shown yet
- Servers up, of 5
- not shown yet
- A leader keeps watch. 5 servers keep copies of one log; the leader, server 1, sends heartbeats to say it is alive.
- The leader stops. When server 1 stops, its heartbeats stop, and each follower's election timer starts running out.
- Random timers. Each timer is random, between 150 and 300 ms, so one follower usually runs out first.
- Ask for votes. Server 5's timer ends first, at 162 ms, so it becomes a candidate and asks the others for votes.
- A majority wins. It votes for itself and gets 2 more votes: 3 of 5, a majority, so it becomes the leader.
- Commit by majority. A new write is committed once a majority of servers store it, so the cluster survives 2 servers down.
- Now you try. How many of the 5 servers are down? The new leader, server 5, stays up. For example: 3 of 5 servers are up and a majority is 3, so the leader can still commit writes. 2 down is the most it can survive; a 6-server cluster also survives only 2, so sizes are odd.
Scene 1 of 6: A leader keeps watch. 5 servers keep copies of one log; the leader, server 1, sends heartbeats to say it is alive.
Key points
- One leader takes every write and copies it to the others, its followers.
- If heartbeats stop, the follower whose random timer ends first asks the others for votes.
- A candidate needs a majority, 3 of 5, so two can never win the same election.
- A write is committed once a majority store it, so the cluster survives a minority being down.
In plain words
Say five servers each keep a copy of one log of writes. Server 1 is the (The one copy that takes every write for its data. The other copies, the followers, copy from it. Kafka has one leader for each partition.): it takes every write, and it sends (A small "I am alive" message sent again and again at a steady interval, to show that a server or connection still works. The interval depends on the system: milliseconds in some, seconds in others.) to the other four to show it is alive. Then server 1 stops. Each of the others has a timer set to a random time between 150 and 300 ms. The server whose timer ends first asks the rest for votes. With 3 votes out of 5, a majority, it becomes the new leader. Getting a group of servers to agree like this is called consensus. Raft is one widely used set of rules for it.
Once elected, the leader handles each write in two steps. It adds the write to its own log and sends it to the followers. When a majority, 3 of 5 here, have saved it, the write is (Making a change final. A committed database write counts and will not be undone. In Git, a commit is one saved set of code changes.) and the client gets its answer. If 3 servers are down, no majority exists, so writes stop instead of risking two leaders. A term number, raised at each election, lets the others ignore an old leader that comes back.
The problem it solves
When the leader seems to be gone, another server must take over. But a slow network looks the same as a dead leader. Two servers may each decide to lead, and each then accepts different writes. This is called (Two servers or regions each believe they are the leader, and both accept writes at once. It usually happens during a network partition.), and it leaves the (A group of servers that work together as one system.) with two histories that disagree.
Why it matters
Some facts must have one answer across a whole cluster: who the leader is, who holds a lock, what the current settings are. Consensus is how several servers agree on such a fact, even when some of them crash. You will rarely build it yourself. But tools you rely on, such as etcd and ZooKeeper, are built on it.
A worked example
In a 7-node Raft cluster, a candidate needs a majority to become leader. A majority is more than half of 7, so it takes 4 votes. If 3 (One machine, or one running copy of a program, in a group that works together.) go offline, the remaining 4 can still reach that 4-vote majority and elect a leader. If a 4th node also goes offline, only 3 remain, no majority is possible, and writes freeze until enough nodes return.
Think of it like…
Think of a club of five that keeps one minute book. A decision counts only when three of the five have written it in their own copy. If the chair goes missing, the members vote, and the new chair needs three votes.
Where this analogy stops working
A majority for every decision is the useful part. But club members sit in one room and can see the chair. Servers know only the messages that arrive, so a slow leader looks like a dead one. A new election may start while the old leader still runs. A term number then tells the others whom to ignore.
Try it
See it working
Mechanics
How it works
The pictures follow this page's example: five servers, with S1 as the leader. The first shows how a new leader is chosen when S1 stops. The second shows when a write counts as done.
Leader election
One server is the leader. It sends a heartbeat to the others, the followers, at a short, steady interval. Each (A copy that takes its writes from the leader instead of from clients.) runs a timer set to a random, longer time, such as 150 to 300 ms. If no heartbeat arrives before the timer ends, that follower becomes a candidate and asks for votes.
Win a majority
S1, leader, FAILED, NOW S1 leader stopped FAILED S2, follower S2 follower timer 230 ms S3, follower S3 follower timer 160 ms S4, follower S4 follower timer 280 ms S5, follower S5 follower timer 200 ms - S1 — leader — FAILED — NOW — stopped
- S2 — follower — healthy — timer 230 ms
- S3 — follower — healthy — timer 160 ms
- S4 — follower — healthy — timer 280 ms
- S5 — follower — healthy — timer 200 ms
- Vote
Step 1 of 3Step 1 of 3 · S1
Failed: S1, the leader, stops
- What happens
- S1 stops sending heartbeats. The four followers notice nothing yet.
- Why
- A follower only learns the leader is gone when heartbeats stop arriving.
- What changes
- Each follower's election timer is running: S3 160 ms, S5 200 ms, S2 230 ms, S4 280 ms.
- Next
- At 160 ms, S3's timer runs out first
LeadernoneWatch: an election is neededVotes for S30 of 5needs 3Step 1 of 3: S1. S1, the leader, stops. S1 stops sending heartbeats. The four followers notice nothing yet.
A candidate becomes leader only with votes from more than half of the servers: 3 of 5. Each server gives at most one vote per election round, called a term. Two candidates cannot both win a term, because two majorities always share a server.
In the picture, the followers' timers happened to get 230, 160, 280 and 200 ms. S3's runs out first, at 160 ms, so S3 asks for votes. S5 and S2 have not voted this term, and with its own vote S3 has 3 of 5.
The random times are the trick. If every timer ran out together, every follower would ask at once, split the votes, and nobody would win. Spread-out timers make one candidate almost always ask first.
Copy the log
Clients send commands to the leader. The leader adds each command to its log, an ordered list, and sends the new entry to every follower.
Commit
Pick a case to follow belowWith this case: 3 of 5 servers stored x = 7, so the leader committed it and answered the client.
S1, leader, NOW S1 leader log: x = 7 NOW S2, follower S2 follower S3, follower S3 follower S4, follower S4 follower S5, follower S5 follower - S1 — leader — healthy — NOW — log: x = 7
- S2 — follower — healthy
- S3 — follower — healthy
- S4 — follower — healthy
- S5 — follower — healthy
- Log entry
Step 1 of 4Step 1 of 4 · S1
Done: S1, the leader, adds x = 7 to its log
- What happens
- The client sends the command to the leader, which adds it as the next entry in its log.
- Why
- Every write goes through the leader, so there is one order of entries.
- What changes
- It is stored on 1 server. That is not yet safe.
- Next
- S2 stores the entry: 2 of 5
Servers that stored x = 71 of 5needs 3Committednothe client is still waitingStep 1 of 4: S1. S1, the leader, adds x = 7 to its log. The client sends the command to the leader, which adds it as the next entry in its log.
What these pictures simplify. These pictures use five servers, stated timer values and one term. Real Raft also checks that a candidate's log is at least as up to date as the voter's before it gives a vote. The Raft lab above runs elections and failures live.
When more than half of the servers have stored the entry, the leader marks it committed. It applies the command and answers the client. A committed entry survives a leader crash, because any new leader must already have it.
In the picture, the leader adds x = 7 to its log. In the first case S2 and then S3 store it, so 3 of 5 servers have it: a majority. The leader commits it at that moment and answers the client, and S4 gets its copy after.
In the second case only S2 answers. Two copies are not a majority, so the leader keeps trying and the client waits. It gets no answer, but it is never told something that could be lost.
Use it when
- Small, important data that every server must agree on, such as settings or the list of running services: etcd and Consul keep it with Raft.
- Choosing one leader for a database or a job scheduler: a majority vote stops two servers from both taking the role.
- A lock service shared by many workers: who holds each lock must be one agreed fact.
Skip it when
- Large files or a flood of small events, such as video chunks or clicks: each write waits for a majority to store it. Keep such data in storage built for it.
- Data where a short disagreement does no harm, such as a like count: simpler (Continuously copying every write from a primary database to one or more replicas, so each one ends up with the same data.) is faster and keeps working without a majority.
- Writing your own Raft for a product feature: use a tested service such as etcd or ZooKeeper. The rare cases are easy to get wrong.
Check yourself
Judgement
Trade-offs and how it fails
What you give up
- Safety
- Raft allows at most one leader per term, and commits an entry only after a majority has stored it. So two sides of a split cluster cannot both commit writes.
- Throughput
- Every write goes through the leader and waits for a majority to store it. That is at least one network round trip per write, so writes are slower than in a store that confirms after one copy.
- Availability
- A cluster of 5 keeps working with 2 servers down. With 3 down there is no majority, so it takes no writes until one of them returns.
Failure modes
- No majority: in a cluster of 5, three servers go down. The 2 that remain cannot elect a leader or commit a write, so the cluster takes no writes until a third returns.
- Split votes: two followers time out at the same moment, each votes for itself, and neither reaches a majority. Random timers make a repeat unlikely, so a later round usually has one clear winner.
- A stale leader: a leader cut off from the majority may still believe it leads. It cannot commit a write. It can still answer a read with old data, unless reads are also checked with a majority.
In the real world
- Kubernetes's documentation calls etcd the backing store for all cluster data. etcd uses Raft.
- Kafka 4.0 (2025) runs without ZooKeeper. It manages cluster metadata with KRaft, its own Raft-based protocol. Source: Apache Kafka 4.0.0 release announcement (2025) (checked 14 October 2026)
- The Raft paper (Ongaro and Ousterhout, 2014) says Raft was designed to be easier to understand than Paxos, an older consensus protocol. It suggests election timers of 150 to 300 ms, the range used on this page. Source: The Raft paper (2014) (checked 14 October 2026)
Trade-offs to be ready to discuss: Raft vs paxos
Check yourself
Interview
What interviewers look for
- Explain why clusters usually have an odd size, such as 3 or 5: "A cluster of 5 survives 2 failures. A cluster of 6 also survives only 2, so the sixth server adds cost and no safety."
- Give the majority rule with numbers: "A majority is more than half. That is 2 of 3, 3 of 5 and 4 of 7."
- Say what the smaller side of a split does: "It cannot elect a leader or commit, so it refuses writes. That is the CP choice from (When copies of data cannot reach each other, a system must choose. It can refuse some requests and stay consistent, or answer them and risk old data. CAP stands for consistency, availability and partition tolerance.)."
- Say you would use etcd or ZooKeeper for leader election and locks, and not write Raft yourself.
Say it out loud: Why do consensus clusters such as etcd and ZooKeeper usually have an odd number of servers: 3, 5 or 7?
Expected depth by interview level
Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.
- JuniorBeyond this level
- Not usually expected.
- MidBeyond this level
- Know that leader election exists and why.
- SeniorExpected
- Leader election, quorums, and what Raft guarantees.
- StaffExpected
- When to rely on a coordination service (etcd, ZooKeeper) instead of building one.
Practice
Apply it
Three harder questions
The first two use the idea on a new case. The last asks you to judge a design.
Apply it
Apply it
Judge a design
Go deeper
Sources and further reading
First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperIn Search of an Understandable Consensus Algorithm (2014)Ongaro and Ousterhout, USENIX ATCThe Raft paper: leader election, log replication and safety, with the full rules on one page.
- Official docsThe Raft Consensus Algorithmraft.github.ioA live visual of Raft and a list of real implementations.