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% hit rates for skewed access patterns.
- 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% for 1M keys).
- 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% hit rate, reduces DynamoDB load by 85%.
- 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%) and hit rate (> 90%) with Prometheus.
- 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% hit rate for hot data).
- 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% for 1M keys).
- 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% hit rate, reduces Cassandra load by 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% CPU overhead).
Limitations
- Low Hit Rate: Ignores access patterns, evicting frequently used items (e.g., 70% hit rate).
- 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% hit rate for hot data, 50% memory savings.
- 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% hit rate).
- 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% hit rate, optimized for scan-heavy analytics.
- 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% CPU overhead).
- 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% hit rate, reduces backend load by 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% memory savings).
- Simple Configuration: Easy to set TTL per key.
Limitations
- Hit Rate Dependency: Short TTLs reduce hit rates (e.g., 80% if TTL is too low).
- Cleanup Overhead: Background expiration checks consume CPU (e.g., 5% for 1M keys).
- 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% hit rate, reduces Cassandra load by 80%.
- 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% CPU).
- 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% hit rate, optimizes memory for large objects.
- 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%) | Medium (5%) | E-commerce | Amazon products |
LFU | Least frequent access | O(1)/O(log n) | High (95%) | High (10%) | Social Media | Twitter posts |
FIFO | Oldest insertion | O(1) | Low (70%) | Low (<2%) | Logging | Uber ride logs |
MRU | Most recent access | O(1) | Low (60%) | Low (<2%) | Analytics | Google Analytics |
RR | Random selection | O(1) | Medium (70%) | Minimal (<1%) | CDN | CDN assets |
TTL | Expiration time | O(1)/O(log n) | High (80–95%) | Medium (5%) | Sessions | Spotify sessions |
Size-Based | Item size | O(1)/O(log n) | Medium (70%) | Medium (5%) | Media Cache | Netflix metadata |
Key Observations
- Hit Rate: LFU achieves the highest hit rates (95%) for frequency-skewed workloads, followed by LRU (90%) for recency-based access. TTL varies (80–95%) based on expiration settings. FIFO, MRU, RR, and Size-Based have lower hit rates (60–70%) due to ignoring access patterns.
- Complexity: LRU, FIFO, MRU, and RR offer O(1) operations, while LFU, TTL, and Size-Based require O(log n) for heap-based eviction.
- Overhead: RR has minimal overhead (< 1%), FIFO and MRU are low (< 2%), LRU and TTL are medium (5%), and LFU and Size-Based are higher (5–10%).
- Use Case Fit: LRU and LFU suit read-heavy systems (e.g., e-commerce, social media). FIFO and Write-Around suit write-heavy logs. MRU is niche for scan-heavy analytics. RR is simple for uniform access. TTL is ideal for transient data. Size-Based optimizes for variable-sized data.
Trade-Offs and Strategic Considerations
These align with prior discussions on caching strategies and data structures:
- Hit Rate vs. Overhead:
- Trade-Off: LFU maximizes hit rate (95%) but has high overhead (10%). RR minimizes overhead (< 1%) but has lower hit rates (70%).
- Decision: Use LFU for high-frequency data, RR for low-overhead simplicity.
- Interview Strategy: Justify LFU for social media, RR for CDNs.
- Access Pattern Suitability:
- Trade-Off: LRU excels for temporal locality, LFU for frequency-based access. FIFO, MRU, and Size-Based are less effective for skewed patterns.
- Decision: Use LRU for sessions, LFU for popular content.
- Interview Strategy: Match LRU to e-commerce, FIFO to logs.
- Complexity vs. Simplicity:
- Trade-Off: LRU and LFU add tracking complexity, while FIFO, RR, and TTL are simpler.
- Decision: Use TTL for predictable lifetimes, LRU for dynamic access.
- Interview Strategy: Propose TTL for sessions, LRU for products.
- Memory Efficiency vs. Performance:
- Trade-Off: TTL and Size-Based save memory by evicting transient/large data, but may reduce hit rates. LRU/LFU maximize hit rates but use more memory.
- Decision: Use TTL for transient data, LRU for hot data.
- Interview Strategy: Highlight TTL for memory savings in sessions.
Integration with Prior Data Structures and Concepts
These policies leverage data structures and concepts from prior discussions:
- Hash Tables: Used in all policies for O(1) key lookups in Redis/Memcached.
- Doubly-Linked Lists: LRU/MRU for O(1) recency tracking.
- Min-Heaps: LFU and Size-Based for O(log n) eviction.
- Queues: FIFO for O(1) insertion order tracking.
- Distributed Caching: Redis Cluster with LRU/LFU/TTL/RR for scalability.
- Polyglot Persistence: Combines caching with databases like DynamoDB (B+ Trees), Cassandra (LSM Trees).
- Caching Strategies: LRU/LFU pair with Cache-Aside/Read-Through, TTL with Write-Around, FIFO with write-heavy systems.
Implementation Considerations
- Deployment: Use AWS ElastiCache (Redis/Memcached), Hazelcast Cloud, or Kubernetes-hosted clusters with 16GB RAM nodes.
- Configuration:
- LRU: allkeys-lru in Redis.
- LFU: allkeys-lfu with aging.
- FIFO/MRU: Custom queue/linked list implementation.
- RR: allkeys-random in Redis.
- TTL: SETEX with 300s–3600s expiration.
- Size-Based: Custom heap-based implementation.
- Performance Optimization:
- Cache hot data (top 1%) for 90% hit rate.
- Tune TTL or aging for dynamic workloads.
- Monitoring:
- Track hit rate (> 90%), eviction rate (< 1%), and latency (< 1ms) with Prometheus/Grafana.
- Monitor Redis INFO, Hazelcast Management Center.
- Security: Encrypt data with AES-256, use TLS 1.3, implement RBAC.
- Testing: Stress-test with redis-benchmark for 1M req/s, validate failover with Chaos Monkey.
Discussing in System Design Interviews
- Clarify Requirements:
- Ask: “What’s the access pattern (recency, frequency)? What’s the hit rate target (90%)? Is memory constrained?”
- Example: Confirm 10M req/day and need for session caching.
- Propose Policy:
- LRU: “Use for product caching in Amazon.”
- LFU: “Use for popular tweets in Twitter.”
- FIFO: “Use for ride logs in Uber.”
- MRU: “Use for analytics in Google.”
- RR: “Use for CDN assets.”
- TTL: “Use for sessions in Spotify.”
- Size-Based: “Use for media metadata in Netflix.”
- Example: “For Amazon, LRU for product data with 90% hit rate.”
- Address Trade-Offs:
- Explain: “LRU maximizes hit rate but has overhead. TTL saves memory but may evict useful data.”
- Example: “Use LFU for skewed access, FIFO for logs.”
- Optimize and Monitor:
- Propose: “Tune TTL to 300s, monitor hit rate with Prometheus.”
- Example: “Track Redis evicted_keys for LRU performance.”
- Handle Edge Cases:
- Discuss: “Handle scans with LFU, manage TTL expiration with background cleanup.”
- Example: “For Twitter, use LFU to retain popular tweets.”
- Iterate Based on Feedback:
- Adapt: “If memory is constrained, switch to TTL or Size-Based.”
- Example: “For Netflix, use Size-Based for large objects.”
Conclusion
The 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.