Introduction
Cache eviction policies are critical mechanisms in caching systems to manage limited memory by determining which items to remove when the cache reaches capacity. These policies ensure efficient use of cache memory, maintaining high hit rates and low latency in high-performance applications such as e-commerce, social media, and streaming platforms. The seven primary cache eviction policies are Least Recently Used (LRU), Least Frequently Used (LFU), First-In-First-Out (FIFO), Most Recently Used (MRU), Random Replacement (RR), Time-To-Live (TTL), and Size-Based Eviction. Each policy uses distinct criteria to prioritize data retention, balancing trade-offs like performance, complexity, and hit rate optimization. This detailed analysis explores these policies, covering their mechanisms, applications, advantages, limitations, real-world examples, implementation considerations, and a comparative analysis, integrating insights from prior discussions on caching strategies, distributed caching, and data structures for technical depth and practical guidance.
Cache Eviction Policies
1. Least Recently Used (LRU)
Mechanism
LRU evicts the least recently accessed item when the cache is full, assuming recently used items are more likely to be accessed again.
- Operation:
- Tracks access recency using a doubly-linked list or timestamp.
- On access (GET or SET), moves the item to the front (most recently used).
- On eviction, removes the item at the tail (least recently used).
- Data Structures:
- Hash Table: O(1) key lookups.
- Doubly-Linked List: O(1) updates for recency tracking.
- Complexity: O(1) for access and eviction with proper implementation.
- Usage in Caches: Redis (allkeys-lru), Memcached.
Applications
- E-Commerce: Caches product data in Redis (e.g., Amazon product pages).
- Web Applications: Caches session data for active users.
- Microservices: Used in Cache-Aside or Read-Through strategies.
- In-Memory Databases: Redis for caching hot data.
Advantages
- High Hit Rate: Prioritizes recently accessed items, achieving 90
- Efficient for Temporal Locality: Works well for workloads where recent data is frequently reused (e.g., user sessions).
- Low Latency: O(1) operations with hash table and linked list.
Limitations
- Ineffective for Frequency-Based Patterns: May evict frequently accessed but older items.
- Overhead: Maintaining recency list adds memory/CPU overhead (e.g., 5
- Scan Resistance: Vulnerable to one-time scans evicting useful data.
Real-World Example
- Amazon Product Pages:
- Context: 10M requests/day, needing < 1ms latency.
- Usage: Redis with allkeys-lru caches product:123, evicting least recently accessed products.
- Performance: 90
- Implementation: Redis Cluster with 16,384 slots, monitored via CloudWatch.
Implementation Considerations
- Cache: Configure Redis with maxmemory-policy allkeys-lru.
- Monitoring: Track eviction rate (< 1
- Optimization: Use fixed-size caches (e.g., 1GB for 1M keys) to limit overhead.
- Security: Encrypt cache data with AES-256.
2. Least Frequently Used (LFU)
Mechanism
LFU evicts the least frequently accessed item, prioritizing items with higher access counts.
- Operation:
- Tracks access frequency using a counter per key.
- On access, increments counter; on eviction, removes the item with the lowest count.
- May use aging to decay counts over time (e.g., Redis LFU).
- Data Structures:
- Hash Table: O(1) key lookups.
- Min-Heap: O(log n) for finding least frequent item.
- Complexity: O(1) for access, O(log n) for eviction with heap.
- Usage in Caches: Redis (allkeys-lfu), Caffeine.
Applications
- Social Media: Caches popular posts in Redis (e.g., Twitter).
- Analytics: Caches frequently accessed metrics in Memcached.
- Search Engines: Caches Elasticsearch results for popular queries.
- Microservices: Used in Read-Through for high-frequency data.
Advantages
- High Hit Rate for Skewed Access: Excels for workloads with frequent access to a small subset (e.g., 95
- Robust to Scans: Resists one-time scans evicting popular items.
- Long-Term Patterns: Retains frequently accessed items over time.
Limitations
- Overhead: Frequency counters and heap maintenance increase memory/CPU usage (e.g., 10
- Cold Start Issue: New items may be evicted quickly if frequency is low.
- Aging Complexity: Requires decay logic to handle changing access patterns.
Real-World Example
- Twitter Posts:
- Context: 500M tweets/day, needing high throughput.
- Usage: Redis with allkeys-lfu caches tweet:789, evicting least frequently accessed tweets.
- Performance: 90
- Implementation: Redis Cluster with async Cassandra updates, monitored via Prometheus.
Implementation Considerations
- Cache: Configure Redis with maxmemory-policy allkeys-lfu, enable aging.
- Monitoring: Track eviction rate and frequency distribution with Grafana.
- Optimization: Tune decay factor to balance new and old items.
- Security: Use TLS for cache access.
3. First-In-First-Out (FIFO)
Mechanism
FIFO evicts the oldest item (first added to the cache), regardless of access frequency or recency.
- Operation:
- Tracks insertion order using a queue.
- On insertion, adds item to the tail; on eviction, removes from the head.
- Data Structures:
- Hash Table: O(1) key lookups.
- Queue: O(1) insertion and eviction.
- Complexity: O(1) for access and eviction.
- Usage in Caches: Custom implementations, less common in Redis/Memcached.
Applications
- Time-Series Databases: Caches metrics in InfluxDB with fixed retention.
- Logging Systems: Caches log entries with sequential access.
- Microservices: Used for transient data with predictable lifetime.
- Write-Around: Suits write-heavy workloads with FIFO eviction.
Advantages
- Simplicity: Minimal overhead for tracking order (O(1) operations).
- Predictable Eviction: Deterministic removal based on insertion time.
- Low Overhead: No recency or frequency tracking (e.g., < 2
Limitations
- Low Hit Rate: Ignores access patterns, evicting frequently used items (e.g., 70
- Ineffective for Locality: Poor for workloads with temporal or frequency skew.
- Limited Use Cases: Less versatile than LRU/LFU.
Real-World Example
- Uber Ride Logs:
- Context: 1M logs/day, write-heavy with occasional reads.
- Usage: Redis with FIFO evicts oldest logs, paired with Write-Around to Cassandra.
- Performance: 80
- Implementation: Custom FIFO queue in Redis, monitored via CloudWatch.
Implementation Considerations
- Cache: Implement custom FIFO queue or use Redis lists.
- Monitoring: Track eviction rate and hit rate with Prometheus.
- Optimization: Use for fixed-lifetime data (e.g., logs).
- Security: Encrypt cache data with AES-256.
4. Most Recently Used (MRU)
Mechanism
MRU evicts the most recently accessed item, assuming recent items are less likely to be reused in specific workloads.
- Operation:
- Tracks recency with a doubly-linked list or timestamp.
- On access, moves item to the front; on eviction, removes the most recent item.
- Data Structures:
- Hash Table: O(1) key lookups.
- Doubly-Linked List: O(1) recency updates.
- Complexity: O(1) for access and eviction.
- Usage in Caches: Rare, used in niche systems like database buffer pools.
Applications
- Database Systems: Caches query results in PostgreSQL buffer pools for scan-heavy workloads.
- Analytics: Caches transient analytics data with one-time access patterns.
- Microservices: Used in Cache-Aside for specific access patterns.
- Search Engines: Caches one-off search results.
Advantages
- Effective for Scan-Heavy Workloads: Retains older data for sequential access (e.g., analytics).
- Low Overhead: O(1) operations with simple tracking.
- Niche Optimization: Suits workloads where recent data is transient.
Limitations
- Low Hit Rate: Evicts recently used items, poor for temporal locality (e.g., 60
- Limited Applicability: Rarely used outside specific scenarios.
- Ineffective for Hot Data: Discards frequently accessed items.
Real-World Example
- Google Analytics:
- Context: Processes 1B queries/day, with transient query results.
- Usage: Custom MRU cache evicts recent queries, retaining older data for batch processing.
- Performance: 60
- Implementation: Custom cache with MRU, integrated with Bigtable, monitored via Google Cloud Monitoring.
Implementation Considerations
- Cache: Implement custom MRU with linked list.
- Monitoring: Track hit rate and eviction patterns with Grafana.
- Optimization: Use for scan-heavy or one-off access patterns.
- Security: Encrypt cache data with TLS.
5. Random Replacement (RR)
Mechanism
RR evicts a random item when the cache is full, using no access or insertion history.
- Operation:
- Selects a random key for eviction (e.g., using a random number generator).
- Data Structures:
- Hash Table: O(1) key lookups.
- Array/Set: O(1) random selection.
- Complexity: O(1) for access and eviction.
- Usage in Caches: Redis (allkeys-random), Memcached (rare).
Applications
- Web Applications: Caches session data with uniform access patterns.
- Microservices: Used in Cache-Aside for simple workloads.
- Content Delivery: Caches CDN assets with unpredictable access.
- In-Memory Databases: Redis for low-overhead caching.
Advantages
- Minimal Overhead: No tracking of recency or frequency (e.g., < 1
- Simplicity: Easy to implement and maintain.
- Fair for Uniform Access: Performs adequately for random access patterns.
Limitations
- Low Hit Rate: Ignores access patterns, leading to suboptimal hit rates (e.g., 70
- Unpredictable Performance: Random evictions may remove hot data.
- Ineffective for Skewed Access: Poor for temporal or frequency-based workloads.
Real-World Example
- Content Delivery Network (CDN):
- Context: Serves 1B asset requests/day with uniform access.
- Usage: Redis with allkeys-random caches assets, evicting randomly.
- Performance: 70
- Implementation: Redis Cluster, monitored via CloudWatch.
Implementation Considerations
- Cache: Configure Redis with maxmemory-policy allkeys-random.
- Monitoring: Track hit rate and eviction rate with Prometheus.
- Optimization: Use for uniform access workloads.
- Security: Use TLS for cache access.
6. Time-To-Live (TTL)
Mechanism
TTL evicts items after a predefined expiration time, regardless of access patterns.
- Operation:
- Assigns a TTL (e.g., 300s) to each key on insertion (e.g., SETEX product:123 300 {data}).
- Expired items are removed lazily (on access) or via background cleanup.
- Data Structures:
- Hash Table: O(1) key lookups.
- Timer/Heap: Tracks expiration times (O(log n) for cleanup).
- Complexity: O(1) for access, O(log n) for expiration checks.
- Usage in Caches: Redis (SETEX), Memcached.
Applications
- E-Commerce: Caches session data with fixed lifetimes (e.g., Amazon carts).
- Web Applications: Caches temporary tokens or API responses.
- Microservices: Used in Cache-Aside or Read-Through for transient data.
- Distributed Systems: Caches event-driven data with expiration.
Advantages
- Predictable Lifespan: Ensures data is removed after TTL, preventing staleness.
- Memory Efficiency: Automatically clears transient data (e.g., 50
- Simple Configuration: Easy to set TTL per key.
Limitations
- Hit Rate Dependency: Short TTLs reduce hit rates (e.g., 80
- Cleanup Overhead: Background expiration checks consume CPU (e.g., 5
- Not Access-Based: Ignores recency or frequency, evicting useful data.
Real-World Example
- Spotify Sessions:
- Context: 100M user sessions/day, needing temporary caching.
- Usage: Redis with 300s TTL for session:abc123, evicting expired sessions.
- Performance: 95
- Implementation: Redis Cluster with SETEX, monitored via Prometheus.
Implementation Considerations
- Cache: Use Redis SETEX or Memcached TTL settings.
- Monitoring: Track expiration rate and hit rate with Grafana.
- Optimization: Tune TTL (e.g., 300s for sessions, 3600s for static data).
- Security: Encrypt cache data with AES-256.
7. Size-Based Eviction
Mechanism
Size-Based Eviction evicts items based on their size (e.g., memory footprint) to optimize cache space, often prioritizing smaller or larger items.
- Operation:
- Tracks item size (e.g., bytes) on insertion.
- Evicts largest or smallest items based on policy (e.g., evict largest to free space quickly).
- Data Structures:
- Hash Table: O(1) key lookups.
- Heap: O(log n) for tracking largest/smallest items.
- Complexity: O(1) for access, O(log n) for eviction.
- Usage in Caches: Custom implementations, less common in Redis/Memcached.
Applications
- Content Delivery: Caches large assets (e.g., images) in CDNs.
- Analytics: Caches variable-sized query results in Bigtable.
- Microservices: Used in Cache-Aside for memory-constrained systems.
- Object Stores: Caches objects with size-based prioritization.
Advantages
- Memory Optimization: Evicts large items to free space quickly (e.g., 1MB item vs. 1KB).
- Customizable: Can prioritize small or large items based on workload.
- Efficient for Heterogeneous Data: Suits caches with varying item sizes.
Limitations
- Low Hit Rate: Ignores access patterns, leading to suboptimal hit rates (e.g., 70
- Overhead: Tracking sizes and heap maintenance adds complexity (e.g., 5
- Limited Use Cases: Less effective for uniform-sized data.
Real-World Example
- Netflix Media Cache:
- Context: 100M streaming requests/day, caching variable-sized media metadata.
- Usage: Custom size-based cache evicts largest metadata entries.
- Performance: 70
- Implementation: Custom cache integrated with Cassandra, monitored via CloudWatch.
Implementation Considerations
- Cache: Implement custom size-based eviction with heap.
- Monitoring: Track size distribution and hit rate with Prometheus.
- Optimization: Prioritize large item eviction for quick memory recovery.
- Security: Use TLS for cache access.
Comparative Analysis
Policy | Eviction Criteria | Complexity | Hit Rate | Overhead | Use Case | Real-World Example |
---|---|---|---|---|---|---|
LRU | Least recent access | O(1) | High (90
Key Observations
Trade-Offs and Strategic ConsiderationsThese align with prior discussions on caching strategies and data structures:
Integration with Prior Data Structures and ConceptsThese policies leverage data structures and concepts from prior discussions:
Implementation Considerations
Discussing in System Design Interviews
ConclusionThe seven cache eviction policies—LRU, LFU, FIFO, MRU, RR, TTL, and Size-Based—offer distinct approaches to managing cache memory, each suited to specific workloads. LRU and LFU excel for high hit rates in read-heavy systems (e.g., Amazon, Twitter), FIFO and Write-Around suit write-heavy logs (e.g., Uber), MRU is niche for scan-heavy analytics (e.g., Google), RR is simple for uniform access (e.g., CDNs), TTL is ideal for transient data (e.g., Spotify), and Size-Based optimizes for variable-sized data (e.g., Netflix). Integration with data structures like hash tables and heaps, and concepts like distributed caching and polyglot persistence, enhances their effectiveness. Trade-offs in hit rate, overhead, and complexity guide strategic choices, ensuring scalable, low-latency caching solutions for modern applications. |