⬡

An interactive deep-dive into the Raft consensus algorithm — leader election, log replication, snapshots and truncation — with two live simulations you can explore and break.

·13 min read

Distributed systems are hard because machines fail. A server can crash mid-write, a network can partition, a message can vanish. The question at the heart of fault-tolerant systems is: how do a group of servers agree on anything when any of them can fail at any time?

That question is answered by consensus algorithms, and the most influential of them is Raft (Ongaro & Ousterhout, 2014). Raft was designed from the ground up to be understandable — its structure mirrors the way a human would solve the problem: elect a leader, have the leader hand out log entries, agree on what's committed. It is now the backbone of etcd, CockroachDB, Consul, and many cloud systems.

This post is two live simulations deep:

  1. A cluster view — nodes as circles, messages as balls flying between them, and a replication log panel
  2. A log view — the Raft log itself as a grid of entries, where you can watch commitIndex advance, snapshots compact the log, stale tails get truncated, and lagging nodes catch up
💡

The visualizations below are live simulations. Click any node to disconnect or reconnect it. Hit 📨 Client write to send a request — watch it travel from the client icon to the leader, then propagate outward as AppendEntries RPCs. The Replication Log panel on the right shows each node's log in real-time: filled squares are committed entries, dashed squares are uncommitted. Two details to watch for: a box only turns committed when the quorum of acknowledgements reaches it (the leader announces it on the next round — replication alone isn't enough), and each log keeps only the last 5 committed entries — older ones drop below the truncation index and fade away, exactly as real implementations compact their logs. Use ⚡ Partition 3|2 to split the cluster and observe what each half can do.

Raft

Raft decomposes the consensus problem into three largely independent sub-problems:

  1. Leader election — exactly one server must be authoritative at any term
  2. Log replication — the leader accepts writes and replicates them to followers
  3. Safety — once an entry is committed, it can never be overwritten or lost

Cluster roles

Every Raft server is always in one of three states:

StateColourDescription
Follower🔵 BluePassive; just applies entries from the leader
Candidate🟡 YellowCampaigning to become leader
Leader🔴 RedHandles all client writes; replicates to followers

Leader election and the election timeout

Each follower has an election timeout — the arc you see growing around each blue node. If that timer expires without receiving a heartbeat from the leader, the follower assumes the leader is dead and promotes itself to Candidate.

The candidate increments its term number (visible inside the node as v1, v2, …), votes for itself, and fires RequestVote (purple circles) at every other server. A server grants its vote if:

  • the candidate's term is at least as large as its own, and
  • it hasn't already voted for someone else this term

If the candidate collects votes from a majority of the cluster, it wins and becomes the new leader — and immediately starts sending AppendEntries heartbeats (blue circles) to reset everyone's timers and prevent a new election.

The randomised timeout window (each follower picks a different timeout) ensures that in most cases only one candidate starts an election at a time, avoiding perpetual split votes.

Raft cluster

Replication Log · keeps 5 past commit

No entries yet
Speed1×
LeaderFollowerCandidateDisconnected
Client writeRequestVoteVoteGrantedVoteDeniedAppendEntriesAppendEntriesOK

No events yet…

Try it: disconnect the red leader node (click it). You'll see the blue nodes' timer arcs drain. One runs out first, turns yellow, and fires purple RequestVote RPCs. Green VoteGranted circles come back. Once a majority responds, a new red leader appears and immediately sends blue heartbeats.

Reconnect the old leader — it receives a heartbeat from the new leader with a higher term, realises someone else is in charge, and quietly steps back to follower.

Log replication

Once a leader is elected, it handles all client writes. The flow is:

  1. Client → Leader: a write request arrives at the leader (the pink circle traveling from the client icon on the left)
  2. Leader appends the entry to its own log
  3. Leader sends AppendEntries to all followers in the same heartbeat round (the blue circles — these double as heartbeats when empty)
  4. Once a majority of servers acknowledge, the entry is committed — the log panel shows it turn from dashed to filled
  5. Leader piggybacks the new commitIndex in the next AppendEntries, so followers advance their commit pointer too

This majority requirement is the key safety property: a committed entry must be on enough servers that any future leader will have it in their log. Raft's Leader Completeness guarantee means a new leader is only elected if its log is at least as up-to-date as any majority — so committed entries are never lost.

Commit index, quorum, and uncommitted entries

