Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of eviction policies in Redis?
easy
A. To encrypt Redis data
B. To configure the network settings of Redis
C. To backup data automatically
D. To decide how Redis frees memory when it is full

Solution

  1. Step 1: Understand Redis memory management

    Redis uses eviction policies to manage memory when it reaches its limit.
  2. Step 2: Identify the role of eviction policies

    Eviction policies decide which data to remove to free memory and keep Redis running smoothly.
  3. Final Answer:

    To decide how Redis frees memory when it is full -> Option D
  4. Quick Check:

    Eviction policies control memory freeing = A [OK]
Hint: Eviction policies manage memory freeing in Redis [OK]
Common Mistakes:
  • Confusing eviction policies with backup or encryption
  • Thinking eviction policies configure network
  • Assuming eviction policies handle data encryption
2. Which command correctly sets the eviction policy to 'allkeys-lru' in Redis?
easy
A. CONFIG SET maxmemory-policy allkeys-lru
B. SET maxmemory-policy allkeys-lru
C. CONFIG GET maxmemory-policy allkeys-lru
D. EVICT POLICY allkeys-lru

Solution

  1. Step 1: Recall the correct command format

    Redis uses CONFIG SET to change configuration settings.
  2. Step 2: Match the correct syntax for eviction policy

    The correct syntax is CONFIG SET maxmemory-policy policy_name, so allkeys-lru fits here.
  3. Final Answer:

    CONFIG SET maxmemory-policy allkeys-lru -> Option A
  4. Quick Check:

    Use CONFIG SET for eviction policy = A [OK]
Hint: Use CONFIG SET maxmemory-policy to set eviction [OK]
Common Mistakes:
  • Using SET instead of CONFIG SET
  • Using CONFIG GET to set a value
  • Using a non-existent EVICT POLICY command
3. Given Redis is set with eviction policy volatile-lru and maxmemory is reached, which keys are eligible for eviction?
medium
A. All keys regardless of expiration
B. Only keys with an expiration set
C. Only keys without expiration
D. No keys are evicted automatically

Solution

  1. Step 1: Understand 'volatile-lru' policy meaning

    This policy evicts keys with expiration set using least recently used (LRU) order.
  2. 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.
  3. Final Answer:

    Only keys with an expiration set -> Option B
  4. Quick Check:

    volatile-lru evicts expiring keys = B [OK]
Hint: volatile-lru evicts only keys with expiration [OK]
Common Mistakes:
  • Thinking all keys are evicted
  • Confusing volatile-lru with allkeys-lru
  • Assuming no keys are evicted automatically
4. You set Redis eviction policy to noeviction but Redis still evicts keys when memory is full. What is the likely cause?
medium
A. The policy was not applied correctly or Redis was restarted without saving config
B. Redis always evicts keys regardless of policy
C. noeviction policy evicts keys randomly
D. Redis ignores eviction policies when maxmemory is set

Solution

  1. Step 1: Understand 'noeviction' policy behavior

    This policy prevents Redis from evicting keys automatically when memory is full.
  2. 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.
  3. Final Answer:

    The policy was not applied correctly or Redis was restarted without saving config -> Option A
  4. Quick Check:

    noeviction means no eviction unless misconfigured = C [OK]
Hint: Check config save and apply for noeviction policy [OK]
Common Mistakes:
  • Believing Redis always evicts keys
  • Thinking noeviction evicts randomly
  • Assuming eviction policies are ignored when maxmemory is set
5. You want Redis to evict the least recently used keys regardless of expiration when memory is full. Which eviction policy should you choose?
hard
A. volatile-lru
B. noeviction
C. allkeys-lru
D. volatile-random

Solution

  1. Step 1: Understand eviction policy options

    volatile-lru evicts only keys with expiration; allkeys-lru evicts any key using LRU.
  2. Step 2: Match requirement to policy

    Since eviction should ignore expiration and evict least recently used keys, allkeys-lru fits best.
  3. Final Answer:

    allkeys-lru -> Option C
  4. Quick Check:

    Evict any key LRU = allkeys-lru = D [OK]
Hint: allkeys-lru evicts any key by least recent use [OK]
Common Mistakes:
  • Choosing volatile-lru which only evicts expiring keys
  • Selecting volatile-random which evicts randomly
  • Picking noeviction which disables eviction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard