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.