Every Raft node tracks two numbers:

  • log.length — how many entries have been appended (proposed but not necessarily safe)
  • commitIndex — how many entries are durably committed (a quorum has acknowledged them)

The gap between the two is the uncommitted tail — entries the leader has proposed but hasn't yet heard back from a quorum about. These are shown as dashed squares in the log panel. Uncommitted entries are not safe: if the leader crashes before reaching quorum, a new leader may have a shorter log, and any follower with extra entries must truncate them back.

An example walkthrough:

  1. Click ⚡ Partition 3|2 to isolate N4 and N5 from the majority.
  2. Hit 📨 Client write two or three times. Watch the majority partition (N1–N3) commit those entries — their log panel squares fill in solid.
  3. Now click the leader to disconnect it mid-term.
  4. A new leader is elected in the majority. If any follower received entries that were never acknowledged by a quorum, those entries show dashed; when the new leader's first AppendEntries arrives, you'll see the event log print ✂ Nₓ removes N uncommitted entries and the dashed squares disappear.
  5. Click 🔗 Heal partition — N4 and N5 rejoin as followers and the current leader sends them the missing committed entries, catching them up.

Why is this safe? The new leader must have a log at least as up-to-date as a majority (enforced by the vote-granting check), so committed entries are always present in the winner's log. Only entries that never reached quorum can be overwritten — and by definition those were never acknowledged to any client.

Catch-up: AppendEntries and InstallSnapshot

When a follower falls behind — perhaps it was offline, or sits in the minority partition — the leader uses AppendEntries to replay the missing suffix of the log. The leader decrements its estimate of the follower's position (nextIndex) until the logs agree, then sends all entries from that point forward. In the log panel you can watch a rejoining node's squares fill in one heartbeat round at a time.

For nodes that are very far behind — hours of log entries in a production cluster — replaying the full log would be too slow. Raft handles this with InstallSnapshot: the leader compresses its entire state machine into a snapshot, ships it to the lagging follower in one RPC, and the follower jumps straight to the snapshot point, discarding its old log up to that index. This is how systems like etcd and CockroachDB handle nodes that have been offline for extended periods.

Inside the log: commits, snapshots, and truncation

The cluster view shows messages; this second simulation shows the thing all those messages are about: the log itself. The client sits at the far left, the nodes line up in a column, and every log entry is a box in a shared index grid — new entries always append at the left edge and push older ones rightward, so you can see exactly where every node's log ends, what it contains, and how far it lags.

Speed1×

Click a circle to cut a node off (click again to bring it back) · click the CLIENT icon to append a write · entries append at the left edge and push older ones right; the leader replicates by sending entries to each node · 📸 Snapshot now folds the log and forces lagging nodes onto the InstallSnapshot path.

LeaderFollowerCandidateCatching up (snapshot)OfflinecommitIndexsnapIndextrunc. idx = commitIndex − 8✂truncate on conflict → rebuild from snapshotmissing entries (to catch up)
committed (solid = reached quorum)uncommitted (dashed)box colour = termviolet box = snapshot (base of the log, fades past the trunc. idx)amber outline = covered by snapshot, kept in log for replayred = doomed (past-term, never committed) — about to be rewrittenAppendEntries (entries travel to nodes)AppendEntriesOKRequestVoteVoteGrantedInstallSnapshotclient write

