Introducing Dragonfly Cloud! Learn More

Question: Why is there no eviction policy in Redis and how can it be configured?

Answer

In Redis, the eviction policy refers to the method chosen to remove data when memory is full. If there's no eviction policy in place, Redis won't remove keys to free up space when the max memory limit is reached. Instead, it may return errors on commands that use more memory.

You can configure an eviction policy through the maxmemory-policy configuration directive, which defaults to noeviction.

Here are the different types of eviction policies available in Redis:

  • noeviction: Returns an error if the memory limit has been reached when trying to insert more data.
  • allkeys-lru: Evicts the least recently used keys out of all keys.
  • volatile-lru: Evicts the least recently used keys out of all keys with an expire set.
  • allkeys-random: Evicts random keys out of all keys.
  • volatile-random: Evicts random keys out of all keys with an expire set.
  • volatile-ttl: Evicts keys with an expire set, sorted by shortest to longest TTL (time to live).
  • volatile-lfu: Evicts the least frequently used keys out of all keys with an expire set.
  • allkeys-lfu: Evicts the least frequently used keys out of all keys.

To set an eviction policy, you can modify your redis.conf file or use the CONFIG SET command. Here's how you can do it using the CONFIG SET command:

redis-cli config set maxmemory-policy allkeys-lru

This command sets the eviction policy to allkeys-lru, meaning that Redis will evict the least recently used keys when the max memory limit is hit. Make sure to choose an eviction policy that suits your application's needs.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.