Eviction policies overview in Redis - Time & Space Complexity
When Redis runs out of memory, it removes some data to make space. This process is called eviction.
We want to understand how the time it takes to evict keys changes as the data size grows.
Analyze the time complexity of eviction when Redis uses the allkeys-lru policy.
// Redis eviction with allkeys-lru
while (memory_used > max_memory) {
key = find_least_recently_used_key();
delete_key(key);
update_memory_usage();
}
This code removes the least recently used keys until enough memory is freed.
Look for repeated actions that affect performance.
- Primary operation: Finding and deleting keys one by one.
- How many times: Repeated until memory is below the limit, depends on how much memory needs freeing.
As the number of keys grows, finding the least recently used key takes more effort.
| Input Size (n keys) | Approx. Operations |
|---|---|
| 10 | Few steps to find and delete keys |
| 100 | More steps, but still manageable |
| 1000 | More searching needed, time grows |
Pattern observation: The time to find keys grows roughly with the number of keys.
Time Complexity: O(n)
This means the time to evict keys grows linearly with the number of keys stored.
[X] Wrong: "Eviction always happens instantly no matter how many keys there are."
[OK] Correct: Finding which keys to remove takes more time as the database grows, so eviction can slow down with more data.
Understanding eviction time helps you explain how Redis manages memory efficiently and what happens under heavy load.
"What if Redis used a random eviction policy instead of LRU? How would the time complexity change?"
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
