Bird
Raised Fist0
Redisquery~10 mins

Eviction policies (LRU, LFU, random) in Redis - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the eviction policy to Least Recently Used (LRU) in Redis configuration.

Redis
CONFIG SET maxmemory-policy [1]
Drag options to blanks, or click blank then click option'
Anoeviction
Bvolatile-lfu
Callkeys-lru
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'volatile-lfu' which is for Least Frequently Used but only on volatile keys.
Using 'noeviction' which disables eviction.
Selecting 'random' which evicts keys randomly.
2fill in blank
medium

Complete the code to set the eviction policy to Least Frequently Used (LFU) for volatile keys only.

Redis
CONFIG SET maxmemory-policy [1]
Drag options to blanks, or click blank then click option'
Avolatile-lru
Ballkeys-random
Callkeys-lfu
Dvolatile-lfu
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'allkeys-lfu' which applies LFU to all keys, not just volatile ones.
Selecting 'volatile-lru' which is LRU, not LFU.
Using 'allkeys-random' which evicts keys randomly.
3fill in blank
hard

Fix the error in the command to set a random eviction policy.

Redis
CONFIG SET maxmemory-policy [1]
Drag options to blanks, or click blank then click option'
Arandom
Ballkeys-random
Cvolatile-random
Drandom-eviction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'random' alone which is not a valid policy name.
Choosing 'random-eviction' which is not recognized.
Selecting 'volatile-random' which only evicts volatile keys.
4fill in blank
hard

Fill both blanks to set the max memory to 100mb and eviction policy to allkeys-lfu.

Redis
CONFIG SET maxmemory [1]
CONFIG SET maxmemory-policy [2]
Drag options to blanks, or click blank then click option'
A100mb
Ballkeys-lfu
Cvolatile-lru
D50mb
Attempts:
3 left
💡 Hint
Common Mistakes
Using '50mb' instead of '100mb' for maxmemory.
Choosing 'volatile-lru' which is a different eviction policy.
5fill in blank
hard

Fill all three blanks to configure Redis to use 200mb max memory, volatile-lru eviction policy, and set a key expiration of 60 seconds.

Redis
CONFIG SET maxmemory [1]
CONFIG SET maxmemory-policy [2]
EXPIRE mykey [3]
Drag options to blanks, or click blank then click option'
Avolatile-lru
B60
C200mb
Dallkeys-random
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up eviction policies like choosing 'allkeys-random' instead of 'volatile-lru'.
Setting expiration time in wrong units.

Practice

(1/5)
1. Which Redis eviction policy removes the least recently used keys first when memory is full?
easy
A. LRU (Least Recently Used)
B. LFU (Least Frequently Used)
C. Random
D. No eviction

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    LRU (Least Recently Used) -> Option A
  4. Quick Check:

    Eviction policy removing least recently used keys = LRU [OK]
Hint: LRU = removes oldest unused keys first [OK]
Common Mistakes:
  • Confusing LRU with LFU
  • Thinking random eviction is based on usage
  • Assuming no eviction means keys are removed
2. Which of the following is the correct Redis configuration directive to set the eviction policy to LFU?
easy
A. maxmemory-policy allkeys-lfu
B. maxmemory-policy random-eviction
C. maxmemory-policy volatile-lru
D. maxmemory-policy noeviction

Solution

  1. 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.
  2. Step 2: Identify the correct syntax for LFU eviction

    The correct directive for LFU eviction on all keys is 'maxmemory-policy allkeys-lfu'.
  3. Final Answer:

    maxmemory-policy allkeys-lfu -> Option A
  4. Quick Check:

    LFU eviction config = maxmemory-policy allkeys-lfu [OK]
Hint: LFU config uses 'allkeys-lfu' in maxmemory-policy [OK]
Common Mistakes:
  • Using 'random-eviction' instead of LFU
  • Confusing 'volatile-lru' with LFU
  • Missing 'maxmemory-policy' keyword
3. Given Redis is configured with the maxmemory-policy allkeys-lru and memory is full, what happens when a new key is added?
medium
A. A random key is evicted to make space
B. The least frequently used key is evicted to make space
C. The least recently used key is evicted to make space
D. The new key is rejected and not added

Solution

  1. Step 1: Understand the 'allkeys-lru' eviction policy

    This policy evicts the least recently used key among all keys when memory is full.
  2. 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.
  3. Final Answer:

    The least recently used key is evicted to make space -> Option C
  4. Quick Check:

    allkeys-lru evicts least recently used key [OK]
Hint: allkeys-lru evicts oldest used key on full memory [OK]
Common Mistakes:
  • Confusing LRU with LFU eviction
  • Thinking random eviction applies here
  • Assuming new keys are rejected automatically
4. You configured Redis with maxmemory-policy volatile-lfu but notice keys without expiration are never evicted. What is the likely cause?
medium
A. LFU policy does not work with volatile keys
B. Only keys with expiration are considered for eviction in volatile-lfu
C. Redis randomly evicts keys regardless of policy
D. Keys without expiration are evicted first

Solution

  1. Step 1: Understand volatile-lfu eviction policy

    Volatile policies only evict keys that have an expiration set (TTL).
  2. Step 2: Analyze why keys without expiration remain

    Since keys without expiration are not eligible, they are never evicted under volatile-lfu.
  3. Final Answer:

    Only keys with expiration are considered for eviction in volatile-lfu -> Option B
  4. Quick Check:

    Volatile policies evict only expiring keys [OK]
Hint: Volatile policies evict only keys with expiration [OK]
Common Mistakes:
  • Assuming LFU ignores expiration
  • Thinking random eviction applies here
  • Believing keys without expiration are evicted first
5. You want Redis to evict keys randomly but only among keys with expiration set. Which eviction policy should you configure?
hard
A. volatile-lru
B. allkeys-random
C. allkeys-lfu
D. volatile-random

Solution

  1. Step 1: Understand eviction policy prefixes

    'allkeys-' applies to all keys; 'volatile-' applies only to keys with expiration.
  2. Step 2: Identify random eviction among volatile keys

    'volatile-random' evicts keys randomly but only among keys with expiration set.
  3. Final Answer:

    volatile-random -> Option D
  4. Quick Check:

    Random eviction on expiring keys = volatile-random [OK]
Hint: Volatile-random evicts random expiring keys only [OK]
Common Mistakes:
  • Choosing allkeys-random which evicts all keys
  • Confusing LFU or LRU with random eviction
  • Selecting volatile-lru which is not random
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