What if your system could decide by itself which data to forget to keep running fast?
Why Eviction policies (LRU, LFU, random) in Redis? - Purpose & Use Cases
Imagine you have a small shelf to keep your favorite books, but you keep buying new books every week. When the shelf is full, you have to decide which book to remove to make space for the new one.
Manually choosing which book to remove is slow and confusing. You might forget which books you read recently or which ones you like the most. This can lead to removing important books by mistake or wasting time deciding.
Eviction policies like LRU (Least Recently Used), LFU (Least Frequently Used), and random automatically decide which items to remove when space runs out. This keeps your shelf organized without you having to think about it.
if shelf_full:
ask_user_which_book_to_remove()if shelf_full:
remove_item_using_LRU_policy()It enables automatic, smart management of limited space so your system stays fast and efficient without manual effort.
In Redis, when memory is full, eviction policies decide which cached data to remove so your app keeps running smoothly without crashing or slowing down.
Manual removal of items is slow and error-prone.
Eviction policies automate smart removal decisions.
This keeps systems efficient and reliable under memory limits.
Practice
Solution
Step 1: Understand eviction policies in Redis
Redis uses eviction policies to free memory when it is full by removing keys based on certain rules.Step 2: Identify the policy that removes least recently used keys
LRU stands for Least Recently Used, which means it removes keys that have not been accessed for the longest time.Final Answer:
LRU (Least Recently Used) -> Option AQuick Check:
Eviction policy removing least recently used keys = LRU [OK]
- Confusing LRU with LFU
- Thinking random eviction is based on usage
- Assuming no eviction means keys are removed
Solution
Step 1: Recall Redis maxmemory-policy options
Redis uses the 'maxmemory-policy' directive to set eviction behavior, with options like 'allkeys-lfu' for LFU eviction.Step 2: Identify the correct syntax for LFU eviction
The correct directive for LFU eviction on all keys is 'maxmemory-policy allkeys-lfu'.Final Answer:
maxmemory-policy allkeys-lfu -> Option AQuick Check:
LFU eviction config = maxmemory-policy allkeys-lfu [OK]
- Using 'random-eviction' instead of LFU
- Confusing 'volatile-lru' with LFU
- Missing 'maxmemory-policy' keyword
maxmemory-policy allkeys-lru and memory is full, what happens when a new key is added?Solution
Step 1: Understand the 'allkeys-lru' eviction policy
This policy evicts the least recently used key among all keys when memory is full.Step 2: Predict behavior when adding a new key
When memory is full, Redis removes the least recently used key to free space for the new key.Final Answer:
The least recently used key is evicted to make space -> Option CQuick Check:
allkeys-lru evicts least recently used key [OK]
- Confusing LRU with LFU eviction
- Thinking random eviction applies here
- Assuming new keys are rejected automatically
maxmemory-policy volatile-lfu but notice keys without expiration are never evicted. What is the likely cause?Solution
Step 1: Understand volatile-lfu eviction policy
Volatile policies only evict keys that have an expiration set (TTL).Step 2: Analyze why keys without expiration remain
Since keys without expiration are not eligible, they are never evicted under volatile-lfu.Final Answer:
Only keys with expiration are considered for eviction in volatile-lfu -> Option BQuick Check:
Volatile policies evict only expiring keys [OK]
- Assuming LFU ignores expiration
- Thinking random eviction applies here
- Believing keys without expiration are evicted first
Solution
Step 1: Understand eviction policy prefixes
'allkeys-' applies to all keys; 'volatile-' applies only to keys with expiration.Step 2: Identify random eviction among volatile keys
'volatile-random' evicts keys randomly but only among keys with expiration set.Final Answer:
volatile-random -> Option DQuick Check:
Random eviction on expiring keys = volatile-random [OK]
- Choosing allkeys-random which evicts all keys
- Confusing LFU or LRU with random eviction
- Selecting volatile-lru which is not random
Ready to prove it?
Pass the test and this pattern moves unknown
