Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Explainers/Distributed Systems

How does Raft work? Getting computers to agree

Raft lets a group of computers agree on one ordered log using a leader, majorities and random timeouts, and was designed to be easy to understand.

Derpx0619 Oct · 7 min read
Contents
The idea in one sentenceHow it worksStep 1: why a majorityStep 2: one leader, numbered termsStep 3: leader electionStep 4: copying the logStep 5: the election rule that protects committed entriesStep 6: membership changes and snapshotsWhat the paper foundWhat to be careful aboutSources
0% read · 7 min leftBack to top ↑
On this page0%
Contents
The idea in one sentenceHow it worksStep 1: why a majorityStep 2: one leader, numbered termsStep 3: leader electionStep 4: copying the logStep 5: the election rule that protects committed entriesStep 6: membership changes and snapshotsWhat the paper foundWhat to be careful aboutSources

Many systems keep copies of the same data on several computers, so that one broken computer does not lose anything. The hard part is keeping every copy identical when computers crash and messages get lost. This problem is called consensus: a group of computers agreeing on one ordered list of commands. The widely used older solution, Paxos, was hard to understand and to build from. This paper designs a new algorithm, Raft, with understandability as its main goal.

Paper

In Search of an Understandable Consensus Algorithm. Diego Ongaro and John Ousterhout, Stanford University. USENIX Annual Technical Conference (ATC) 2014. Link: raft.github.io/raft.pdf (the extended version).

The idea in one sentence

Elect one leader, let only the leader decide the order of commands, and count a command as final once more than half of the servers have stored it.

Raft splits the problem into three smaller parts: choosing a leader, copying the leader's list to the others, and rules that keep the list safe when leaders change.

How it works

Terms used below. A server is one computer in the group. A log is a server's ordered list of commands. A majority (also called a quorum) is more than half of the servers.

Step 1: why a majority

Waiting for all servers is no good, since one crashed server blocks everything. So Raft waits for a majority.

Any two majorities of the same group share at least one server. With 5 servers a majority is 3, and two groups of 3 must overlap in at least 3 + 3 - 5 = 1 server. So anything a majority has stored is known to at least one server in any later majority. Both election and commit use this overlap.

Toy example: the overlap

Servers A, B, C, D, E. A, B, C store an entry. Later, a group B, D, E tries to decide something. B is in both groups, so B can tell the new group about the entry. Try any other group of 3 and you will always find A, B or C in it.

Step 2: one leader, numbered terms

Every server is in one of three states. A follower only answers other servers. A candidate is asking for votes. A leader accepts commands from clients and sends them to the followers. The leader sends regular small messages, called heartbeats, so followers know it is alive.

1Follower
2times out, becomes Candidate
3wins a majority of votes
4Leader
A candidate that sees a higher term, or another leader, goes back to follower.

Time is cut into terms, numbered 1, 2, 3 and so on. A term starts with an election. There is at most one leader per term. Every message carries the sender's term. A server that sees a higher term than its own adopts it. A leader or candidate that sees a higher term steps down at once. A server rejects any message with an older term.

Step 3: leader election

A follower that hears no heartbeat before its election timeout assumes the leader is gone. It adds 1 to its term, votes for itself and asks the others for votes. Each server gives one vote per term, to the first valid request it sees. A candidate that gets a majority becomes the leader and sends heartbeats.

If two servers become candidates together, the votes can split and nobody wins. Raft prevents repeats by giving each server a random election timeout, chosen from a range such as 150 to 300 ms. Usually one server times out first and wins before the others start.

Toy example: an election with 5 servers

The leader of term 4 crashes. The random timeouts are S1 = 240 ms, S2 = 170 ms, S3 = 290 ms, S4 = 210 ms and S5 = 260 ms. S2 has the shortest timeout. A majority is 3 of 5.

S1
S2
S3
S4
S5

Term 4 leader crashes. All five are followers with different random timeouts

Step 1 of 5
Toy example. The timeouts are made up for illustration.

The paper tested this. With a fixed timeout, electing a leader took more than 10 seconds in its tests because of repeated split votes. With 5 ms of randomness the median downtime was 287 ms. With 50 ms of randomness the worst of 1000 trials took 513 ms.

Step 4: copying the log

Each log entry holds a command, the term when it was created, and an index (its position). A client sends a command to the leader. The leader appends it to its log and sends it to the followers with an AppendEntries message.

