A Raft consensus simulator

Several computers, one identical list.

Large services do not keep their data on a single computer — if that one dies, everything is gone. The data lives on several computers at once, and every one of them has to hold exactly the same list of records. The difficulty: any of them can die at any moment, and the network can cut out mid-delivery. Raft is the rulebook that keeps them in agreement anyway.

What you just read is the easy part. The hard part is a handful of small rules in Raft that look redundant — until you switch one off. That is what this app is for: turn a rule off, then watch the guarantee it was defending collapse in front of you.

How it works, briefly

  1. One leader is elected

    Every computer waits to hear from a leader. Whichever waits longest without hearing anything stands for election and asks for votes. Once more than half vote for it, it is the leader — the only one allowed to record anything new.

  2. Records are copied to everyone

    The leader sends each new record to all the others. Once more than half have stored it, it is declared committed: settled, and never to change again.

  3. Stragglers are brought back into line

    A computer that was down or cut off gets its list reconciled as soon as it is reachable again. If its list had diverged, the wrong part is overwritten by the leader’s.

n0leader
  • 11
  • 21
  • 32
n1member
  • 11
  • 21
  • 32
n2down
  • 11
  • 21
  • 3
Row 3 is held by only two of the three computers — and that is enough. Two of three is a majority, so the row is settled even though n2 is down. When n2 comes back, it catches itself up.

How to read the picture

Each shape is one computer. The little boxes between them are messages in flight — messages take time to arrive, and some never arrive at all.

  • Leader

    In charge. The only one that accepts new records and distributes them.

  • Follower

    An ordinary member. Waits to hear from the leader and copies its records.

  • Candidate

    Currently standing for election and asking the others for their votes.

  • Down

    Currently down. Sends nothing and receives nothing.

Where to start

Five promises that must never break

Raft promises five things. This app checks all five after every single event — not at the end, but continuously — and names the one that failed the moment it fails.

  • There are never two leaders at once in the same round.

    Election Safety · At most one leader can be elected in a given term.

  • A leader only ever adds to the end of its list; it never deletes its own records.

    Leader Append-Only · A leader never overwrites or deletes entries in its log; it only appends new entries.

  • If two computers hold the same record at a row, everything above that row is identical too.

    Log Matching · If two logs contain an entry with the same index and term, then the logs are identical in all entries up through the given index.

  • A record that has been settled never goes missing from any later leader.

    Leader Completeness · If a log entry is committed in a given term, then that entry will be present in the logs of the leaders for all higher-numbered terms.

  • No two computers ever act on different contents at the same row.

    State Machine Safety · If a server has applied a log entry at a given index to its state machine, no other server will ever apply a different log entry for the same index.

If you already know Raft

A Raft simulator you can break on purpose. Deterministic discrete-event simulation, continuous safety-invariant checking, and an ablation mode that turns individual Raft rules off so you can watch the guarantee they protect actually fail.

Prior art

RaftScope exists, it is excellent, and it was written by the author of the Raft paper. It is the canonical Raft visualiser — if you want to watch Raft run, start there. The Secret Lives of Data is a fine explainer too.

What this adds

Not a replacement for either. Its contribution is narrower: ablation and invariant checking. RaftScope shows you the mechanism; here you switch a rule off and watch the safety property it defends break, while five indicators tell you which one failed and why.

Normative source

Ongaro & Ousterhout, In Search of an Understandable Consensus Algorithm (USENIX ATC 2014), extended version — Figure 2 in particular. Every rule in lib/raft cites the figure and rule number it implements.