How it works · Go library for agreement between servers

How etcd’s Raft works

Several servers keep the same list of changes in the same order, even when some of them crash. This is the library Kubernetes’ database is built on.

Think of it like this. A committee with one chair. Only the chair proposes, a change counts once most members have written it down, and if the chair goes quiet the members elect another.

The library is only the rules. Your program sends the messages and writes the disk, and tells the library what happened.

  1. The state machine The rules: what a follower, a candidate and a leader each do with every message. raft.go
  2. The log The ordered list of changes, and how far along it everyone has agreed. log.go
  3. The node What your program talks to: propose a change, tick the clock, collect what to do next. node.go
  4. Progress The leader’s note of how far each follower has got. tracker/progress.go
  5. Storage The few things the library asks of your disk. storage.go

Follow one thing through: the leader stops answering

  1. Each follower counts clock ticks since it last heard from the leader.raft.go
  2. Each waits a slightly different, random time, so they rarely all stand at once.raft.go
  3. The first to run out becomes a candidate, moves to a new term, votes for itself and asks the others.raft.go
  4. A server votes yes only if the candidate’s log is at least as up to date as its own.log.go
  5. With votes from more than half, it becomes leader.raft.go
  6. It starts sending heartbeats, and the others go back to following.raft.go

The clever bit: No network, no disk, no clock

The core never sends a message, writes a file or reads the time. Your program calls Tick() when time passes and Step() when a message arrives, and gets back a Ready: entries to save, messages to send, changes that are now safe to apply. Because the rules are a plain function of what went in, a test can replay any order of crashes and lost messages exactly, which is how a system this subtle gets trusted.

node.go

Words you’ll meet

term
A numbered period with at most one leader. A higher term always wins.
log
The ordered list of changes every server keeps.
quorum
More than half the servers. Any two quorums share at least one server.
committed
Stored on a quorum, so it can never be lost.

Good questions to ask next

Open etcd’s Raft in Bento and ask them there. It reads the project in your browser, draws its parts as a map you can press, and opens the real code behind each one.

Take etcd’s Raft apart yourself.

Free and open source. Nothing to install, no account.

Ideas inside it

More projects, taken apart

How React worksYou describe what the screen should look like. React works out the smallest set of changes to make it so.How Express worksA request comes in and passes through a list of functions, in order, until one of them answers.How Flask worksYou write a function for each address. Flask finds the right one for a request, calls it, and turns what it…How Redis worksOne big dictionary held in memory, answered by a single loop that takes one command at a time.How nanoGPT worksText becomes numbers, a model learns to guess the next number, and guessing again and again writes new text.How micrograd worksEvery number remembers how it was made, so the answer can be traced back to say how much each input mattered.How SQLite worksYour SQL is compiled into a small program, and a tiny virtual machine runs it against one file on disk.How FastAPI worksYou write a function with type hints. FastAPI reads the hints to check the request, convert the values and…How Vue worksVue notices which data each part of the screen reads, so when that data changes it knows exactly what to…How Requests worksOne friendly line, requests.get(url), on top of the fiddly work of opening connections and speaking HTTP.How Redux worksAll of an app’s state lives in one place, and the only way to change it is to describe what happened.How Git worksEvery version of every file is stored under a name made from its own contents, and a commit is a small note…How Preact worksThe same idea as React, in a few kilobytes: you describe the screen, and Preact changes only what is…How Zustand worksA store is a value, a way to change it, and a list of who wants to know. The whole core is about a hundred…How Axios worksOne way to make web requests that works the same in a browser and in Node, with hooks before and after every…How htmx worksAny element can ask the server for a piece of HTML and put it into the page. You write attributes, not…How LevelDB worksA store for keys and values that makes writing fast by never changing a file: it only adds, and tidies up…

Written by hand from the code as it was on 2026-11-19 (commit ae830f4). etcd’s Raft belongs to its authors; this page is an independent explanation. Spot a mistake? Tell us.