Understanding Redis Eviction Policies
Redis Eviction Policies

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)

  • What it does: Refuses to insert new data when memory is full.
  • Use case: If you want to make sure nothing is removed automatically.
  • Drawback: Your app may crash or throw errors when memory is exhausted.

2. allkeys-lru (Least Recently Used)

  • What it does: Removes the least recently used keys, regardless of whether they have an expiration time or not.
  • Use case: Best for general-purpose caching when you don’t set expirations.
  • Drawback: Might evict important long-lived data if it’s not accessed frequently.

3. volatile-lru

  • What it does: Removes the least recently used keys with expiration set (TTL)
  • Use case: Great when you want Redis to evict only temporary cache data, not important persistent data.
  • Drawback: If no keys have TTL, eviction can fail.

4. allkeys-random

  • What it does: Evicts a random key, no matter what.
  • Use case: Quick and dirty. Not very smart, but better than crashing.
  • Drawback: It’s like spinning a wheel. You don’t know what’s getting deleted.

5. volatile-random

  • What it does: Evicts a random key with an expiration.
  • Use case: Same as above but only affects temporary data.
  • Drawback: Still unpredictable.

6. volatile-ttl

  • What it does: Evicts the keys with the nearest expiration time (lowest TTL)
  • Use case: When you want keys that are about to expire to go first.
  • Drawback: Only works with keys that have 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:

  • More memory (upgrade your server)
  • Redis clustering to split data across nodes
  • Offloading cold data to another storage like a database or file system

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:

  • Caching (short-lived data)
  • Persistent stuff (like queues, sessions, etc.

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.

To view or add a comment, sign in

Others also viewed

Explore topics