0
0
Redisquery~30 mins

Eviction policies (LRU, LFU, random) in Redis - Mini Project: Build & Apply

Choose your learning style9 modes available
Configuring and Testing Redis Eviction Policies
📖 Scenario: You are managing a Redis cache server that stores user session data. To keep the cache efficient and prevent it from growing too large, you need to set an eviction policy. This policy decides which data Redis removes when it reaches its memory limit.Redis supports several eviction policies like Least Recently Used (LRU), Least Frequently Used (LFU), and random eviction. You will configure these policies using Redis CLI and test how Redis behaves when the cache is full.
🎯 Goal: Set up a Redis cache with a memory limit and configure different eviction policies (LRU, LFU, random) using CONFIG SET. Then, add keys to the cache to observe which keys get removed when the memory limit is reached.
📋 What You'll Learn
Set maxmemory limit to 102400 bytes (100 kilobytes) using CONFIG SET
Set the eviction policy to 'allkeys-lru' using CONFIG SET
Add multiple keys with values to the Redis cache using SET
Change the eviction policy to 'allkeys-lfu' and then to 'allkeys-random' using CONFIG SET and observe behavior
💡 Why This Matters
🌍 Real World
Caches like Redis often run on limited memory. Setting eviction policies helps keep the cache efficient by removing less important data automatically.
💼 Career
Understanding and configuring eviction policies is important for roles like backend developers, DevOps engineers, and system administrators who manage caching layers.
Progress0 / 4 steps
1
Set maxmemory limit
Using the Redis CLI, set the maxmemory to 102400 bytes (equivalent to 100kb) using the CONFIG SET command.
Redis
Need a hint?

The CONFIG SET maxmemory 102400 sets the memory limit Redis can use (100KB in bytes).

2
Set eviction policy to allkeys-lru
Using the Redis CLI, set the maxmemory-policy to allkeys-lru using CONFIG SET.
Redis
Need a hint?

Use CONFIG SET maxmemory-policy allkeys-lru for Least Recently Used eviction on all keys.

3
Add keys to Redis cache
Using the Redis CLI, add keys named key1 to key5 with values value1 to value5 respectively using the SET command. To test eviction later, you can add more keys.
Redis
Need a hint?

Use the SET key value command to add keys and values to Redis. Add more keys later to trigger eviction.

4
Change eviction policy to allkeys-lfu and then allkeys-random
Using CONFIG SET, change the eviction policy first to allkeys-lfu, add some more keys (e.g., key6 value6, etc.) to trigger eviction, observe with GET or KEYS *. Then change to allkeys-random and repeat.
Redis
Need a hint?

Use CONFIG SET maxmemory-policy allkeys-lfu then allkeys-random. Add extra SET commands to fill memory and observe evictions.