Why Eviction Matters
When a cache reaches its maximum size, it must decide which entry to evict. This decision dramatically impacts hit rate:Studies show that W-TinyLFU achieves hit rates within 99% of the theoretical optimal (Belady’s algorithm) on real-world workloads.
Traditional Policies
- LRU (Least Recently Used)
- LFU (Least Frequently Used)
- W-TinyLFU
Evicts the entry that was accessed longest ago.Pros:
- Simple to implement
- Good for recency-heavy workloads
- Vulnerable to scan patterns
- Ignores frequency information
- Can evict popular items
Window TinyLFU Architecture
W-TinyLFU divides the cache into two regions:Window Region (1%)
New entries start in the window region:- Uses simple LRU eviction
- Captures entries with high temporal locality
- Protects against scan patterns
- Small size (typically 1% of cache)
Main Region (99%)
Long-lived entries move to the main region:- Uses Segmented LRU
- Divided into probation (20%) and protected (80%)
- Protected segment for proven popular entries
- Frequency-based admission control
Segmented LRU Details
Segmented LRU Details
Probation segment:
- Newly admitted entries from window
- Entry must prove its worth
- Can be quickly evicted if not accessed
- Frequently accessed entries
- Higher retention priority
- Only evicted under memory pressure
- Access in probation → moves to protected
- Protected fills up → demote to probation
TinyLFU Admission Policy
When the window is full, the victim competes with a candidate from the main region:A small amount of randomness (jitter) is added to prevent hash collision attacks from causing admission failures.
Frequency Sketch
The frequency sketch is a compact probabilistic data structure:Count-Min Sketch
Caffeine uses a 4-bit Count-Min Sketch implementation:Key Properties
Space Efficient
4 bits per counter, 16 counters per longOnly 4x cache size in counters
Time Efficient
O(1) query and update operationsTypically one memory access
Cache-Line Optimized
Counters constrained to 64-byte blocksMatches L1 cache line size
Probabilistic
93.75% confidence intervalError bound: ε / width
Frequency Aging
To adapt to changing workloads, frequencies are periodically halved:Aging happens every 10N operations (where N is the cache size), allowing old entries to fade away as workload patterns change.
Adaptive Window Sizing
The optimal window size depends on the workload:- Recency-biased workload: Larger window (capture temporal patterns)
- Frequency-biased workload: Smaller window (more space for popular items)
Adjust window size
Make small adjustments in both directions:
- Increase window by 1%
- Decrease window by 1%
Performance Characteristics
Time Complexity
| Operation | Complexity | Description |
|---|---|---|
| Frequency query | O(1) | 4 hash computations and lookups |
| Frequency increment | O(1) | 4 counter updates |
| Admission decision | O(1) | Two frequency queries + comparison |
| Aging | O(n) amortized | Happens every 10N operations |
Space Complexity
The frequency sketch adds only 2 bytes per entry on average, making W-TinyLFU extremely memory-efficient.
Handling Adversarial Patterns
W-TinyLFU includes protections against attack patterns:Hash Collision Attack
An attacker could exploit hash collisions to artificially inflate frequencies:Scan Pattern
Large sequential scans can pollute the cache:- Window region absorbs scan entries
- Frequency filter prevents admission to main
- Scan entries quickly evicted from window
- Popular entries protected in main region
Research Papers
W-TinyLFU is based on peer-reviewed research:TinyLFU: A Highly Efficient Cache Admission Policy
Gil Einziger, Roy Friedman, Ben ManesOriginal TinyLFU paper introducing the admission policy
Adaptive Software Cache Management
Gil Einziger, Ohad Eytan, Roy Friedman, Ben ManesAdaptive window sizing using hill climbing
Lightweight Robust Size Aware Cache Management
Gil Einziger, Ohad Eytan, Roy Friedman, Ben ManesExtensions for weighted entries and size-aware eviction
Comparison with Other Policies
Benchmark results on real-world traces:| Policy | Avg Hit Rate | Memory Overhead | Time Complexity |
|---|---|---|---|
| LRU | 68.2% | Minimal | O(1) |
| LFU | 71.5% | High (counters) | O(log n) |
| ARC | 74.1% | Moderate | O(1) |
| W-TinyLFU | 82.3% | Low | O(1) |
| Optimal (Belady) | 83.1% | N/A | N/A (offline) |
W-TinyLFU achieves 99% of the theoretical optimal hit rate while maintaining O(1) operations and minimal memory overhead.
Next Steps
Benchmarks
See performance measurements and comparisons
Architecture
Understand the internal implementation