0
0
RedisComparisonBeginner · 4 min read

LRU vs LFU Eviction in Redis: Key Differences and Usage

In Redis, 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.

FactorLRU (Least Recently Used)LFU (Least Frequently Used)
Eviction CriteriaRemoves keys not accessed recentlyRemoves keys used least often over time
Tracking MethodTracks last access timeTracks usage frequency counters
Memory OverheadLow (simple timestamps)Higher (counters per key)
Use CaseGood for temporal localityGood for frequency-based caching
ComplexitySimpler to implementMore complex due to counters
Redis Config Nameallkeys-lru or volatile-lruallkeys-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.

redis
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
Output
OK OK OK "value1" OK # Eviction happens when maxmemory is reached, least recently used keys removed
↔️

LFU Equivalent

Here is how you configure Redis to use LFU eviction policy and test eviction behavior.

redis
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
Output
OK OK OK "value1" "value1" "value1" OK # Eviction happens when maxmemory is reached, least frequently used keys removed
🎯

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.

Key Takeaways

LRU evicts keys based on how recently they were accessed, favoring recent usage.
LFU evicts keys based on how often they are accessed, favoring frequently used data.
LRU has lower memory overhead and is simpler to implement than LFU.
Configure Redis eviction policy with CONFIG SET maxmemory-policy to choose LRU or LFU.
Use LRU for workloads with temporal locality and LFU for workloads needing frequency-based caching.