Design a Scalable Key-Value Store with Interface, Memory Optimization, Sharding, Hashing etc.
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2022-03-10
Problem Statement
Design a high-performance, distributed key-value store similar to Redis, DynamoDB, or Memcached. The store should expose simple APIs (put(key, value), get(key), delete(key)) while ensuring scalability, fault-tolerance, and memory optimization. The system should support sharding, hashing, replication, and handle large-scale workloads.
Functional Requirements
1.Basic Operations
•put(key, value) → Insert or update a key-value pair.
•get(key) → Retrieve value for a given key.
•delete(key) → Remove a key-value pair.
2.Advanced Features
•Support time-to-live (TTL) for keys.
•Provide batch operations (mget, mput).
•Handle concurrent requests safely.
3.Distributed Features
•Sharding: Distribute keys across multiple nodes.
•Replication: Ensure data availability if a node fails.
•Leader election for consistency in replication.
Non-Functional Requirements
•Scalability → Store should handle billions of keys across thousands of nodes.
•Low Latency → Reads and writes should be <5ms in-memory.
•Consistency vs Availability → Configurable (support strong consistency or eventual consistency).
•Memory Optimization → Use compression, LRU eviction, compact data structures.
•Fault Tolerance → System should recover from node or network failures.
•High Availability → 99.99% uptime.
Design Considerations
1.Data Partitioning / Sharding
•Use consistent hashing to evenly distribute keys across nodes.
•Handle node additions/removals with minimal re-sharding.
2.Replication & Reliability
•Each shard replicated to multiple nodes (e.g., primary + replicas).
•Use quorum-based reads/writes (like DynamoDB) for consistency.
3.Memory Optimization
•Store values in in-memory data structures with optional persistence to disk.
•Implement eviction policies (LRU, LFU, TTL).
•Use bloom filters to optimize negative lookups.
4.Failure Handling
•Use a gossip protocol for membership detection.
•Automatic failover & leader election (via Raft/Zookeeper/etcd).
5.APIs & Interfaces
•REST or gRPC APIs: PUT /store/{key}, GET /store/{key}, DELETE /store/{key}.
•Support client libraries for Java, Python, Go.
6.Persistence & Durability
•Optional append-only log (AOF) or snapshotting to disk.
•Trade-off between durability and speed.