Eviction policies help Redis decide which data to remove when it runs out of memory. This keeps Redis fast and prevents crashes.
Eviction policies overview in Redis
CONFIG SET maxmemory-policy <policy_name>
You set the eviction policy using the CONFIG SET command.
The <policy_name> is the name of the eviction policy you want to use.
CONFIG SET maxmemory-policy noeviction
CONFIG SET maxmemory-policy allkeys-lru
CONFIG SET maxmemory-policy volatile-lru
CONFIG SET maxmemory-policy allkeys-random
This sets Redis to use 100 megabytes of memory and to remove the least recently used keys from all keys when memory is full.
CONFIG SET maxmemory 100mb
CONFIG SET maxmemory-policy allkeys-lru
Eviction policies only work if you set a memory limit with maxmemory.
Choosing the right policy depends on your use case, like caching or persistent storage.
Common policies include noeviction, allkeys-lru, volatile-lru, allkeys-random, and volatile-random.
Eviction policies control how Redis frees memory when it is full.
You set eviction policies with CONFIG SET maxmemory-policy.
Pick a policy that fits your app's needs to keep Redis fast and stable.
Practice
Solution
Step 1: Understand Redis memory management
Redis uses eviction policies to manage memory when it reaches its limit.Step 2: Identify the role of eviction policies
Eviction policies decide which data to remove to free memory and keep Redis running smoothly.Final Answer:
To decide how Redis frees memory when it is full -> Option DQuick Check:
Eviction policies control memory freeing = A [OK]
- Confusing eviction policies with backup or encryption
- Thinking eviction policies configure network
- Assuming eviction policies handle data encryption
Solution
Step 1: Recall the correct command format
Redis usesCONFIG SETto change configuration settings.Step 2: Match the correct syntax for eviction policy
The correct syntax isCONFIG SET maxmemory-policy policy_name, soallkeys-lrufits here.Final Answer:
CONFIG SET maxmemory-policy allkeys-lru -> Option AQuick Check:
Use CONFIG SET for eviction policy = A [OK]
- Using SET instead of CONFIG SET
- Using CONFIG GET to set a value
- Using a non-existent EVICT POLICY command
volatile-lru and maxmemory is reached, which keys are eligible for eviction?Solution
Step 1: Understand 'volatile-lru' policy meaning
This policy evicts keys with expiration set using least recently used (LRU) order.Step 2: Identify which keys can be evicted
Only keys that have an expiration time are considered for eviction under 'volatile-lru'. Keys without expiration stay safe.Final Answer:
Only keys with an expiration set -> Option BQuick Check:
volatile-lru evicts expiring keys = B [OK]
- Thinking all keys are evicted
- Confusing volatile-lru with allkeys-lru
- Assuming no keys are evicted automatically
noeviction but Redis still evicts keys when memory is full. What is the likely cause?Solution
Step 1: Understand 'noeviction' policy behavior
This policy prevents Redis from evicting keys automatically when memory is full.Step 2: Identify why eviction still happens
If eviction occurs, likely the policy was not saved or applied properly, or Redis restarted without config persistence.Final Answer:
The policy was not applied correctly or Redis was restarted without saving config -> Option AQuick Check:
noeviction means no eviction unless misconfigured = C [OK]
- Believing Redis always evicts keys
- Thinking noeviction evicts randomly
- Assuming eviction policies are ignored when maxmemory is set
Solution
Step 1: Understand eviction policy options
volatile-lru evicts only keys with expiration; allkeys-lru evicts any key using LRU.Step 2: Match requirement to policy
Since eviction should ignore expiration and evict least recently used keys, allkeys-lru fits best.Final Answer:
allkeys-lru -> Option CQuick Check:
Evict any key LRU = allkeys-lru = D [OK]
- Choosing volatile-lru which only evicts expiring keys
- Selecting volatile-random which evicts randomly
- Picking noeviction which disables eviction
Ready to prove it?
Pass the test and this pattern moves unknown
