Case study
Crystal
A Raft key-value store in Go
- Role
- Sole author
- Stack
- Go
- Status
- Open source, in active development
- Since
- 2025
The problem
Raft is the consensus algorithm behind etcd and a lot of coordination services. The paper is clear, and a correct implementation is still hard, because the dangerous bugs are quiet. A server that answers a vote request before writing the vote to disk can vote twice in one term after a restart. Nothing crashes. The cluster just loses an acknowledged write some time later.
I built Crystal to find out where those bugs hide, and to end up with a codebase where every consensus decision can be traced back to a section of the paper. The Understanding Raft series on this site explains the algorithm part by part, using Crystal as the example.
What it does
A Crystal cluster is a group of nodes that agree on an ordered log of writes and apply it to identical copies of a key-value map. Clients set, get and delete keys over HTTP, and the data stays consistent and available as long as a majority of nodes are up, through leader crashes, partitions and restarts.
| Area | What is there | Paper |
|---|---|---|
| Leader election | Randomized timeouts, no fixed leader, step down on any higher term | §5.2 |
| Log replication | AppendEntries with consistency check, batching, conflict hints |
§5.3 |
| Safety | Election restriction, the Figure 8 commit rule, persist before respond | §5.4 |
| Leader stability | Pre-vote, CheckQuorum, and the stickiness check | §6 |
| Compaction | Snapshots, log truncation, InstallSnapshot for lagging followers |
§7 |
| Reads | Linearizable reads from any node through ReadIndex, plus bounded-staleness reads | §8 |
| Clients | Exactly-once writes through client sessions | §8 |
Decisions and what they cost
One goroutine owns consensus state
The commit index, the role and the waiting requests have exactly one writer, the control loop. Everything else sends it a message. Raft’s risky steps read several fields and then write one, and with one owner they become plain sequential code with no lock to forget. The cost is throughput. Every event goes through one loop, so the loop never blocks on the network, and replication runs in one goroutine per follower that only reports back.
fsync before every acknowledgement
Raft’s safety proof assumes a server never forgets a vote or an entry it acknowledged. Crystal’s write-ahead log syncs to disk before a reply goes out, and on recovery it cuts off a half-written last frame instead of trusting it. The disk sets the floor for write latency, so entries are synced in batches with one fsync per batch.
Snapshots at the applied index
A snapshot is a copy of the state machine, which only contains what has been applied. Crystal snapshots at the last applied index rather than the commit index, because on a follower the two drift apart, and a snapshot at the commit index would claim writes it doesn’t contain. It also refuses to snapshot at an index whose term it doesn’t know. Both rules cost a little disk space while the applied index catches up.
Reads that say how fresh they are
A linearizable read waits for the leader to confirm, with a round of heartbeats, that it still leads. Callers that can accept slightly older data ask for a bounded-staleness read instead, which any recently contacted node can serve. If the node can’t meet the bound, it refuses instead of quietly answering with old data.
Proof
- Unit tests for the invariants, among them a test that replays the paper’s Figure 8 against the commit rule.
- An in-process cluster of several nodes over a fake network, for fast and repeatable scenarios.
- An integration suite that runs real processes over HTTP. It kills the leader and checks that no acknowledged write is lost, restarts a follower and checks it converges, makes a follower catch up through a snapshot, and checks that a retried client request is applied exactly once.
In total that’s more than 170 Go test functions.
Not built yet
Dynamic membership. The joint-consensus math, learners and the configuration type are written and unit-tested, and carrying configuration changes through the log comes next. Leadership transfer and service discovery are also open, so peers are passed on the command line for now. After that, the plan is an LSM-tree storage engine behind the existing state machine interface, so Crystal can grow from a coordination service into a real database.