Case study: Distributed key-value store

Topic 15 of 15 in Week 7: Classic case studies15 min readAdvancedFree course

Overview

This case study pulls together most of weeks 3 and 5: partitioning, replication, quorums, consistency, failure detection and storage engines. It’s modelled on Amazon’s Dynamo paper and Apache Cassandra. Interviewers use it to test depth rather than breadth.

Try the exercise first: 45 minutes, then compare.

1. Requirements

Functional

  • put(key, value), get(key), delete(key); values up to 1 MB; optional TTL.

Non-functional

  • Scale horizontally to hundreds of terabytes and millions of operations per second.
  • Highly available for reads and writes, even with node or zone failures (AP-leaning).
  • Low latency (single-digit milliseconds at p99 in a data centre).
  • Tunable consistency per request.

2. Estimates

Text
data       : 300 TB × 3 replicas ≈ 900 TB  → with ~4 TB usable per node ≈ 225+ nodes
throughput : 2M ops/s ÷ ~20k ops/s per node ≈ 100 nodes (storage dominates here)

3. API

Text
put(key, value, consistency = QUORUM, ttl = null) → ok | error
get(key, consistency = QUORUM)                    → value(s) + version
delete(key)                                       → writes a tombstone

4. High-level design

Distributed key-value store design. A client sends requests to a coordinator, which can be any node. The coordinator sends each request to the three replicas for key k: node A, the owner, node B, the next node clockwise, and node C, the next after B. A write succeeds after W equals 2 acknowledgements and a read asks R equals 2 replicas. On each node, data flows through an LSM engine: commit log, memtable, SSTables and compaction. Replicas are kept in sync with gossip to track who is alive, hinted handoff, and read repair with Merkle trees. Versions, as timestamps or vector clocks, resolve conflicts, and N, W and R are tuned per use case.

5. Deep dives

Partitioning

Keys are hashed onto a consistent-hashing ring (week 3). Each physical node owns many virtual nodes, so data and load spread evenly and a failed node’s share is picked up by many others. Adding a node moves only about 1/N of the data, streamed from its neighbours in the background.

Clients can route requests to any node (which becomes the coordinator for that request) or use a token-aware client that sends directly to a replica, saving a hop.

Replication

Each key is stored on N nodes: the owner and the next N−1 distinct physical nodes clockwise, chosen to be in different racks and availability zones. N = 3 is typical.

Tunable consistency

The coordinator sends writes to all N replicas and waits for W acknowledgements; reads ask replicas and wait for R replies (week 5, quorums).

  • QUORUM reads and writes (W = R = 2 with N = 3): reads see the latest acknowledged write and tolerate one replica down.
  • ONE: fastest and most available, possibly stale.
  • LOCAL_QUORUM: a majority within the local data centre, avoiding cross-region latency.

Versioning and conflicts

Concurrent writes to the same key on different replicas can conflict. Options:

  • Last write wins by timestamp: simple (Cassandra’s default) but can drop writes under clock skew.
  • Vector clocks (Dynamo): detect concurrent versions and return both to the client to merge, as with a shopping cart union.
  • CRDTs for data types that merge automatically (counters, sets).

Deletes write tombstones, which must be kept until all replicas have seen them (then removed by compaction), or deleted data can resurrect.

Failure detection and membership

  • Gossip: every second, each node exchanges membership and heartbeat information with a few random peers; information spreads quickly without a central coordinator, and nodes that stop heartbeating are suspected down.
  • Temporary failures: with a sloppy quorum, a write for an unavailable replica goes to another node with a hint; when the replica returns, hinted handoff delivers it.
  • Permanent divergence: read repair fixes stale replicas noticed during reads; anti-entropy compares replicas with Merkle trees (hash trees over key ranges), so only differing ranges are synchronised.

Storage engine

Each node uses an LSM tree (week 3): writes are appended to a commit log for durability and inserted into an in-memory memtable; full memtables are flushed as immutable sorted SSTables; compaction merges SSTables and drops overwritten values and expired tombstones. Bloom filters let reads skip SSTables that can’t contain the key; a row cache helps hot keys. Writes are fast and sequential, which suits write-heavy workloads.

6. Trade-offs and bottlenecks

  • Availability vs consistency: the AP default with tunable quorums lets each use case choose.
  • Last-write-wins is simple but lossy; vector clocks push merge logic to clients.
  • Compaction competes with live traffic for disk and CPU; throttle and schedule it.
  • Hot keys still land on N nodes; cache or split them at the application level.

7. Follow-up questions

  • Strongly consistent operations? Lightweight transactions (Paxos-based compare-and-set) for the few operations that need them, at a latency cost.
  • Range queries? Hash partitioning breaks key order; offer sort keys within a partition (Cassandra’s clustering columns) or use range partitioning instead.
  • Multi-region? Replicate per data centre with LOCAL_QUORUM for low latency and asynchronous cross-region replication.

Recap

A Dynamo-style key-value store partitions keys with consistent hashing and virtual nodes, replicates each key to N nodes across zones, and lets clients tune W and R to trade consistency for latency. Versions resolve conflicts; gossip, hinted handoff, read repair and Merkle-tree anti-entropy keep the cluster healthy and convergent; and each node stores data efficiently in an LSM tree.

Exercise

Design it yourself (45 minutes)

Set a 45-minute timer and design a distributed key-value store with get(key) and put(key, value): values up to 1 MB; hundreds of terabytes of data; millions of operations per second; it must stay available when nodes or a whole zone fail; consistency should be tunable per request. Use the framework, then compare.

Text
1. Clarifying questions:
2. Functional requirements:
3. Non-functional requirements:
4. Estimates:
5. API:
6. Partitioning:
7. Replication and consistency:
8. Failure handling:
9. Storage engine:
10. Trade-offs:

Summary

  • Partition keys across nodes with consistent hashing and virtual nodes.
  • Replicate each key to N nodes in different zones and tune W and R per request to trade consistency for latency.
  • Detect conflicts with versions (vector clocks or timestamps) and resolve them deliberately.
  • Use gossip for membership, hinted handoff for temporary failures, and read repair plus Merkle-tree anti-entropy to converge.
  • Each node stores data in an LSM tree: commit log, memtable, SSTables, compaction and Bloom filters.