Problem
Design and implement the architecture for an in-memory key-value cache with TTL (time-to-live) support. This cache will be used as an application-level caching layer to reduce database load and improve response times.
Requirements
- Set: Store a key-value pair with an optional TTL in seconds. If the key already exists, overwrite it.
- Get: Retrieve the value for a given key. Return null if the key does not exist or has expired.
- Delete: Remove a key-value pair from the cache.
- TTL Expiration: Keys with a TTL should automatically become inaccessible after expiration.
- Max Size: The cache has a maximum number of entries. When full, evict entries using an LRU (Least Recently Used) policy.
Constraints
- The cache is single-node (no distributed coordination needed).
- It should support up to 100,000 entries.
- Get and Set operations should be O(1) average time complexity.
- Memory usage should be predictable and bounded.
- The cache should handle concurrent reads safely (assume a single-threaded event loop like Node.js, or describe your concurrency strategy).
What to Design
- The internal data structures used for O(1) get/set and LRU eviction
- How TTL expiration is handled (lazy vs. active expiration)
- The eviction strategy when the cache reaches max capacity
- The public API interface
- How you would monitor cache hit/miss rates