I Built a Distributed Key-Value Store in Go. Then I Killed the Leader.
How I built Concord, a distributed key-value store in Go using Raft consensus: leader election, log replication, quorum commits, failover, and WAL recovery.
Contents8 sections
00 / Why build a distributed store?
Inside Concord: leader elections, majority commits, crash recovery, and why sometimes refusing a write is the correct behavior.
A key-value store sounds simple: receive SET(language, go), put it in a map, and return OK.
Now put that store on three different machines. One machine accepts a write, another misses a message, and the third suddenly goes offline. Which machine has the correct value? What happens if the node accepting writes crashes halfway through the operation?
Those questions led me to build Concord, a distributed key-value store written in Go. Instead of plugging in an existing consensus library, I implemented the core of the Raft consensus algorithm myself. My goal wasn't to build the next production database. It was to understand what agreement actually requires when machines fail.
01 / A quick tour of the architecture
Concord’s default Docker setup runs as a three-node cluster. Each node owns a TCP server, Raft state and replicated log, an in-memory key-value store, and local persistent storage. Each node also exposes an HTTP API that powers its browser dashboard and accepts SET, GET, and DELETE requests.
One agreed history.
Followers → leader: acknowledgements
A client can contact any healthy node through HTTP. That node attempts to discover the leader and forwards the operation; requests can fail while an election is in progress or the leader is unreachable. The leader coordinates mutating commands, while followers replicate its log. The dashboard makes leadership changes visible through each node's role, term, and log progress.
02 / Raft leader election: who gets to lead?
Every node starts as a follower. Leaders send periodic heartbeats—every 200 ms in Concord's configuration. If a follower stops hearing from one, it waits for a randomized election timeout (800–1,400 ms), becomes a candidate, increments its term, and requests votes.
A term is Raft’s logical election counter. Each voter compares the candidate’s last log term, then its last log index, and grants a vote only if that log is at least as up to date as its own. A node can vote for only one candidate per term. In a three-node cluster, a candidate needs two votes, including its own, to win.
Why randomize the timeout? If every node started an election simultaneously, they could repeatedly split the vote. Randomized timeouts help one candidate get a head start.
Once elected, the leader sends heartbeats and starts managing replication. If another node later presents a higher term, the old leader must step down.
03 / Log replication & quorum: OK must mean something
This is the path a write takes in Concord:
earn its meaning.
No confirmed success.Return an error. Do not apply an uncommitted entry. A retained entry may commit after quorum returns.
- The leader receives a
SETorDELETEcommand and appends an entry containing its index, term, and command to the Raft log. - It sends
AppendEntriesrequests to followers, along with the preceding log index and term. Followers reject inconsistent prefixes; the leader backs up and retries to repair conflicting logs. - The leader tracks which followers have replicated the entry. It can advance
commitIndexto an entry from its current term once that entry is stored on a majority, counting the leader itself. Earlier entries in that log prefix become committed too. - Committed commands are applied in order. Concord first synchronizes each mutation to its write-ahead log, then updates the in-memory store and returns
OK.
The key distinction is between replicated (stored on another node), committed (safe to apply under Raft’s quorum rules), and applied (executed against the key-value store). commitIndex tracks the highest log index known to be committed; it does not mean every follower has already applied that entry.
A log entry can exist on one node without being safe to treat as a successful write. If the leader loses contact with both followers, it may still be running—but it cannot gather a majority. Concord must reject the write rather than pretend it succeeded.
The TestLeaderDoesNotCommitWithoutQuorum integration test checks exactly that: with both followers stopped, the request fails and the uncommitted value does not reach the leader’s in-memory store while quorum is unavailable.
An entry left in the Raft log may still be replicated and committed after quorum returns. A client can also lose the response to a write that already committed. A failed request means the client has no confirmation of success; it does not always mean the operation had no effect. Concord does not implement request deduplication for exactly-once retry semantics.
04 / What happens when the leader dies?
Suppose Node 1 is the leader. It stops running, leaving Nodes 2 and 3 alive. If they can communicate, the remaining nodes still form a majority of the configured cluster and can elect a new leader, then resume accepting committed writes.
The cluster carries on.
The TestPersistentLeaderFailureAndRejoin test covers this sequence: shut down the original leader, elect a different one in a higher term, accept another write, restart the old leader using its on-disk data, and verify that it catches up as a follower. This test uses a controlled node shutdown; it is not proof of recovery from every abrupt crash or power-loss scenario.
The opposite case matters just as much: one surviving node out of three is not a quorum. Availability is sacrificed rather than allowing two disconnected parts of a cluster to accept conflicting successful writes.
05 / Surviving a crash requires more than replication
Replication helps with node failure, but a restarted node also needs its own history.
Concord persists Raft metadata and log entries in a local raft-state.json file using a temporary file, sync, and rename. Separately, committed mutations are recorded in a checksummed write-ahead log (WAL) before their effects are applied to the in-memory store. On startup, Concord replays WAL records and applies any additional committed Raft entries that need recovery.
A particularly useful edge case is a crash in the middle of a disk write. Every WAL record ends in a newline; if the final bytes lack that terminator, Concord treats them as a potentially torn tail and truncates them while preserving complete earlier records. Complete version-2 records with invalid CRC32 checksums are rejected instead of silently accepted. The WAL tests exercise both truncated tails and checksum corruption.
This is the point where a toy map[string]string starts to feel like storage engineering: the hard part isn't storing a value. It's knowing which values are safe to recover.
06 / What Concord does not promise
Concord is an educational implementation, not a production-ready database. There is no snapshotting or log compaction yet, so retained history and storage costs grow over time. Cluster membership is static, and transport is unencrypted. Reads are routed to the node believed to be the leader, but the implementation does not use Raft ReadIndex or leader-lease checks. An isolated former leader may therefore serve stale reads; I do not claim fully linearizable reads across partitions.
Being explicit about these gaps is part of the engineering exercise. A consensus algorithm is not a magic switch that makes every operation automatically safe.
07 / Try it yourself
With Docker installed, the three-node cluster starts with:
git clone https://github.com/JashRashne/Concord.git
cd Concord
docker compose up --buildOpen http://localhost:9091, http://localhost:9092, or http://localhost:9093 to see the dashboards. Set a key, identify the current leader, and note its term. If node-1 is the leader, stop it from a second terminal:
docker compose stop node-1Use a surviving node’s dashboard to watch for a new leader in a higher term, then issue another write. Restart the stopped node and check that it rejoins as a follower and catches up:
docker compose start node-1This is a controlled failover exercise, not a measured recovery-time benchmark. The 800–1,400 ms election timeout is only one part of the process; scheduling, RPCs, and retries also affect how long recovery takes.
The repository includes unit tests, multi-node integration tests, and race-detector commands.
For this article, the quorum-loss and persistent leader-rejoin integration tests were rerun with Go’s race detector; both passed. To run those two scenarios yourself:
go test -race -count=1 ./internal/node \
-run 'Test(LeaderDoesNotCommitWithoutQuorum|PersistentLeaderFailureAndRejoin)$'Code references use Concord revision dc79507.
The biggest lesson I took away: fault tolerance isn't about ensuring every request succeeds. It's about ensuring a successful response actually means something, even when the system is failing.
Thanks for reading.
Back to the notebook