Databases 6 min read

Master Redis Memory Eviction: All 8 Policies and Configuration Tips

This article explains Redis's eight memory eviction policies, their ideal use cases, and provides step‑by‑step commands and configuration examples for querying and setting both the eviction strategy and maximum memory limits.

Ma Wei Says
Ma Wei Says
Ma Wei Says
Master Redis Memory Eviction: All 8 Policies and Configuration Tips

Overview of Memory Eviction Strategies

When Redis reaches its configured maximum memory, it selects keys to delete based on the chosen eviction policy. Redis offers eight distinct policies, each suited to different data durability and performance requirements.

Eight Eviction Policies

noeviction – The default policy. New write operations are rejected with an error when memory is insufficient, preserving existing data and signaling administrators to increase memory or adjust usage.

allkeys-lru – Implements the Least Recently Used (LRU) algorithm across all keys, evicting the key that has not been accessed for the longest time when memory is full.

volatile-lru – Applies LRU only to keys that have an expiration set, removing the least recently used expiring keys first.

allkeys-random – Randomly selects any key for eviction, regardless of expiration or access frequency, providing a simple and fast way to relieve memory pressure.

volatile-random – Randomly evicts a key among those that have an expiration, ensuring that permanent keys are never removed.

volatile-ttl – Targets expiring keys with the smallest Time‑To‑Live (TTL), removing the keys that will expire soonest.

allkeys-lfu – Uses the Least Frequently Used (LFU) algorithm across all keys, evicting the key with the lowest access frequency when memory is exhausted.

volatile-lfu – Applies LFU only to keys with an expiration, evicting the least frequently accessed expiring keys while leaving non‑expiring keys untouched.

Related Operations and Settings

1) Retrieve the maximum memory Redis can use config get maxmemory Result shown in the image below:

Redis maxmemory query result
Redis maxmemory query result

2) Retrieve the current memory eviction policy config get maxmemory-policy Result shown in the image below:

Redis maxmemory-policy query result
Redis maxmemory-policy query result

3) Set the eviction policy in redis.conf

Example configuration line (see image):

redis.conf eviction policy setting
redis.conf eviction policy setting

4) Set the memory limit in redis.conf

Example configuration line (see image):

redis.conf maxmemory setting
redis.conf maxmemory setting

5) Configure policy and memory limit via commands (temporary effect)

# Set eviction policy
config set maxmemory-policy allkeys-lru

# Set memory limit
config set maxmemory 100mb
CacheRedisLRULFUDatabase Configurationmemory eviction
Ma Wei Says
Written by

Ma Wei Says

Follow me! Discussing software architecture and development, AIGC and AI Agents... Sometimes sharing insights on IT professionals' life experiences.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.