{}DSA Atlas

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.

Advanced5 interview questions

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

A B C D E F hash(k) N = 3 → B, C, DW = 2, R = 2 Techniques partitioning → consistent hashing + vnodes replication → N successors (rack/AZ-aware) consistency → quorums W + R > N versioning → LWW or vector clocks temporary failure → hinted handoff permanent failure → Merkle-tree repair membership → gossip + phi detector storage → commit log + LSM tree
The key is stored on the first N distinct nodes clockwise from its hash.

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

SettingBehaviour
N=3, W=1, R=1fastest, weakest — reads may be stale
N=3, W=2, R=2balanced; overlap → reads see latest acked write (no failures)
N=3, W=3, R=1fast reads, writes fail if any replica down
Cassandra LOCAL_QUORUMquorum within the local datacenter; async to others
EACH_QUORUMquorum in every DC (slow, strong across DCs)
client coordinator B ✓ ack C ✗ down D ✓ ack E stores hint"for C" W=2 reached → success
Writes stay available; the hint is replayed to C when it returns.

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)

write commit log (append) memtable (sorted) SSTable L0 SSTable L1 SSTable L2 flush compaction merges, drops tombstones read: memtable → (Bloom filter says maybe?) → SSTable index → block cache → disk newest version wins across levels
Sequential writes, mergeable immutable files, Bloom filters to keep reads fast.

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

ChoiceBenefitCost
Leaderlessno failover, writes always possibleconflicts, eventual consistency
Tunable quorumsper-request consistencyoperators must understand it
LWWsimplelost concurrent updates
Vector clocksno silent lossclient merge complexity
LSMwrite throughputread/space amplification, compaction I/O

Interview questions

0 / 5 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.