← Back to System Design, page 4

system-design-0073 · Hard senior

Design a distributed key-value store in the style of Amazon Dynamo or Cassandra that stays writable during failures and stores 10 TB with 1 million operations per second.

Partition keys with consistent hashing and virtual nodes, replicate each key to the next N nodes on the ring, and use tunable quorums (for example N = 3, W = 2, R = 2) so reads see the latest write while tolerating one failed replica. Handle temporary failures with hinted handoff, repair divergence with read repair and Merkle-tree anti-entropy, and spread membership with gossip. Each node stores data in an LSM tree for fast writes. 10 TB at 3 replicas is 30 TB, about 15 nodes by storage, and more with headroom.

Full answer

Requirements

Functional:

  • put(key, value), get(key), delete(key), values up to about 1 MB.
  • Per-request consistency level (fast and possibly stale, or quorum).

Non-functional:

  • Always writable, even when some nodes or a data centre link fail.
  • Scale out by adding nodes; no single point of failure.
  • Low latency: single-digit milliseconds at p99 inside a region.

Capacity estimates

Assumptions: 10 TB of data, replication factor 3, 2 TB usable disk per node, 100,000 operations per second per node, 1M operations per second total.

  • Stored data: 10 TB x 3 = 30 TB.
  • Nodes by storage: 30 / 2 = 15.
  • Nodes by throughput: each operation touches several replicas, but even counting 1M operations against 100,000 per node gives 10 nodes.
  • Storage decides: start with about 20 nodes to leave room for compaction space and growth.

API

PUT /kv/{key} body: value headers: Consistency: QUORUM, If-Match: <version> -> 200 {"version": ...} GET /kv/{key} Consistency: ONE | QUORUM | ALL -> value + version (or several siblings) DELETE /kv/{key} -> writes a tombstone

Data model

Each record: key, value, version (a vector clock or a timestamp with node ID), tombstone flag, TTL. On disk per node: commit log plus memtable plus SSTables (an LSM tree). Each node also keeps a Merkle tree per key range for repair.

High-level design

mermaid
flowchart LR
  C[Client] --> CO[Coordinator: any node]
  CO --> A[Replica A]
  CO --> B[Replica B]
  CO --> D[Replica C]
  A <-->|gossip| B
  B <-->|gossip| D
  A --> L[(Commit log, memtable, SSTables)]

How requests work

  • Placement. Hash the key onto a ring; walk clockwise to find the first N distinct physical nodes. Virtual nodes (many tokens per server) spread load evenly and make adding a node move small slices from many peers.
  • Write. The coordinator sends the write to all N replicas and returns success after W acknowledge.
  • Read. It asks R replicas and returns the newest version. With R + W > N (2 + 2 > 3), every read set overlaps every write set in at least one replica that has the latest write.
  • Hinted handoff. If a replica is down, another node stores the write with a hint and passes it on when the replica returns, so writes keep succeeding.
  • Repair. Read repair fixes stale replicas noticed during reads; background anti-entropy compares Merkle trees to find and sync differing ranges cheaply.
  • Conflicts. With last-write-wins timestamps, concurrent writes silently drop one; with vector clocks, the store returns siblings and the application merges them (Dynamo's shopping cart example).

Bottlenecks and trade-offs

  • Consistency versus latency. ONE is fastest and may be stale; QUORUM is safer; ALL loses availability when any replica is down.
  • Sloppy quorums keep writes available but mean R + W > N no longer guarantees overlap during failures.
  • Hot keys still land on N nodes; cache them or split them at the application level.
  • Tombstones must be kept longer than the repair interval, or deleted data comes back from a stale replica.
  • Compaction in LSM trees uses disk bandwidth and space; leave headroom and schedule it carefully.

By level

Fresher: Explain that keys are spread across servers with a hash ring and each key is copied to three servers. A fresher should say why copies keep data safe when a server fails.

Mid: Describe quorums with R + W > N, hinted handoff, read repair and the capacity math including replication. Explain virtual nodes.

Senior: Discuss sloppy quorums, vector clocks versus last-write-wins, tombstone timing, Merkle-tree repair and compaction headroom. Expect exact trade-offs between consistency levels.

Key points

  • Consistent hashing with virtual nodes for placement
  • Replicate to N nodes with tunable W and R; R + W > N gives overlap
  • Hinted handoff keeps writes available
  • Read repair and Merkle trees fix divergence
  • Gossip for membership; LSM storage for fast writes

Common mistakes

  • Claiming R + W > N always gives strong consistency even with sloppy quorums
  • Using last-write-wins without noting it can drop concurrent writes
  • Deleting data without tombstones
  • Sizing by throughput only and ignoring replication and compaction space

Follow-ups

  • How would you add a node with no downtime?
  • How do vector clocks detect concurrent writes?
  • How would you support range scans?

Tips

  • Walk through one write and one read with N, W, R
  • Explain hinted handoff with a failed node
  • Size by storage with the replication factor

Evaluation rubric

  • Correct partitioning and replication
  • Quorum reasoning
  • Failure handling and repair
  • Conflict resolution and capacity math