Eviction policies help Redis decide which data to remove when memory is full. This keeps Redis fast and avoids crashes.
Eviction policies (LRU, LFU, random) in Redis
maxmemory-policy <policy_name>
Set this in your Redis configuration file or at runtime with the CONFIG SET command.
Common policies: allkeys-lru, allkeys-lfu, volatile-lru, volatile-lfu, volatile-random, allkeys-random, noeviction.
CONFIG SET maxmemory-policy allkeys-lru
CONFIG SET maxmemory-policy volatile-lfu
CONFIG SET maxmemory-policy allkeys-random
This example sets Redis to use 100 MB max memory and the allkeys-lru eviction policy. When memory is full, Redis removes the least recently used keys from all keys.
CONFIG SET maxmemory 100mb CONFIG SET maxmemory-policy allkeys-lru SET key1 value1 SET key2 value2 # Simulate memory full by adding many keys # Redis will evict least recently used keys automatically GET key1 GET key2
LRU means Least Recently Used: Redis removes keys not used recently.
LFU means Least Frequently Used: Redis removes keys used less often.
Random eviction removes any key randomly, which is simple but less efficient.
Eviction policies control how Redis frees memory when full.
LRU and LFU keep the most useful data by usage patterns.
Random eviction removes keys without pattern, simpler but less smart.
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
