0
0
Redisquery~10 mins

Eviction policies overview in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Eviction policies overview
Memory Limit Reached?
Yes
Select Eviction Policy
Remove Keys Based on Policy
Free Memory
Continue Accepting Writes
When Redis memory is full, it checks the eviction policy to decide which keys to remove to free space or to reject writes.
Execution Sample
Redis
CONFIG SET maxmemory 100mb
CONFIG SET maxmemory-policy allkeys-lru
SET key1 value1
SET key2 value2
... (many keys)
SET keyN valueN
This example sets a memory limit and eviction policy, then adds keys until Redis evicts some keys based on the policy.
Execution Table
StepMemory UsageActionEviction PolicyKeys RemovedResult
110mbAdd key1allkeys-lru0Success
220mbAdd key2allkeys-lru0Success
395mbAdd keyN-1allkeys-lru0Success
4100mbAdd keyNallkeys-lru0Memory limit reached, triggers eviction
590mbEvict keysallkeys-lru1 (least recently used)Space freed
6100mbAdd keyNallkeys-lru0Success
7100mbAdd keyN+1noeviction0Error - memory limit reached, no eviction
💡 Execution stops when memory limit is reached and no eviction policy allows freeing memory.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
Memory Usage0mb10mb20mb95mb100mb90mb100mb100mb
Keys Stored012N-1NN-1NN
Eviction TriggeredNoNoNoNoYesYesNoNo
Write SuccessN/AYesYesYesYesYesYesNo
Key Moments - 3 Insights
Why does Redis remove keys only after memory limit is reached?
Redis allows writes until memory usage hits the limit (see Step 4 in execution_table). Only then it triggers eviction to free space.
What happens if the eviction policy is set to 'noeviction'?
Redis refuses new writes when memory is full instead of removing keys (see Step 7 in execution_table).
How does the 'allkeys-lru' policy decide which key to remove?
It removes the least recently used key regardless of expiration (Step 5 shows one key removed based on usage).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Redis first trigger eviction?
AStep 4
BStep 3
CStep 5
DStep 7
💡 Hint
Check the 'Action' and 'Result' columns in execution_table rows for when eviction starts.
According to variable_tracker, what is the memory usage after eviction at Step 5?
A100mb
B90mb
C95mb
D92mb
💡 Hint
Look at the 'Memory Usage' row under 'After Step 5' in variable_tracker.
If the eviction policy was 'noeviction', what would happen at Step 7?
ARedis evicts keys to free memory
BRedis accepts the write successfully
CRedis returns an error due to memory limit
DRedis increases memory limit automatically
💡 Hint
Refer to Step 7 in execution_table under 'Result' for 'noeviction' policy.
Concept Snapshot
Redis Eviction Policies Overview:
- When memory limit is reached, Redis uses eviction policies to free space.
- Policies include allkeys-lru, volatile-lru, noeviction, etc.
- allkeys-lru removes least recently used keys regardless of expiration.
- noeviction rejects writes when memory is full.
- Proper policy choice helps maintain performance under memory pressure.
Full Transcript
This visual execution shows how Redis handles memory limits using eviction policies. When Redis reaches its configured max memory, it checks the eviction policy. If the policy allows, Redis removes keys based on that policy, such as least recently used keys in allkeys-lru. If the policy is noeviction, Redis refuses new writes once memory is full. The execution table traces memory usage and actions step-by-step, while the variable tracker shows how memory and keys change. Key moments clarify common confusions about when eviction triggers and how policies differ. The quiz tests understanding of these steps and outcomes.