0
0
HLDsystem_design~10 mins

Cache eviction policies (LRU, LFU, TTL) in HLD - Scalability & System Analysis

Choose your learning style9 modes available
Scalability Analysis - Cache eviction policies (LRU, LFU, TTL)
Growth Table: Cache Eviction Policies at Different Scales
Users / RequestsCache SizeEviction Policy BehaviorPerformance Impact
100 usersSmall (MBs)LRU or TTL works well; simple evictionLow latency, minimal overhead
10K usersMedium (GBs)LRU may cause overhead; LFU helps with popular items; TTL for freshnessModerate CPU for eviction; good hit rate
1M usersLarge (10s GBs)LFU preferred for stable popularity; TTL combined to expire stale dataEviction overhead grows; need efficient data structures
100M usersVery Large (100s GBs+)Distributed cache with TTL; hybrid policies; eviction done per shardNetwork and coordination overhead; eviction must be distributed
First Bottleneck

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.

Scaling Solutions
  • 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.
Back-of-Envelope Cost Analysis
  • 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
Interview Tip

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.

Self Check

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.

Key Result
Cache eviction policies face CPU and memory overhead as cache size and traffic grow; approximate algorithms and distributed caching help scale efficiently.