What are the common cache eviction policies in Redis (LRU, LFU, TTL)?
Understand Redis eviction policies — LRU, LFU, TTL and noeviction — how maxmemory works, and how to pick the right strategy to keep your cache fast.
Expected Interview Answer
Eviction policies decide which keys Redis removes when memory reaches the maxmemory limit, using strategies like LRU (least recently used), LFU (least frequently used), TTL-based expiry, or random removal.
You configure behavior with maxmemory and maxmemory-policy. Policies split into two families: volatile-* only evict keys that have an expiry set (volatile-lru, volatile-lfu, volatile-ttl, volatile-random), while allkeys-* consider every key (allkeys-lru, allkeys-lfu, allkeys-random). The noeviction default rejects writes instead of removing data. Redis uses approximate LRU/LFU by sampling a few keys rather than scanning the whole keyspace, trading perfect accuracy for speed.
- Keeps memory usage bounded under load
- LRU favors recency, ideal for session-like access
- LFU favors popularity, ideal for hot-key workloads
- volatile-ttl prioritizes soon-to-expire keys
- noeviction protects data when eviction is unacceptable
AI Mentor Explanation
Think of a team dressing room with only twelve pegs for kit bags. When a new player arrives, LRU means removing the bag of whoever has not batted or bowled recently, LFU means removing the bag of whoever features in the fewest games, and TTL-based eviction means clearing the loan player whose contract expires soonest first.
Step-by-Step Explanation
Step 1
Set a memory cap
Configure maxmemory (e.g. maxmemory 512mb) so Redis knows the ceiling that triggers eviction.
Step 2
Choose a policy
Set maxmemory-policy to a volatile-* or allkeys-* strategy, or noeviction to reject writes instead.
Step 3
Decide the key scope
volatile-* only evicts keys with a TTL; allkeys-* can evict any key regardless of expiry.
Step 4
Pick the eviction signal
LRU tracks recency, LFU tracks access frequency, volatile-ttl targets nearest expiry, random picks arbitrarily.
Step 5
Tune LFU if used
Adjust lfu-log-factor and lfu-decay-time so frequency counters age appropriately for your traffic.
Step 6
Monitor evictions
Watch INFO stats evicted_keys and keyspace_misses to confirm the policy matches real access patterns.
What Interviewer Expects
- Knows the difference between volatile-* and allkeys-* families
- Can explain LRU vs LFU trade-offs
- Understands maxmemory and maxmemory-policy configuration
- Aware that Redis LRU/LFU is approximate via sampling
- Knows noeviction is the default and rejects writes
Common Mistakes
- Assuming Redis evicts keys with a TTL automatically even without a memory limit
- Thinking LRU and LFU are exact rather than sampled approximations
- Confusing volatile-lru with allkeys-lru and losing untagged keys unexpectedly
- Forgetting that noeviction causes write errors under memory pressure
- Believing eviction and expiry are the same mechanism
Best Answer (HR Friendly)
“When Redis runs out of its allotted memory, it needs a rule for what to throw away. It can remove whatever was used least recently, whatever is used least often, or whatever is due to expire soonest, so the cache stays fast without running out of space.”
Code Example
# redis.conf or via CONFIG SET
CONFIG SET maxmemory 512mb
CONFIG SET maxmemory-policy allkeys-lru
# Prefer frequency-based eviction for hot-key workloads
CONFIG SET maxmemory-policy allkeys-lfu
CONFIG SET lfu-log-factor 10
CONFIG SET lfu-decay-time 1
# Inspect how many keys were evicted
INFO stats | grep evicted_keysFollow-up Questions
- How does Redis approximate LRU instead of tracking it exactly?
- When would you choose allkeys-lfu over allkeys-lru?
- What happens to writes under the noeviction policy?
- How do lfu-log-factor and lfu-decay-time affect eviction?
- How is key expiration different from eviction in Redis?
MCQ Practice
1. Which policy only evicts keys that have an expiry set?
volatile-* policies restrict eviction to keys that carry a TTL, leaving persistent keys untouched.
2. What does Redis do by default when memory is full and no policy is set?
The default maxmemory-policy is noeviction, which returns errors on writes rather than removing data.
3. Which policy best suits a workload with a few very hot keys?
LFU keeps frequently accessed keys resident, making allkeys-lfu ideal for hot-key access patterns.
Flash Cards
What triggers eviction in Redis? — Memory usage reaching the configured maxmemory limit.
volatile-* vs allkeys-*? — volatile-* only evicts keys with a TTL; allkeys-* can evict any key.
Is Redis LRU exact? — No, it is approximate — Redis samples a few keys rather than scanning all.
Default eviction policy? — noeviction — writes are rejected with an error instead of evicting.
LRU vs LFU? — LRU evicts least recently used; LFU evicts least frequently used.