| Users / Requests | Cache Size | Eviction Policy Behavior | Performance Impact |
|---|---|---|---|
| 100 users | Small (MBs) | LRU or TTL works well; simple eviction | Low latency, minimal overhead |
| 10K users | Medium (GBs) | LRU may cause overhead; LFU helps with popular items; TTL for freshness | Moderate CPU for eviction; good hit rate |
| 1M users | Large (10s GBs) | LFU preferred for stable popularity; TTL combined to expire stale data | Eviction overhead grows; need efficient data structures |
| 100M users | Very Large (100s GBs+) | Distributed cache with TTL; hybrid policies; eviction done per shard | Network and coordination overhead; eviction must be distributed |
Cache eviction policies (LRU, LFU, TTL) in HLD - Scalability & System Analysis
The first bottleneck is the eviction algorithm's CPU and memory overhead as cache size grows.
LRU requires tracking usage order, which can be costly with large caches.
LFU needs frequency counters, increasing memory and CPU usage.
TTL requires timers or expiration checks, which can add overhead at scale.
- Horizontal scaling: Use distributed caches (e.g., Redis Cluster) to shard data and eviction.
- Approximate algorithms: Use approximate LRU/LFU (e.g., CLOCK, TinyLFU) to reduce overhead.
- TTL tuning: Set TTLs to limit stale data and reduce eviction complexity.
- Cache partitioning: Separate hot and cold data to apply different eviction policies.
- Eviction offloading: Use background threads or separate processes to handle eviction asynchronously.
- Requests per second (RPS):
- 1K users -> ~100-500 RPS
- 10K users -> ~1K-5K RPS
- 1M users -> ~100K RPS
- 100M users -> ~10M+ RPS (requires distributed caching)
- Storage needed:
- Small scale: MBs to GBs
- Large scale: 10s to 100s GBs or more
- Bandwidth:
- Eviction metadata updates add overhead
- Distributed cache requires network bandwidth for coordination
Start by explaining the eviction policies simply: LRU removes least recently used, LFU removes least frequently used, TTL removes after time expires.
Discuss how each policy affects CPU, memory, and complexity as cache size grows.
Identify the bottleneck (eviction overhead) and propose scaling solutions like distributed caching and approximate algorithms.
Use real numbers to show understanding of scale and trade-offs.
Your cache eviction algorithm handles 1000 QPS. Traffic grows 10x. What do you do first?
Answer: The eviction overhead will increase. First, consider switching to an approximate eviction algorithm (e.g., CLOCK or TinyLFU) to reduce CPU and memory usage, or shard the cache horizontally to distribute load.
Practice
Solution
Step 1: Understand LRU policy
LRU removes the cache entry that was accessed the longest time ago, prioritizing recent usage.Step 2: Compare with other policies
LFU removes least used items by frequency, TTL removes expired items by time, Random Replacement is arbitrary.Final Answer:
Least Recently Used (LRU) -> Option DQuick Check:
Oldest access time = LRU [OK]
- Confusing LRU with LFU
- Thinking TTL removes based on usage
- Assuming random removal is LRU
Solution
Step 1: Define TTL policy
TTL (Time To Live) removes cache entries after their set expiration time passes.Step 2: Contrast with other policies
LRU removes oldest used, LFU removes least used by frequency, random is arbitrary.Final Answer:
Removes data after a fixed expiration time -> Option AQuick Check:
TTL = expiration time removal [OK]
- Mixing TTL with LRU or LFU
- Thinking TTL depends on usage frequency
- Assuming TTL removes randomly
Solution
Step 1: Count frequency of each key
Key A accessed 3 times, B accessed 2 times, C accessed 1 time.Step 2: Identify LFU eviction candidate
LFU removes the least frequently used key, which is C with frequency 1.Final Answer:
Key C -> Option AQuick Check:
Lowest frequency = C [OK]
- Evicting most recent key instead of least frequent
- Confusing frequency with recency
- Evicting key D which is new
Solution
Step 1: Understand TTL eviction mechanism
TTL eviction requires checking if entries have expired before returning or during cleanup.Step 2: Identify why expired entries remain
If cache does not check expiration on access or cleanup, expired entries stay indefinitely.Final Answer:
The cache does not check expiration before access -> Option CQuick Check:
Expiration check missing = no TTL eviction [OK]
- Assuming low TTL causes no eviction
- Confusing TTL with LRU behavior
- Thinking cache size affects TTL eviction
Solution
Step 1: Analyze data usage pattern
Articles are popular shortly after publishing, then become stale after days.Step 2: Match eviction policy to pattern
TTL removes data after expiration time, ideal for removing stale articles automatically.Step 3: Compare with other policies
LRU and LFU focus on usage, but TTL ensures freshness by time. Random is inefficient.Final Answer:
Time To Live (TTL) -> Option BQuick Check:
Freshness by expiration time = TTL [OK]
- Choosing LRU or LFU ignoring data staleness
- Assuming random eviction is efficient
- Not considering time-based expiration
Ready to prove it?
Pass the test and this pattern moves unknown
