Break the leader.
Keep the data.
A visual walkthrough of a persistent three-node key-value store built in Go. Writes complete only after a two-node majority confirms the log entry.
One logical store.
Three independent processes.
Clients can contact any node. Followers redirect writes to the current leader. The leader serializes mutations, replicates log entries, and acknowledges only after a majority has persisted them.
Client boundary
HTTP endpoints expose writes, linearizable reads, explicitly stale reads, and observable node status.
Peer boundary
RequestVote, AppendEntries, and InstallSnapshot coordinate nodes over non-persistent HTTP connections.
Storage boundary
Terms, votes, logs, snapshots, and commit progress survive process and container restarts.
What happens after
PUT /kv/color?
The safety guarantee comes from order: persist locally, replicate to peers, reach quorum, then apply and acknowledge.
Only the leader accepts a mutation.
A follower returns the known leader so the client can retry. A client/request ID makes that retry idempotent instead of creating a duplicate write.
POST /kv/color { "value": "cyan", "client_id": "demo", "request_id": "42" }
Linearizable read
Leader-only read that confirms current-term authority with a quorum. It never returns an older value after a completed write.
GET /kv/:keyStale replica read
Available from any node without a quorum round trip. Useful for observing replication while explicitly accepting older data.
GET /kv/:key?consistency=staleIdempotent write
A repeated request ID returns the original result. Reusing that ID for different content is rejected as a conflict.
client_id + request_idFault tolerance is a
tested behavior.
Each scenario begins with fresh containers and volumes, injects one controlled failure, verifies safety during the fault, heals the cluster, and checks deterministic convergence.
Leader process crash
Stop the active leader, elect a replacement, commit through the surviving quorum, restart the old leader, verify catch-up.
Live network partition
Keep the old leader running but isolated. It must reject quorum-confirmed reads while the majority partition elects and writes.
Lagging follower
Leave a follower behind the compaction boundary, then require snapshot installation plus log-suffix catch-up.
Container restart
Recover persisted term, vote, log, snapshot, commit index, key/value state, and deduplication records without reconfiguration.
Bounded history.
Complete recovery.
Snapshots keep the replicated log from growing forever while preserving enough state to reconstruct the service and retry semantics.
01Leader detects follower's nextIndex is behind the retained log.
02InstallSnapshot transfers the compacted state atomically.
03Normal replication resumes with entries after the snapshot index.
Claims backed by
repeatable experiments.
Fifteen independent 10,000-write runs used fresh cluster state at concurrency 1, 8, and 16. Every run verified commit index, key count, and a deterministic SHA-256 state hash across all nodes.
100% scenario pass rate
maximum observed in matrix
maximum observed convergence
| Concurrency | Mean throughput | p50 latency | p95 latency | p99 latency | Convergence |
|---|---|---|---|---|---|
| 1 worker | 21.52 ops/s | 45.97 ms | 86.20 ms | 106.59 ms | Verified |
| 8 workers | 24.99 ops/s | 325.18 ms | 576.37 ms | 609.42 ms | Verified |
| 16 workers | 25.50 ops/s | 625.29 ms | 1134.82 ms | 1206.84 ms | Verified |
Why throughput plateaus: the implementation prioritizes understandable safety and synchronous durable writes over batching. Each committed mutation pays persistence and quorum costs—an explicit engineering tradeoff, not a hidden benchmark condition.
From failure mode
to engineering control.
The project is a collection of safety decisions. Each mechanism exists because a specific distributed-systems failure makes the simpler design incorrect.
Single-node failure loses availability
↓SOLUTIONReplicated log, randomized elections, heartbeats, and majority-based leadership.
Two leaders can claim authority
↓SOLUTIONMonotonic terms, one persisted vote per term, and step-down on higher-term messages.
A retry can apply a write twice
↓SOLUTIONPersisted client/request IDs with same-command replay and conflict rejection.
Quorum contact alone can allow a stale read
↓SOLUTIONA committed current-term no-op barrier before serving linearizable reads.
An old leader remains alive after partition
↓SOLUTIONQuorum-confirmed reads and writes; non-persistent peer connections reflect network state.
Logs grow and slow follower recovery
↓SOLUTIONAutomatic snapshots, absolute log indexes, compaction, and InstallSnapshot RPC.
Explain the system
from first principles.
Use these prompts to walk through the design. Open any question for a concise answer, then follow the source links for implementation detail.
01Why does a three-node cluster tolerate only one failure?+
A commit needs a majority: two votes or acknowledgements. With one node unavailable, two remain and can progress. With two unavailable, the survivor cannot distinguish failure from partition and must stop committing to preserve safety.
02What happens when the current leader crashes?+
Followers stop receiving heartbeats. After randomized election timeouts, one becomes candidate, increments the term, votes for itself, and requests votes. A candidate with an up-to-date log becomes leader after receiving two votes.
03Why can’t the isolated old leader keep writing?+
It may append locally, but cannot collect a second acknowledgement. Without a majority, it cannot advance the commit index or report success. When connectivity returns, the higher-term leader overwrites any conflicting uncommitted suffix.
04How is a GET made linearizable?+
Reads are leader-only and quorum-confirmed. A newly elected leader first commits an entry from its current term, establishing authority over committed history before it serves the read.
05How does a follower recover after missing compacted entries?+
If the follower's next required index precedes the leader's retained log, the leader sends a snapshot. The follower installs it atomically, restores key/value and deduplication state, then receives the remaining log suffix normally.
06What real bug did fault testing uncover?+
Leaders were still processing their election timers, causing unnecessary term churn during sustained writes. The fix made leaders ignore election timeouts, and a race-enabled regression test now protects that state-machine rule.