Design a Configurable Cache System with pluggable eviction strategy

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-12-09

Problem Statement

Design a cache system that stores key-value pairs in memory and supports pluggable eviction strategies. Focus is on OOP design, modularity, and efficient data structures.


Functional Requirements


1.Basic Operations
•put(key, value) → store or update a key-value pair.
•get(key) → retrieve the value associated with a key.
•delete(key) → remove a key-value pair from the cache.
2.Cache Capacity
•The cache has a maximum capacity.
•When capacity is reached, eviction strategy determines which entry to remove.
3.Pluggable Eviction Strategy
•Support multiple strategies:
•LRU (Least Recently Used)
•LFU (Least Frequently Used)
•FIFO (First In First Out)
•Ability to switch or configure strategy at runtime.
4.Optional Features
•TTL (Time-to-Live) per key.
•Statistics: hit rate, miss rate, eviction count.
•Thread-safe operations for concurrent access.


Non-Functional Requirements


•Performance: Fast get and put operations (ideally O(1) for LRU).
•Extensibility: Easy to add new eviction strategies.
•Scalability: Efficient memory usage for large number of keys.
•Concurrency: Safe for multi-threaded access if needed.


Edge Cases & Constraints


•Cache capacity = 0 → no storage, all operations fail.
•Updating an existing key → must update position for LRU/LFU.
•Concurrent access → handle race conditions for get and put.
•Eviction during put → must trigger strategy correctly.


Design Hints


•Key Classes / Interfaces:
•Cache<K, V> → main cache interface exposing get, put, delete.
•EvictionStrategy<K> → interface for pluggable eviction policies.
•Implementations: LRUStrategy, LFUStrategy, FIFOStrategy.
•CacheEntry<K, V> → stores key, value, metadata (frequency, timestamp).
•Data Structures:
•HashMap<K, CacheEntry> → O(1) lookup of key-value pairs.
•Doubly Linked List / Priority Queue / Heap → maintain eviction order per strategy.
•Counters / Timestamps → track usage for LFU or LRU.
•Design Patterns:
•Strategy Pattern → pluggable eviction logic.
•Observer Pattern (Optional) → notify metrics collector on eviction or hit/miss.
•Singleton (Optional) → centralized cache instance.


Example Flow


1.Cache initialized with capacity = 3, eviction strategy = LRU.
2.put("A", 1), put("B", 2), put("C", 3) → cache filled.
3.get("A") → updates access order.
4.put("D", 4) → triggers eviction of least recently used key (B).
5.Switch strategy to LFU → further puts evict least frequently used keys.


Evaluation Areas


•Clean class/interface separation between cache and eviction strategies.
•Efficient O(1) get/put for LRU, correct order maintenance for other strategies.
•Extensibility for TTL, new strategies, and metrics.
•Handling edge cases and concurrency if multi-threaded access is considered.


This LLD problem tests OOP design, design patterns (Strategy), efficient data structures, and modularity in building a configurable cache system.

Solution

Comments