Redis maxmemory-policy Explained: Choosing the Right Eviction Policy
Introduction
The maxmemory-policy directive determines what Redis does when the configured maxmemory limit has been reached.
Without an appropriate eviction policy, Redis may reject new write operations or remove existing keys depending on the selected policy.
Choosing the right policy is essential for balancing memory usage, application performance, and data persistence.
This guide explains every available eviction policy, when to use each one, and best practices for production environments.
Test Environment
Default Value
maxmemory-policy noeviction
With the default setting, Redis does not automatically remove keys when memory is exhausted.
Instead, write commands that require additional memory will return an error.
Syntax
maxmemory-policy
Example:
maxmemory-policy allkeys-lru
Available Policies
Redis supports several eviction policies.
Understanding Each Policy
noeviction
maxmemory-policy noeviction
When memory is full:
Read operations continue normally.
New write operations fail.
Best for:
Persistent databases.
Critical business data.
Applications that cannot tolerate automatic data removal.
allkeys-lru
maxmemory-policy allkeys-lru
Redis removes the least recently used key regardless of whether it has an expiration time.
Best for:
General caching.
Web applications.
Session caches.
This is one of the most commonly used policies.
volatile-lru
maxmemory-policy volatile-lru
Only keys with an expiration time are eligible for eviction.
Persistent keys remain untouched.
Best for:
Mixed workloads.
Applications storing both permanent and temporary data.
allkeys-lfu
maxmemory-policy allkeys-lfu
Redis removes the least frequently used keys.
Unlike LRU, LFU considers how often a key has been accessed over time.
Best for:
Long-lived caches.
Frequently accessed datasets.
API caching.
volatile-lfu
Only expiring keys participate in LFU eviction.
Useful when permanent data should never be removed.
allkeys-random
Redis removes a random key.
Rarely recommended except for testing or special workloads.
volatile-random
Randomly removes an expiring key.
Less predictable than LRU or LFU.
volatile-ttl
Redis removes the key that will expire soonest.
Useful when expiration times accurately represent data importance.
Which Policy Should You Choose?
Configure the Policy
Edit the Redis configuration file.
sudo nano /etc/redis.conf
Example:
maxmemory 4gb
maxmemory-policy allkeys-lru
Restart Redis.
sudo systemctl restart redis
Verify the Configuration
redis-cli CONFIG GET maxmemory-policy
Example output:
1) "maxmemory-policy"
2) "allkeys-lru"
Change the Policy at Runtime
redis-cli CONFIG SET maxmemory-policy allkeys-lfu
Remember to update the configuration file if you want the change to persist after a restart.
Common Mistakes
Using noeviction for Cache Servers
Cache workloads usually benefit from automatic eviction. Using noeviction may cause write operations to fail unexpectedly.
Forgetting maxmemory
The eviction policy has no effect unless maxmemory is configured.
Choosing Random Policies
Random eviction policies rarely provide predictable performance and are generally unsuitable for production.
Best Practices
Always configure maxmemory together with maxmemory-policy.
Use allkeys-lru for most caching workloads.
Consider allkeys-lfu for applications with frequently reused data.
Monitor memory usage and eviction statistics using INFO stats.
Test different policies under realistic workloads before deploying to production.
Related Articles
Conclusion
The maxmemory-policy directive controls how Redis responds when memory limits are reached. Selecting the right eviction policy ensures stable performance and efficient memory usage.
For most caching environments, allkeys-lru is an excellent default choice due to its balance of simplicity and effectiveness, while allkeys-lfu may provide better performance for workloads where frequently accessed data should remain in memory.
Explore More
› Redis loglevel Explained: Choosing the Right Logging Level
› Redis maxmemory Explained: How to Limit Memory Usage and Prevent OOM Errors
› Redis appendonly Explained: Understanding AOF Persistence
› Redis save Explained: Configuring RDB Snapshots for Data Persistence
› Why Do People in Southeast Asia Love Iced Drinks? The Climate, Culture, and Science Explained
› Northern vs Southern Chinese Business Culture: Key Differences Explained