Distributed cache system

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-01-12

Problem Statement

Design a Distributed Cache System that provides low-latency access to frequently used data in a scalable, fault-tolerant, and consistent manner. The system should support high throughput, replication, eviction policies, and integration with backend databases.


Functional Requirements (FR)


1.Cache Operations
•GET key – Retrieve cached data.
•SET key value – Store data in the cache.
•DEL key – Delete cache entry.
•Optional: EXPIRE key ttl – Set time-to-live for keys.
2.Consistency & Replication
•Replicate cache entries across multiple nodes for fault tolerance.
•Provide strong or eventual consistency depending on use case.
3.Eviction Policies
•Handle cache eviction when memory is full using policies like LRU, LFU, or TTL.
4.Sharding / Partitioning
•Distribute keys across multiple cache nodes to balance load.
•Ensure minimal impact when nodes are added or removed.
5.Persistence (Optional)
•Persist cache to disk for recovery after failures.
6.Integration with Backend
•Act as a caching layer in front of databases or services.
•Support read-through, write-through, and write-back caching strategies.


Non-Functional Requirements (NFR)


•Low Latency – Microsecond to millisecond response times for reads/writes.
•High Throughput – Handle millions of requests per second.
•Scalability – Scale horizontally by adding more cache nodes.
•Availability – Replication and failover for minimal downtime.
•Consistency vs Performance – Trade-offs between strict consistency and low latency.


Core Components


1.Client Interface / API
•Provides standard cache operations (GET, SET, DEL, EXPIRE).
2.Cache Nodes
•In-memory storage for key-value data.
•Handle read/write requests and replication.
3.Partitioning / Sharding Module
•Distributes keys across cache nodes using consistent hashing.
4.Replication Module
•Ensures cache entries are replicated to other nodes for fault tolerance.
•Supports synchronous or asynchronous replication.
5.Eviction Manager
•Monitors memory usage and evicts entries based on policies (LRU, LFU, TTL).
6.Cluster Manager / Coordinator
•Keeps track of nodes, membership, and health.
•Handles node addition, removal, and failover.
7.Persistence Layer (Optional)
•Writes snapshot or logs to disk for recovery.


Data Flow


1.Client sends GET/SET request → Partitioning Module identifies target cache node.
2.Cache Node processes request → checks local memory → returns value (or writes value).
3.Replication Module replicates updated entries to backup nodes.
4.Eviction Manager removes entries if memory limit exceeded.
5.Cluster Manager monitors node health → redistributes keys if a node fails.


Design Considerations


Partitioning

•Consistent hashing to distribute keys evenly and reduce reshuffling on node changes.

Replication & Consistency

•Synchronous replication: strong consistency, higher latency.
•Asynchronous replication: eventual consistency, lower latency.

Eviction Policy Trade-offs

•LRU: removes least recently used, simple to implement.
•LFU: removes least frequently used, more memory overhead.
•TTL: removes expired items, lightweight but may evict useful data prematurely.

Scalability & Availability

•Horizontal scaling: add nodes to increase capacity.
•Failover: redirect traffic to replicas if a node goes down.

Integration Strategies

•Read-through: fetch from DB if cache miss, populate cache.
•Write-through: write to cache and DB simultaneously.
•Write-back: write to cache first, persist to DB asynchronously.


✅ This HLD problem tests:


•Distributed system design principles (sharding, replication, failover)
•Trade-offs between consistency, latency, and availability
•Eviction strategies and memory management
•Integration with backend storage for performance optimization


Solution

Comments