Case Study 04 โ€” Key-Value Store

Design a simplified distributed key-value store (think Redis/Dynamo basics).

1. Problem

PUT key value, GET key, optional DELETE, across many nodes with large capacity.

2. Requirements

Functional

  • Get / Put / Delete
  • Optional TTL
  • Optional replication factor

Non-functional

  • Horizontal scale
  • High availability
  • Tunable consistency (quorum)

3. Back-of-the-envelope estimates

We do rough math so we know what to optimize. Exact precision is not the goal โ€” order of magnitude is.

Why we estimate (beginner tip)

Ask three questions:

  1. How busy? โ†’ QPS (requests per second)
  2. How much data? โ†’ storage (GB/TB)
  3. How fat is the pipe? โ†’ bandwidth (MB/s) when media matters

Cheat sheet: 1 QPS โ‰ˆ 86,400 requests/day. Peak is often 2โ€“5ร— average.

Assumptions (say these out loud)

  • 10 billion keys total in the cluster
  • Average value size 1 KB (range: bytes to a few KB)
  • 500k operations/sec at peak (GET + PUT + DELETE combined)
  • Read:write ratio โ‰ˆ 80:20 (reads dominate, like most caches/stores)
  • Replication factor N = 3 (each key on 3 nodes)
  • Quorum: W = 2 writes, R = 2 reads

Step A โ€” Traffic (QPS)

Total peak ops:                    500,000/s

Split by read:write (80:20):
  Read QPS  โ‰ˆ 400,000/s
  Write QPS โ‰ˆ 100,000/s

Per node (assuming 100 nodes, even partition):
  ~5,000 ops/s/node average
  Hot keys can skew โ€” use virtual nodes on consistent hash ring

Replication write amplification:
  Each PUT hits W=2 nodes โ†’ 100k logical writes โ‰ˆ 200k physical writes/s cluster-wide

Step B โ€” Storage

Logical data:
  10B keys ร— 1 KB avg value โ‰ˆ 10 TB

With overhead (key names, version vectors, metadata +30%):
  โ‰ˆ 13 TB logical

Replication factor N=3:
  13 TB ร— 3 replicas โ‰ˆ 39 TB total disk across cluster

Per node (100 nodes):
  ~390 GB/node โ€” fits on modern SSDs; plan for compaction headroom

Step C โ€” Bandwidth / other (if relevant)

Internal replication traffic on writes:

100k writes/s ร— 1 KB value ร— (N-1) replica copies โ‰ˆ 200 MB/s inter-node replication

Read egress to clients:
  400k reads/s ร— 1 KB โ‰ˆ 400 MB/s โ€” NIC and sharding matter at this scale

Not media-heavy, but inter-node bandwidth for replication and read repair is real.

Step D โ€” Read:write ratio

PathApprox shareImplication
GET (read)~80%Quorum read from R=2 replicas; cache hot keys locally
PUT (write)~15%Write to W=2; async replicate to 3rd
DELETE~5%Tombstones + compaction; same replication rules

What the numbers tell us

  • 10B keys, 10 TB logical โ†’ no single machine holds everything; consistent hashing partitions keys across ~100 nodes
  • 500k peak ops/s โ†’ in-memory memtable + append-only commit log (LSM) for write throughput
  • N=3 replication โ†’ survive 1 node failure; W+R > N gives strong consistency option
  • Write amplification from replication โ†’ budget 2โ€“3ร— disk I/O vs naive single-copy
  • Hot keys break even partition โ€” add virtual nodes and optional local cache on coordinators

Common mistake for this problem

Trying to store 10 billion keys on one Redis/Postgres instance because "we can add RAM later." At 10 TB + 500k ops/s, you must partition early and treat replication/quorum as first-class design choices.

4. HLD

Core ideas

  1. Partition keys across nodes with consistent hashing
  2. Replicate each key to N successors
  3. Quorum reads/writes (R, W)
N = 3 replicas
W = 2 write quorum
R = 2 read quorum
W+R > N โ†’ overlapping for stronger consistency

5. LLD

API

PUT    /v1/kv/{key}   body: raw bytes / JSON value
GET    /v1/kv/{key}
DELETE /v1/kv/{key}
Headers: X-TTL-Seconds: 60

Consistent hashing ring

hash(key) โ†’ position on ring
primary = first node clockwise
replicas = next N-1 nodes

Virtual nodes (many tokens per physical server) improve balance.

Data on a node

Simple approach for learning:

memtable (in memory) + append-only commit log
flush to SSTable segments on disk
compaction merges segments

(This is LSM intuition used by Cassandra/RocksDB-style stores.)

Versioning / conflict

Use vector clocks or timestamp+version:

value record:
  data
  version
  timestamp

On concurrent writes (rare with quorum), return conflict or last-write-wins (LWW) by timestamp โ€” trade-off.

Gossip & membership

Nodes share ring membership via gossip protocol; clients refresh topology.

Failure handling

  • Hinted handoff: if replica down, partner keeps hint and replays later
  • Read repair: on quorum read mismatch, push newest to stale replicas
  • Anti-entropy repair jobs

Pseudocode GET with quorum

nodes = replicaSet(key, N)
responses = parallel GET from nodes until R success
return newest(responses)
maybe async read_repair

6. Trade-offs table

ChoiceBenefitCost
LWWSimpleMay lose concurrent updates
Strong quorum alwaysSafer readsHigher latency
More replicasDurabilityCost, write amp

7. Recap

  • Consistent hashing for scale-out
  • Replication + quorum for availability/consistency knobs
  • LSM/commit-log for durable high write throughput

This case study builds intuition for Dynamo/Cassandra/Riak-style systems.