How to read it:

  • Circles are nodes — red leader, blue followers, amber candidates, cyan while catching up, dark when offline. Click a circle to disconnect or reconnect it. To their left sits the CLIENT, with a faint dotted line always pointing at the current leader, so you can see exactly who it fires each write at.
  • Boxes are log entries, flowing right: every new entry appends at the left edge of the row and pushes older entries rightward. The fill colour encodes the term the entry was created in; a dashed box is uncommitted, a solid box has reached quorum — a distinction that survives every term change, because state (solid/dashed) and term (colour) are encoded independently. The small letter inside is the value that entry applies to the state machine (∅ is the no-op a new leader appends to seal the log).
  • Snapshot boxes (violet, 📸): when a snapshot is created the box hangs below the row, marking the deleted region without overlapping entries; when a snapshot is installed on a catching-up node it sits at the base of that node's log — the streamed entries pile up on top of it. A node can show several at once (multiple snapshots); older ones fade as the truncation barrier passes them. The amber outline marks entries that are already covered by a snapshot but kept in the log as the replayable catch-up buffer.
  • Small balls are RPCs: pink client writes flying from the client to the leader, blue AppendEntries carrying the entry they replicate to each node, teal acknowledgements coming back. A catching-up node first receives the big cyan snapshot packet, then — after a small lag — the missing entries travel over as individual balls.
  • Lines mark the key log positions. The green line is the cluster's commitIndex — everything right of it is durably committed everywhere. The violet line is snapIndex — the newest snapshot covers everything right of it. The amber line is the truncation index (commitIndex − N, always below the latest snapIndex): deletion sweeps past it one entry per commit, and when a follower's tail conflicts with the leader's, repair rewinds to the last entry both sides agree on. A follower that falls below the leader's retained history gets an InstallSnapshot instead — so no node can ever be stranded. Each row also carries its own small green/violet ticks plus a ci/si read-out, so you can see where that node is.
  • Red ✂ marks are repair cut-backs: where a node's log conflicts with the leader's log (a real prevLogIndex/prevLogTerm check), the whole stale term-run is rewound in one step down to the last entry both sides agree on — and the leader's log simply replays over the gap. Only when a follower has fallen below the leader's retained history does the repair instead ship a fresh snapshot followed by streamed replay.
  • Red boxes are doomed entries: uncommitted, from a term that lost the leadership race — they never reached a quorum and can no longer. They light up red (pulsing) the moment the leader's AppendEntries announces they will be rewritten, then visibly get deleted (a red shrink-away) before the replacement entries arrive.
  • Cyan zones are catch-up buffers: they sit exactly left of a lagging node's newest entry and are labelled with the index range it is missing (e.g. ⟳ catch-up 15–21).

Try these scenarios:

  1. Normal operation. Just let it run. Client writes fly from the left to the leader, entries push in at the left edge of every node's row and shove older ones right, and when a quorum acknowledges, the boxes flip from dashed to solid as the green commitIndex line sweeps leftward over them — with a green pulse ring at the leader.
  2. Snapshot compaction. Keep writing. Whenever commitIndex runs more than a handful of entries past snapIndex, each node compacts: its oldest entries (the right end of the row) fade quietly into the violet 📸 snapshot badge, and the snapIndex line jumps right. You can also press 📸 Snapshot now to fold everything immediately — afterwards, any node that is behind can only catch up by receiving a full InstallSnapshot. The log never grows forever — this is exactly the log-compaction step real implementations run in the background, and because folding replaces clipping, you never see the view paginate or reset.
  3. Leader transition. Click 💥 Kill leader. A follower times out, turns amber, and fires purple RequestVote RPCs (you can watch the election arcs). Once a majority grants votes, a new leader emerges, appends a ∅ no-op at the left edge of its row, and replicates it — and any uncommitted entries left behind by the old leader get cut: a red ✂ marks the rewind point, and every uncommitted box that can no longer reach quorum pulses red, slides off to the left and fades away before the replacement entries arrive.
  4. Reconnect the old leader. It comes back still believing it's in charge — until the new leader's higher-term heartbeat arrives and it steps down, truncating whatever extras it was holding.
  5. Fall behind, then catch up. Click a follower to disconnect it, let several writes commit — press 📸 Snapshot now if you want to force the snapshot path — then bring the node back: instead of replaying dozens of entries, the leader ships one big cyan InstallSnapshot packet — the node flashes cyan, its log jumps instantly to snapIndex, and the labelled cyan catch-up buffer on its row visibly fills in with the missing newest entries, pushing the rest of its log rightward until it is caught up.

What the simulations don't show

The visualisations are intentionally simplified to keep them legible:

  • Log contents are simplified — entries only carry the term they were created in, not actual data
  • Disk writes are omitted (in real systems, entries are fsynced before acknowledging)
  • One-directional partitions are not modelled — the partition button disconnects both directions symmetrically
  • Membership changes (adding/removing nodes without downtime via joint-consensus) are not modelled
  • Pre-vote and leadership transfer extensions (common in production implementations) are omitted

For a deeper treatment of these topics, the original paper is surprisingly readable.

Further reading

Related Articles

◆
·12 min read

The Challenges of WebGL

WebGL lets you run shaders in the browser — and that's genuinely magical. But using it for arbitrary GPU computation quickly reveals a thicket of constraints that OpenCL and WebGPU were designed to escape.