Understanding Redis Eviction Policies
Redis is an in-memory data store, which means it stores everything in RAM. Fast? Absolutely. But memory isn’t unlimited. So, what happens when your Redis instance runs out of memory?
That’s where eviction policies come into play.
An eviction policy in Redis decides what data should be removed (or evicted) when memory is full and new data needs to be added. Think of it as a strategy Redis uses to avoid crashing when memory is exhausted.
Redis Eviction Policies Explained
Redis provides multiple eviction policies. Each one is designed for a specific use case. Here are the most common ones:
1. noeviction (default)
2. allkeys-lru (Least Recently Used)
3. volatile-lru
4. allkeys-random
5. volatile-random
6. volatile-ttl
How to Configure the Eviction Policy
To configure Redis to use a specific eviction policy, you can:
Option 1: Edit redis.conf
Find this line:
maxmemory-policy noeviction
Change it to your desired policy, for example:
maxmemory-policy allkeys-lru
Also, set the memory limit:
maxmemory 256mb
Option 2: Use Redis CLI
You can update it on the fly (though it’s not persistent after restart):
CONFIG SET maxmemory 256mb
CONFIG SET maxmemory-policy allkeys-lru
Want to check the current policy?
CONFIG GET maxmemory-policy
What to Do When Redis Eats All Your RAM in Production?
So the fire alarm’s going off, your Redis instance just filled up the server’s memory. What now?
🔍 Step 1: Check Memory Usage
Run:
INFO memory
This gives you an overview of memory stats.
🗑 Step 2: Evict Unused or Stale Keys
You can manually delete big keys or patterns:
DEL big_key
Or:
SCAN 0 MATCH user:* COUNT 1000
Use UNLINK instead of DEL for faster deletion without blocking:
UNLINK huge_key
⚙️ Step 3: Enable an Eviction Policy (if you haven’t already)
If you’re on noeviction, change to something smarter like allkeys-lru.
🧹 Step 4: Use TTLs Everywhere
Make it a habit to set TTLs when storing cache-like data:
SET session:123 abc EX 3600
This way, Redis can clean up after itself when space runs low.
🚀 Step 5: Scale Up (if all else fails)
If none of the above helps, you might need:
Pro Tips for Managing Redis
1. Use MEMORY USAGE to Find Heavy Keys
Redis lets you check how much memory a specific key is using:
MEMORY USAGE some_key
Want to hunt down your top memory hogs? Here’s a basic idea using CLI + scripting:
SCAN 0 MATCH * COUNT 1000 | while read key; do
echo "$(redis-cli MEMORY USAGE $key) $key"
done | sort -nr | head -n 10
2. Organize Keys by Prefixes for Easier Cleanup
Use namespaces in your key names:
user:123:profile
user:123:posts
session:abc
This makes it much easier to bulk delete or set TTLs:
SCAN 0 MATCH session:* COUNT 1000
3. Monitor With redis-cli or RedisInsight
Use tools to monitor memory usage over time.
INFO stats
INFO memory
4. Prefer volatile-* Policies in Multi-Purpose Redis Instances
If you’re using Redis both for:
Then use a volatile policy (like volatile-lru or volatile-ttl).
Why? Because these will only evict keys that have expiration — meaning your long-lived data won’t vanish silently.