Design Distributed Cache System
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2022-05-24
Problem Statement
Design a high-performance distributed cache system to improve read/write latency for frequently accessed data, reduce database load, and scale horizontally across multiple nodes. The system should handle large amounts of data with high availability, fault tolerance, and consistency guarantees.
Functional Requirements (FR)
Cache Operations
•GET(key) → Retrieve value for a key.
•PUT(key, value) → Add or update a key-value pair.
•DELETE(key) → Remove a key-value pair.
Eviction Policy
•Support configurable eviction strategies: LRU, LFU, TTL (time-to-live).
•Automatically remove stale or least-used entries.
Data Partitioning
•Distribute keys across multiple cache nodes using consistent hashing.
•Support dynamic addition/removal of nodes.
Replication & Fault Tolerance
•Replicate data across multiple nodes for high availability.
•Ensure data consistency across replicas.
Persistence (Optional)
•Optionally persist hot keys to disk to recover from full cluster failure.
Monitoring & Metrics
•Track cache hits, misses, eviction count, and node health.
•Provide metrics for scaling decisions.
Non-Functional Requirements (NFR)
•Low Latency → Sub-millisecond read/write operations.
•High Throughput → Support thousands of operations per second.
•Scalability → Horizontally scalable to hundreds of nodes.
•Availability → No single point of failure, automatic failover.
•Consistency → Configurable: strong vs. eventual consistency.
Core Components
Cache Client
•Sends requests (GET, PUT) to cache nodes.
•Handles retries and failovers.
Cache Node
•Stores key-value pairs in memory.
•Implements eviction policies and replication logic.
Consistent Hashing Ring / Coordinator
•Maps keys to nodes.
•Handles node addition/removal with minimal rehashing.
Replication Manager
•Ensures copies of keys exist on multiple nodes.
•Handles synchronization between primary and replica nodes.
Persistence Layer (Optional)
•Stores snapshots or write-ahead logs for recovery.
Monitoring & Metrics Service
•Tracks cache statistics, node health, and usage metrics.
High-Level Flow
1.Client performs GET(key) → Consistent hashing → Target cache node.
2.Cache node checks memory → cache hit → return value.
3.Cache miss → fetch from backend database → store in cache → return value.
4.PUT(key, value) → Consistent hashing → Update primary node → replicate to backup nodes.
5.Eviction triggered if memory exceeds capacity → remove keys based on LRU/LFU policy.
Design Considerations
•Sharding → Use consistent hashing to distribute keys evenly.
•Replication → Synchronous vs. asynchronous replication for performance vs. consistency trade-off.
•Eviction → Optimize for memory usage and access patterns.
•Concurrency → Ensure thread-safe access to cache entries.
•Failure Handling → Automatic failover, rebalancing, and recovery from node crashes.
Evaluation Criteria
•Efficient key distribution and load balancing.
•Correct replication and fault tolerance handling.
•Optimized memory usage and eviction strategies.
•Clear scalability and monitoring architecture.
•Consideration for consistency, availability, and partition tolerance (CAP theorem).