LRU vs LFU Eviction in Redis: Key Differences and Usage
LRU (Least Recently Used) eviction removes keys that haven't been accessed recently, while LFU (Least Frequently Used) eviction removes keys used least often. Both help manage memory by evicting keys, but LRU focuses on recent usage and LFU on usage frequency.Quick Comparison
This table summarizes the main differences between LRU and LFU eviction policies in Redis.
| Factor | LRU (Least Recently Used) | LFU (Least Frequently Used) |
|---|---|---|
| Eviction Criteria | Removes keys not accessed recently | Removes keys used least often over time |
| Tracking Method | Tracks last access time | Tracks usage frequency counters |
| Memory Overhead | Low (simple timestamps) | Higher (counters per key) |
| Use Case | Good for temporal locality | Good for frequency-based caching |
| Complexity | Simpler to implement | More complex due to counters |
| Redis Config Name | allkeys-lru or volatile-lru | allkeys-lfu or volatile-lfu |
Key Differences
LRU eviction in Redis removes keys that have not been accessed recently. It assumes that data accessed recently will likely be accessed again soon, so it evicts the least recently used keys first. This method uses timestamps to track when keys were last accessed, which requires minimal memory overhead.
On the other hand, LFU eviction removes keys that are used least frequently over time. It keeps counters for each key to record how often they are accessed. This approach is better when you want to keep frequently used data in cache regardless of recent access, but it uses more memory to maintain these counters.
In Redis, you can configure eviction policies using settings like allkeys-lru or allkeys-lfu. Choosing between them depends on whether your workload benefits more from recent access patterns (LRU) or frequency of use (LFU).
Code Comparison
Here is how you configure Redis to use LRU eviction policy and test eviction behavior.
CONFIG SET maxmemory 100mb CONFIG SET maxmemory-policy allkeys-lru # Add keys SET key1 value1 SET key2 value2 # Access key1 to make it recently used GET key1 # Add more keys to trigger eviction # Redis will evict least recently used keys first
LFU Equivalent
Here is how you configure Redis to use LFU eviction policy and test eviction behavior.
CONFIG SET maxmemory 100mb CONFIG SET maxmemory-policy allkeys-lfu # Add keys SET key1 value1 SET key2 value2 # Access key1 multiple times to increase frequency GET key1 GET key1 GET key1 # Add more keys to trigger eviction # Redis will evict least frequently used keys first
When to Use Which
Choose LRU eviction when your application benefits from caching recently accessed data, such as session stores or web caches where recent activity predicts future use. It is simpler and uses less memory.
Choose LFU eviction when your workload favors keeping frequently accessed data regardless of recency, like recommendation systems or analytics caches. Although it uses more memory for counters, it better preserves hot data.
In summary, use LRU for temporal locality and LFU for frequency-based caching needs.