Storage & DataIntermediate~45 min

Design a Key-Value Store

Design a distributed key-value store that supports high throughput reads and writes. Consider data partitioning, replication, consistency models, and failure handling across a cluster of nodes.

You'll practice

storagedistributed

Functional Requirements

  • Support GET, PUT, and DELETE operations
  • Support TTL-based key expiration
  • Distribute data across multiple nodes

Non-Functional Requirements

  • Sub-5ms read/write latency at p99
  • Fault-tolerant with data replication
  • Scale to 500K+ operations per second

Frequently Asked Questions

What is consistent hashing and why use it?

Consistent hashing maps both keys and nodes to positions on a virtual ring. Each key is assigned to the nearest node clockwise on the ring. When a node is added or removed, only keys in its range need to be remapped — unlike simple hash partitioning where nearly all keys must move. This minimizes disruption during cluster scaling.

How do you handle node failures in a distributed key-value store?

Replicate each key to N nodes (e.g., 3). Use a quorum-based protocol: writes succeed if W of N replicas acknowledge, reads succeed if R of N respond, where W + R > N ensures consistency. Failed nodes are detected via heartbeats and replaced via hinted handoff or anti-entropy repair.

What consistency model should a key-value store use?

It depends on the use case. Strong consistency (linearizability) is easiest to reason about but adds latency. Eventual consistency offers better availability and performance but requires conflict resolution. Many systems offer tunable consistency — letting clients choose per-request based on their needs (e.g., Cassandra’s consistency levels).

How do you handle hot keys in a key-value store?

Hot keys (keys accessed far more than others) can overload a single partition. Solutions include read replicas for hot keys, client-side caching, key splitting (appending a random suffix to spread load across partitions), and dedicated caching layers. Monitoring access patterns helps detect hot keys early.

Ready to design this system?