That message also names the index and term of the entry just before the new ones. A follower accepts only if its own log has that same entry there. Otherwise it refuses, and the leader tries again one position earlier, until it finds a point where the logs agree. Then the follower deletes its own entries after that point and copies the leader's.

This one check gives the Log Matching Property: if two logs have an entry with the same index and term, then the two logs are identical up to that entry.

Toy example: repairing a follower

Entries are written as index:term. The leader's log is 1:1, 2:1, 3:2. A follower's log is 1:1, 2:1, 3:1, where its entry 3 is a leftover from an old leader. The leader now appends 4:3.

Leader log: 1:1 2:1 3:2 4:3
Follower log: 1:1 2:1 3:1

The leader sends entry 4:3 and says: the entry before it is 3:2. The follower has 3:1, not 3:2

Step 1 of 4
Toy example. The follower's conflicting entry is removed, never the leader's.

An entry is committed once the leader has stored it on a majority of servers. A committed entry will not be lost. The leader tells the followers which index is committed, and each applies the committed commands, in order, to its own copy of the data.

Step 5: the election rule that protects committed entries

Suppose an entry is committed and then the leader crashes. If a server missing that entry became leader, it would overwrite the entry. Raft prevents this with the election restriction. A server refuses to vote for a candidate whose log is less up to date than its own.

"More up to date" is defined exactly. Compare the terms of the last entries. The log with the later last term wins. If the last terms are equal, the longer log wins.

A committed entry is on a majority. A winning candidate needs votes from a majority. These two majorities overlap, so at least one voter has the entry, and that voter refuses any candidate that lacks it.

Toy example: who can win

A voter has the log 1:1, 2:1, 3:2, 4:3 (last term 3, length 4). Candidate X has 1:1, 2:1, 3:2, 4:2, 5:2. Its last term is 2, lower than 3, so the voter refuses even though X's log is longer. Candidate Y has 1:1, 2:1, 3:2, 4:3, 5:3. Its last term is 3, equal, and it is longer, so the voter votes for Y.

There is one more rule. A leader never commits an entry from an older term just by counting copies. It commits an entry from its own term. That also commits all earlier entries through the Log Matching Property. The paper's Figure 8 shows a case where counting old entries would lose a committed entry.

Step 6: membership changes and snapshots

To change the set of servers, Raft first uses a joint consensus period. Decisions then need a majority of the old set and a majority of the new set. Switching directly could briefly let two separate majorities exist. To stop the log from growing forever, servers save snapshots of their data and drop the log entries behind them.

What the paper found

Raft was tested for understandability and for election speed. The paper also argues safety in a proof, and a TLA+ proof of one key property relies on invariants that were not mechanically checked.

TestResult
User study, 43 students at two universities, quizzes on Raft and Paxos33 scored higher on Raft. Mean score 25.7 on Raft and 20.8 on Paxos, out of 60
Election with 12 to 24 ms timeouts35 ms on average to elect a leader, longest trial 152 ms

The study shows Raft was easier to learn in that sample. The authors say the setup favored Paxos in two ways: 15 of 43 participants had prior Paxos experience, and the Paxos video was 14% longer. They also say self-reported opinions may be biased by knowing the hypothesis. Raft was designed to match multi-Paxos in function and in efficiency, and it is not claimed to be faster than Paxos.

What to be careful about

  • Safety does not need good timing, but availability does. The paper requires broadcast time much smaller than election timeout, which is much smaller than the time between failures. Without a steady leader Raft makes no progress.
  • A strong leader has a cost. All commands go through one leader, which rules out some optimisations. The paper names Egalitarian Paxos as faster in some cases.
  • The minority side stops. A group split in two can only continue on the side that holds a majority.
Key takeaways
  • Consensus means servers agreeing on one ordered log, even when some fail.
  • Any two majorities overlap, and Raft uses this for both elections and commits.
  • Terms, one leader per term and random timeouts prevent two leaders and endless elections.
  • A candidate with a less up-to-date log cannot win, so committed entries are never lost.

Sources

  • Ongaro, Ousterhout. In Search of an Understandable Consensus Algorithm (Extended Version). USENIX ATC 2014. Sections 5 to 9 and Figures 2, 8, 14 and 16.
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Research Papers · 5 minMamba explained: an alternative to TransformersLLM Systems · 7 minWhy do AI chatbots make things up?Inference · 7 minWhat is quantization? Running AI models in 4 bits
Start here
01Why do AI chatbots make things up?02Mamba explained: an alternative to Transformers03What is quantization? Running AI models in 4 bits