Raft-based Key-Value Store
A distributed, fault-tolerant key-value store implementing the Raft consensus algorithm from scratch in Go. It provides linearizable read and write operations across a cluster of nodes with leader election, log replication, and persistent state snapshots.
Reading consensus papers is not the same as debugging split-brain scenarios or network partitions under load. I built this system to develop an intuitive, mechanical sympathy for distributed consensus invariants, leader leases, term changes, and log compaction under real failure modes.
Client Request
│
▼
┌───────────────────────────────────┐
│ RPC / Transport Layer │
└────────────────┬──────────────────┘
│
Leader Consensus Node
│
┌───────────────────┴───────────────────┐
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Consensus │ ── AppendEntries ──► │ Peer Node │
│ Engine │ ◄─ Acknowledgement ── │ Replicated │
└──────┬───────┘ │ State │
│ └──────────────┘
Commit Invariant Satisfied
▼
┌──────────────┐
│ State Machine│ ── Write-Ahead Log & Snapshots
│ (KV Engine) │
└──────────────┘1. Candidate State : Election timeout triggers -> RequestVote RPC -> Majority quorum 2. Leader Lease : Heartbeats (AppendEntries) issued at 50ms intervals 3. Log Invariant : Entry committed once acknowledged by majority of quorum 4. Linearizable Read: ReadIndex validation to prevent stale reads during partitions
Subtle race conditions during leader heartbeats and election timers caused intermittent split votes when simulating network delays. I learned firsthand why deterministic election jitter is essential, how volatile state must be safely synchronized to non-volatile disk before sending RPC replies, and how tricky linearizable read semantics can be without read-index validation.
- Leader election convergence:
< 160ms(across a 3-node cluster with randomized election jitter (150ms – 300ms window)) - Log throughput:
~4,500 ops/sec(committed writes on single-node leader with synchronous fsync batching) - Partition recovery:
1.2s(cluster auto-heals and commits consensus backlog after network partition heals)
- GitHub Repository: github.com/amit-devb/raft-kv ↗