System DesignOct 5, 2026⏱️ 3 min read
Design a Distributed Key-Value Store: CAP Theorem, Quorum, Vector Clocks & LSM Trees

Design a Distributed Key-Value Store: CAP Theorem, Quorum, Vector Clocks & LSM Trees

Inside Dynamo-Style Distributed Key-Value Stores

Distributed key-value stores (like Amazon Dynamo, Apache Cassandra, and Riak) power massive workloads requiring sub-10ms reads/writes, horizontal petabyte scalability, and high availability across network failures.
Unlike single-node relational databases, a distributed KV store must coordinate replication, partition tolerance, conflict resolution, and fast on-disk persistence simultaneously.
Distributed Key-Value Store Architecture

Distributed Key-Value Store Architecture


1. CAP & PACELC Trade-Offs

In a distributed system, network partitions (P) are inevitable—switches fail, links drop packets. When a partition occurs, you must choose between:
  • CP (Consistency + Partition Tolerance): Reject writes or stale reads on the minority side of a partition (e.g., etcd, ZooKeeper, HBase).
  • AP (Availability + Partition Tolerance): Allow both sides to keep serving reads/writes, reconciling conflicts afterward (e.g., Cassandra, Dynamo).

2. Partitioning & Tunable Quorum Consistency

Data is partitioned across nodes via Consistent Hashing with virtual nodes. To survive node failures, every key is replicated to the first $N$ distinct physical nodes walking clockwise on the ring.

The Quorum Formula: $W + R > N$

  • $N$: Replication factor (typically 3).
  • $W$: Write quorum (nodes that must confirm a write).
  • $R$: Read quorum (nodes queried for a read).
Configuration ($N=3$)BehaviorIdeal Workload
$W=1, R=3$Ultra-fast writes, slower strong readsTelemetry, event ingestion
$W=3, R=1$Ultra-fast reads, strict durable writesRead-heavy configuration lookups
$W=2, R=2$Balanced strong consistency ($2+2 > 3$)General-purpose primary KV data
Because any quorum of $W$ and $R$ with $W + R > N$ overlaps by at least one node, reads are guaranteed to see the latest acknowledged write.

3. Conflict Resolution with Vector Clocks

In AP systems accepting concurrent writes across partitions, wall-clock timestamps (LWW / Last-Write-Wins) lose updates due to NTP clock drift.
Vector Clocks attach a logical version list [Node, Counter] to each object:
  • [S1: 1, S2: 1] is strictly newer than [S1: 1] (safe overwrite).
  • [S1: 2, S2: 1] vs. [S1: 1, S2: 2] have diverged concurrently (neither dominates). Both siblings are stored and returned to the client for domain-specific reconciliation (e.g., merging a user's shopping cart union).

4. Failure Detection & Anti-Entropy

  1. Decentralized Gossip Protocol: Every second, nodes exchange heartbeat counters with a few random peers. If a node's heartbeat stops advancing for T_fail seconds across peers, it is marked down without needing a centralized coordinator.
  2. Hinted Handoff: During temporary outages, a neighbor node buffers writes with a header hint and replays them once the failed node recovers.
  3. Merkle Trees: For permanent replica divergence, nodes exchange cryptographic hash trees over key ranges, pinpointing mismatched keys in $O(\log K)$ without transferring entire databases.

5. High-Speed Storage Engine: LSM Trees

To handle high write throughput without slow random disk seeks, nodes use Log-Structured Merge (LSM) Trees:
  1. Commit Log (WAL): Append write sequentially to disk immediately for crash durability.
  2. MemTable: Insert sorted key-value into in-memory skip list / balanced tree.
  3. SSTables: When MemTable hits threshold (e.g., 64 MB), flush immutably to disk as sorted files, paired with in-memory Bloom Filters so missing keys never touch disk.
  4. Compaction: Background threads merge SSTables and discard tombstones/overwrites.

Share this article

Comments