Multi-Level Cache Management System
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-11-10
Problem Statement
Design a Multi-Level Cache Management System that supports two or more cache layers (e.g., L1, L2) with configurable capacities, eviction policies, and read/write strategies. The goal is to optimize data access latency and memory utilization.
🔷 Functional Requirements:
1.Read(key):
•Fetch the value for a given key.
•Check caches in order (L1 → L2 → ... → fallback).
•If found in a lower level (e.g., L2), promote it to the higher level(s) as per strategy.
2.Write(key, value):
•Write data into caches using a configurable write policy:
•WRITE_THROUGH: Write to all cache levels.
•WRITE_BACK: Write to L1 only, sync to lower levels lazily.
3.Eviction:
•Each level has its own capacity and eviction policy, e.g.:
•LRU (Least Recently Used)
•LFU (Least Frequently Used)
•FIFO
4.Delete(key):
•Remove the key from all levels.
5.Statistics (Optional):
•Track cache hits and misses at each level.
🔷 Constraints & Assumptions:
•The number of cache levels can be configured (minimum 2).
•Eviction should trigger when a cache exceeds its capacity.
•The underlying data store (e.g., DB or external source) can be simulated.
•No persistence or real-time syncing needed — use in-memory structures.
•Thread-safety not required unless specified.
🔶 Expected Design Focus:
•Core classes:
•MultiLevelCacheManager
•CacheLevel
•EvictionPolicy (interface)
•LRU, LFU, etc. (implementations)
•Use of interfaces, composition, and generics (if applicable).
•Clean separation of concerns:
•Cache logic vs eviction vs promotion logic.
•Follow OOP + SOLID principles.
•System should be easily extendable for more levels or new policies.
🔷 Bonus Features (Optional):
•TTL (Time To Live) support for key expiry.
•Persistence fallback (simulate DB).
•Async promotion between layers.
•Thread-safe version with locks or concurrent data structures.
✅ Sample Use Case:
1plaintext
2CopyEdit
3- Setup: L1 (capacity=2, LRU), L2 (capacity=3, FIFO), WRITE_THROUGH
4- Write("a", 1)
5- Write("b", 2)
6- Write("c", 3) → causes eviction in L1 ("a" or "b")
7- Read("a") → Miss in L1, found in L2 → promote to L1
8- Delete("b") → removed from all levels