← All projects

Raft-based Key-Value Store

GitHub Repository ↗
What it is

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.

Why I built it

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.

Architecture
                      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)  │
 └──────────────┘
Consensus Core Loop
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
What broke and what I learned

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.

Numbers & Benchmarks
  • 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)
Links
← Back to Projects