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 1Server 1leaderServer 2Server 2Server 3Server 3Server 4Server 4Server 5Server 5
  • 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
  1. A leader keeps watch. 5 servers keep copies of one log; the leader, server 1, sends heartbeats to say it is alive.
  2. The leader stops. When server 1 stops, its heartbeats stop, and each follower's election timer starts running out.
  3. Random timers. Each timer is random, between 150 and 300 ms, so one follower usually runs out first.
  4. Ask for votes. Server 5's timer ends first, at 162 ms, so it becomes a candidate and asks the others for votes.
  5. A majority wins. It votes for itself and gets 2 more votes: 3 of 5, a majority, so it becomes the leader.
  6. Commit by majority. A new write is committed once a majority of servers store it, so the cluster survives 2 servers down.
  7. 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

  1. One leader takes every write and copies it to the others, its followers.
  2. If heartbeats stop, the follower whose random timer ends first asks the others for votes.
  3. A candidate needs a majority, 3 of 5, so two can never win the same election.
  4. A write is committed once a majority store it, so the cluster survives a minority being down.

Go deeper in the Raft leader election lab →

In plain words

Say five servers each keep a copy of one log of writes. Server 1 is the : it takes every write, and it sends 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 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 , and it leaves the 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 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

Live simulationOpen the full lab →

Lab · Distributed data

Raft leader election

Five nodes agree on one log. Crash the leader, split the network, or set the election timers close together. See when a new leader wins and when a write is safe.

Try it

Lab time 3.0 s
Node 1, Leader, LEADERNode 1Leaderterm 1 · log 1LEADERNode 2, FollowerNode 2Followerterm 1 · log 1Node 3, FollowerNode 3Followerterm 1 · log 1Node 4, FollowerNode 4Followerterm 1 · log 1Node 5, FollowerNode 5Followerterm 1 · log 1
  • Node 1 — Leader — healthy — LEADER — term 1 · log 1
  • Node 2 — Follower — healthy — term 1 · log 1
  • Node 3 — Follower — healthy — term 1 · log 1
  • Node 4 — Follower — healthy — term 1 · log 1
  • Node 5 — Follower — healthy — term 1 · log 1
  • Vote request
  • Yes, or entries stored
  • No, mismatch, or lost
  • Heartbeat
  • Log entries
Leader
Node 1
Healthy: term 1
Highest term
1
Elections
2
Split votes
0
Time without a leader
1.7 s
Writes done
0
0 waiting · 0 dropped · 0 committed but never confirmed · 0 no leader

Numbers add up:At most one leader per term; 1 committed entries never change; every write told "done" is committedThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Faults

You can also click a node to crash or restart it. "Cut off the leader" leaves it with one follower, away from the other three.

Writes and elections
0 a second
Election timeout

A wide random range, as Raft recommends.

Each node's log

Node1
Node 1Leader, term 1no-opT1 ✓, committed
Node 2Follower, term 1no-opT1 ✓, committed
Node 3Follower, term 1no-opT1 ✓, committed
Node 4Follower, term 1no-opT1 ✓, committed
Node 5Follower, term 1no-opT1 ✓, committed

Each cell is one log entry: the write, and the term it was added in. ✓ means this node knows it is committed: stored on at least 3 of 5, by a leader that counted it. An older entry on 3 nodes gets its ✓ only once a new leader commits an entry of its own. A dashed cell is stored but not committed yet.

What just happened

  1. 1.7sOK: Node 1 won 3 of 5 votes and leads term 1. It adds an empty entry to mark its term.
  2. 1.6sInfo: Node 2's timer ran out. It starts an election for term 1 and votes for itself.
  3. 1.5sInfo: Node 1's timer ran out. It starts an election for term 1 and votes for itself.
  4. 0.0sInfo: Five nodes start as followers. The first timer to run out starts an election.
What this model simplifies
  • Times are 10× real Raft defaults: heartbeats every 0.5 s and election timeouts of 1.5–3 s, against about 50 ms and 150–300 ms in real systems.
  • Every message takes 0.1 s and is never lost, except when it is sent to a crashed node or across the split.
  • A restarted node keeps its term, vote and log, as Raft keeps them on disk. Here it also remembers what was committed, which is safe but not required.
  • A leader sends new entries with its next heartbeat, so a write can wait up to 0.5 s before it is copied. Real Raft sends at once.
  • Membership changes, snapshots and pre-vote are left out.
  • The client sends each write to the node it last knew as leader, and gives up if that node is not leading. Real clients retry and find the new leader.

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.

  1. Leader election

    One server is the leader. It sends a heartbeat to the others, the followers, at a short, steady interval. Each 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.

  2. Win a majority

    S1, leader, FAILED, NOWS1leaderstoppedFAILEDS2, followerS2followertimer 230 msS3, followerS3followertimer 160 msS4, followerS4followertimer 280 msS5, followerS5followertimer 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 3

    Step 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
    Leader
    none
    Watch: an election is needed
    Votes for S3
    0 of 5
    needs 3

    Step 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.

  3. 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.

  4. Commit

    Pick a case to follow below

    With this case: 3 of 5 servers stored x = 7, so the leader committed it and answered the client.

    S1, leader, NOWS1leaderlog: x = 7NOWS2, followerS2followerS3, followerS3followerS4, followerS4followerS5, followerS5follower
    • S1 — leader — healthy — NOW — log: x = 7
    • S2 — follower — healthy
    • S3 — follower — healthy
    • S4 — follower — healthy
    • S5 — follower — healthy
    • Log entry
    Step 1 of 4

    Step 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 = 7
    1 of 5
    needs 3
    Committed
    no
    the client is still waiting

    Step 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 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

In Raft, when is a log entry committed?

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

Why do Raft clusters usually have an odd number of servers, such as 3 or 5?

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 ."
  • 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?

A Raft cluster has 5 servers. A network break leaves 2 on one side and 3 on the other. What happens?

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

A Raft cluster has 7 servers. How many can be down while it still takes writes?

Apply it

In a 5-server cluster, the leader stores a new entry and one follower copies it. Then the leader crashes. Was the entry committed?

Judge a design

For safety, a team runs Raft on 4 servers, 2 in each of 2 data centres. The link between the data centres breaks. What happens, and what is better?

Next: distributed locks and fencing tokens, how to let one worker at a time do a job across many machines.

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.