Design a Distributed Key-Value Store (Dynamo / Cassandra)
Build a highly available, partition-tolerant key-value store — consistent hashing with virtual nodes, replication and preference lists, tunable quorums, versioning and conflict resolution with vector clocks or LWW, sloppy quorums and hinted handoff, anti-entropy with Merkle trees, gossip membership, and an LSM-tree storage engine.
1. Requirements
- API:
put(key, value),get(key),delete(key); values up to ~1 MB. - Highly available for writes ("always writeable" — e.g., shopping carts), partition tolerant → AP with tunable consistency.
- Horizontal scalability to hundreds of nodes and petabytes; incremental scaling.
- Low latency: p99 < 10 ms.
- No single point of failure; symmetric nodes (no master).
2. Architecture overview
3. Partitioning
Consistent hashing with ~256 virtual nodes per physical node (see probabilistic structures). Replica placement skips vnodes of the same physical node and spreads replicas across racks/AZs.
4. Replication and quorums
| Setting | Behaviour |
|---|---|
| N=3, W=1, R=1 | fastest, weakest — reads may be stale |
| N=3, W=2, R=2 | balanced; overlap → reads see latest acked write (no failures) |
| N=3, W=3, R=1 | fast reads, writes fail if any replica down |
Cassandra LOCAL_QUORUM | quorum within the local datacenter; async to others |
EACH_QUORUM | quorum in every DC (slow, strong across DCs) |
5. Versioning and conflicts
Text
Vector clocks for a shopping cart key:
v1 [A:1] {milk} written via node A
v2 [A:2] {milk, eggs} descends from v1 → v1 discarded
v3 [A:2, B:1] {milk, eggs, tea} ← concurrent with v4
v4 [A:3] {milk} (eggs removed)
read returns siblings v3 and v4 → app merges (e.g., union adds, tombstones for removes)
6. Storage engine (per node)
7. Repair and membership
- Read repair fixes replicas touched by reads.
- Anti-entropy: replicas build Merkle trees per token range and compare, streaming only differing ranges (Merkle trees).
- Gossip spreads membership and load information; phi accrual detects failures.
- Bootstrapping a new node: it takes vnodes from existing nodes and streams their data.
8. Trade-offs
| Choice | Benefit | Cost |
|---|---|---|
| Leaderless | no failover, writes always possible | conflicts, eventual consistency |
| Tunable quorums | per-request consistency | operators must understand it |
| LWW | simple | lost concurrent updates |
| Vector clocks | no silent loss | client merge complexity |
| LSM | write throughput | read/space amplification, compaction I/O |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.