WorkCase study

Distributed systems · Open source · 2026

quorum

Raft consensus in Go, with a simulator that can replay a whole cluster and its failures from a single seed.

$ go run ./cmd/quorum-sim -seeds 500
500 seeds in 6.37s: 2006617 ticks, 330221 operations (330221 completed), 3244 leader terms, 337948 entries committed,
30313 snapshots, 2055 crashes, 4957 partitions, 12871111 messages (2192855 lost)
no property violated

$ go run ./cmd/quorum-sim -seeds 40 -unsafe-reads
seed 17: VIOLATION at tick 4013: linearizability: no sequential order explains the history of key "k1"
seed 21: VIOLATION at tick 4005: linearizability: no sequential order explains the history of key "k1"
seed 26: VIOLATION at tick 4009: linearizability: no sequential order explains the history of key "k2"
40 seeds in 398ms: 160535 ticks, 29059 operations (29059 completed), 257 leader terms, 19371 entries committed,
1738 snapshots, 164 crashes, 395 partitions, 730476 messages (141629 lost)
3 seed(s) violated a property
The simulator across 500 seeds, then again with a bug I added on purpose (reads served from a leader that might have been replaced). The checker catches it on three seeds.
Role
Everything: the Raft node, the key value store on top of it, the simulator and the linearizability checker.
Team
Solo
Stack
Go, Go standard library only, GitHub Actions
Status
Open source, 2026

Problem

Consensus code is easy to write and hard to trust. The bugs hide in rare orderings: a message that lands after a crash, a leader that doesn't know it's been replaced, a retry that arrives twice. Unit tests don't reach those cases, and you can't reproduce them on a real network.

My distributed parking project has the same issue. Its tests start real processes and kill them, and one failure only shows up some of the time. quorum is set up so that any failure can be replayed.

It only uses the Go standard library.

What I built

A solo project with four parts: the Raft algorithm in one file, a replicated key value store (get, put, delete, compare and swap), the simulator with its fake network, clients and fault injector, and the linearizability checker.

FeatureNotes
Leader electionRandomized timeouts, one vote per term, the up to date log check.
Pre-VoteA node checks whether it could win before raising its term, so a node coming back from a partition doesn't knock out a healthy leader.
CheckQuorumA leader that stops hearing from a majority steps down.
Log replicationFast conflict back off. Handles duplicate and out of order messages.
Commit ruleOnly counts entries from the leader's own term (Raft section 5.4.2).
SnapshotsLog compaction, and sending a snapshot to a follower that's too far behind.
Exactly once commandsClient id and sequence number on each command, with a session table that's included in snapshots.

Not implemented: membership changes, leadership transfer, read leases, a real network transport and an on disk log. For now it only runs inside the simulator.

How it works

  1. SeedDrives every random choice
  2. Simulator5 nodes, 4 clients, a lossy network and a fault injector
  3. Every tickOne leader per term, no conflicting entries
  4. End of runEvery operation finished, all stores identical
  5. HistoryLinearizability check per key

The usual way to build Raft is a node with its own goroutines, timers and network connections. That's closer to production, but then a failure depends on scheduling and you can't get the same run twice.

Here the node doesn't own any of that. Every input is a function call: time passed, a message arrived, a client proposed something. The node returns the messages it wants to send and the entries it wants to apply. The simulator decides the order of everything from one random seed, so if a seed fails I can run it again and get the exact same failure.

The tradeoff is that there's no real transport or disk yet. Storage is modeled as always succeeding, so torn writes and bad fsyncs aren't covered.

The fault injector partitions the cluster, isolates the leader, and crashes and restarts nodes (never more than a minority at once). Messages get delayed, dropped and duplicated. Clients retry timed out commands against other nodes.

The commit rule

A leader can only count replicas toward a commit if the entry is from its own term. It's a few lines, and getting it wrong can lose committed data.

raft/raft.go// maybeCommit advances the commit index to the highest index stored on a quorum,
// but only if that entry is from the current term (Raft section 5.4.2).
func (n *Node) maybeCommit() bool {
	matched := make([]uint64, 0, len(n.cfg.Peers))
	for _, id := range n.cfg.Peers {
		matched = append(matched, n.match[id])
	}
	sort.Slice(matched, func(i, j int) bool { return matched[i] > matched[j] })
	index := matched[n.quorum()-1]
	if index <= n.commit {
		return false
	}
	if term, ok := n.termAt(index); !ok || term != n.p.Term {
		return false
	}
	n.commit = index
	return true
}

Does the checker catch real bugs?

A checker that never fails doesn't tell you much, so I broke things on purpose to see what it would catch.

  • Stale reads: caught. If reads come from the local state of whichever node thinks it's the leader, an old leader can return outdated data. The linearizability check flags it on 3 of 40 seeds.
  • Voting without the up to date check: caught, but rarely. About one seed in 300 on a three node cluster.
  • The commit rule above: missed. Removing the current term condition wasn't caught in 600 seeds. The situation that triggers it is too specific for random faults, so I cover it with a unit test that sets it up directly.

So the simulator finds a lot, but passing seeds don't prove the code is correct.

There are 34 tests, and CI runs them with the race detector. Here are the first eight seeds one at a time:

$ go run ./cmd/quorum-sim -seed 1 -seeds 8 -v
seed 1: ok  ops=712 terms=6 committed=724 snapshots=71 crashes=3 partitions=9
seed 2: ok  ops=791 terms=6 committed=809 snapshots=77 crashes=4 partitions=9
seed 3: ok  ops=657 terms=9 committed=683 snapshots=58 crashes=6 partitions=10
seed 4: ok  ops=600 terms=7 committed=617 snapshots=54 crashes=4 partitions=11
seed 5: ok  ops=682 terms=5 committed=697 snapshots=54 crashes=3 partitions=9
seed 6: ok  ops=759 terms=8 committed=774 snapshots=67 crashes=2 partitions=15
seed 7: ok  ops=562 terms=6 committed=584 snapshots=48 crashes=3 partitions=7
seed 8: ok  ops=543 terms=7 committed=567 snapshots=45 crashes=6 partitions=13
8 seeds in 107ms: 32089 ticks, 5306 operations (5306 completed), 54 leader terms, 5455 entries committed,
474 snapshots, 31 crashes, 83 partitions, 199499 messages (36642 lost)
no property violated

Limits

  • Storage always succeeds in the simulator. Whatever a node wrote before a crash is still there after.
  • The linearizability check can get very slow when lots of operations overlap, so each client only has one operation in flight.
  • Passing seeds are evidence that it works. They don't prove it.
  • There's no network transport or on disk log, so it isn't something you could deploy.

What's next

An on disk log first, with torn writes and bad fsyncs added to the fault injector. Then a real transport so the node can run outside the simulator. Membership changes after that.