Design a Distributed Key-Value Store

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2019-02-06

Problem Statement

Designing a highly available, fault-tolerant, and scalable distributed key-value store that supports massive amounts of reads and writes across geographically distributed data centers. The system should provide low latency, handle node failures gracefully, and maintain data consistency guarantees based on use cases.


✅ Functional Requirements


1.<span></span>Basic Operations
2.<span></span>put(key, value) → Store a key-value pair.
3.<span></span>get(key) → Retrieve a value by key.
4.<span></span>delete(key) → Remove a key-value pair.
5.<span></span>Advanced Operations (optional)
6.<span></span>Support for range queries (keys in sorted order).
7.<span></span>TTL (time-to-live) for expiring keys automatically.
8.<span></span>Batch operations (multi-put, multi-get).
9.<span></span>Consistency & Durability
10.<span></span>Configurable consistency levels (strong, eventual, quorum).
11.<span></span>Guarantee no data loss even in case of failures.


⚙️ Non-Functional Requirements


1.<span></span>Scalability → Store billions of keys, handle millions of ops/sec.
2.<span></span>Low latency → <10ms P95 for reads and writes.
3.<span></span>Fault tolerance → Survive data center or node failures.
4.<span></span>Availability → 99.99% uptime with automated failover.
5.<span></span>Replication → Data replicated across multiple nodes/regions.


🔐 Constraints & Assumptions


1.<span></span>Keys are unique and immutable (overwrite allowed).
2.<span></span>Value sizes are small to medium (KB–MB range).
3.<span></span>Nodes can fail at any time → system must handle recovery.
4.<span></span>Network partitions may occur → must decide between AP (Availability + Partition Tolerance) or CP (Consistency + Partition Tolerance) in CAP theorem.


🧱 Key Components to Design


1.<span></span>API Layer → Client library/REST API supporting put, get, delete.
2.<span></span>Partitioning/Sharding → Consistent hashing or range-based sharding for distributing keys.
3.<span></span>Replication → Master-slave or multi-leader replication for durability.
4.<span></span>Consensus Protocol → Paxos/Raft for strong consistency OR gossip protocol for eventual consistency.
5.<span></span>Storage Engine → LSM tree-based (like RocksDB) or B+ tree for persistence.
6.<span></span>Cluster Management → Node discovery, membership, leader election, rebalancing.
7.<span></span>Caching Layer → In-memory cache (Redis-like) for hot keys.
8.<span></span>Failure Recovery → Data re-replication, hinted handoff, anti-entropy (Merkle trees).


📈 SLAs & Success Metrics


1.<span></span>Availability: 99.99% uptime.
2.<span></span>Throughput: 1M+ ops/sec across cluster.
3.<span></span>Latency: <10ms for reads/writes (P95).
4.<span></span>Durability: No acknowledged write should ever be lost.


🔄 Trade-offs to Discuss


1.<span></span>Consistency vs Availability (AP system like Cassandra vs CP system like etcd).
2.<span></span>Replication strategy (synchronous vs asynchronous).
3.<span></span>Sharding method (consistent hashing vs range partitioning).
4.<span></span>Storage model (in-memory vs disk-based).
5.<span></span>Hotspot handling (popular keys skewing partitions).


👉 The interviewer expects you to:


1.<span></span>Draw an architecture diagram (Client → API → Partition Layer → Replicas → Storage).
2.<span></span>Explain failure handling (node crash, network partition, leader election).
3.<span></span>Discuss scaling strategy (adding/removing nodes, rebalancing).
4.<span></span>Compare with real-world systems (Redis Cluster, DynamoDB, Cassandra).


Solution

Comments