Redis is an in-memory data structure store widely used as a cache, database, or message broker.
When its memory usage approaches the configured limit (maxmemory), Redis employs eviction policies to free up space
by removing less critical data. This article explains Redis eviction policies, how they work, and how to choose the
right one for your use case.
What Are Redis Eviction Policies?
Redis eviction policies determine which keys to remove when memory is full and new data needs to be stored. These
policies are crucial for maintaining performance in memory-constrained environments. Redis offers six primary eviction
policies, configurable via the maxmemory-policy setting.
Available Eviction Policies
noeviction: Redis rejects new write operations when memory is full, returning an error to the client. This ensures data integrity but may disrupt applications expecting writes to succeed. Ideal for scenarios where data loss is unacceptable.
allkeys-lru: Evicts the least recently used (LRU) keys across all keys, regardless of whether they have an expiration set. This prioritizes keeping frequently accessed data, making it suitable for caching workloads.
allkeys-random: Randomly removes keys from the entire keyspace. It's fast and simple but lacks precision, as it doesn't consider access patterns. Use this in low-overhead scenarios where key importance is uniform.
volatile-lru: Applies the LRU algorithm only to keys with an expiration (TTL) set. Non-expiring keys are untouched, making this a good choice when only some keys are safe to evict.
volatile-random: Randomly removes keys with an expiration set. Like
allkeys-random, it’s lightweight but less predictable, suitable for workloads with many expiring keys.volatile-ttl: Evicts keys with the shortest remaining time-to-live (TTL) among those with an expiration set. This maximizes memory availability by removing keys that would expire soon anyway.
How Redis Implements Eviction
When memory usage hits the maxmemory limit, Redis triggers eviction based on the configured policy. For LRU-based
policies (allkeys-lru, volatile-lru), Redis approximates LRU by sampling a small subset of keys (configurable via
maxmemory-samples, default 5) to balance accuracy and performance. Random policies (allkeys-random,
volatile-random) select keys without considering usage or TTL, while volatile-ttl prioritizes keys closest to
expiration.
Eviction only occurs when memory is needed for new writes, and Redis ensures minimal disruption by evicting
incrementally. The maxmemory directive must be set explicitly (e.g., maxmemory 100mb) to enable eviction;
otherwise, Redis may crash if memory is exhausted.
Choosing the Right Policy
Match the policy to your workload. If all your keys are cacheable, use allkeys-lru for recency or allkeys-random for simplicity. If some keys hold persistent data, set expirations on the cache keys and use volatile-lru or volatile-ttl. If losing data is unacceptable, use noeviction — but monitor memory closely, because writes fail once memory fills. If CPU cost matters more than precision, a random policy is the cheapest option.
Best Practices
- Set
maxmemory: Always configuremaxmemoryto prevent Redis from consuming all system memory. A good rule is 70-80% of available RAM, leaving room for system processes. - Use expirations strategically: Policies like
volatile-lruorvolatile-ttlwork best when keys have TTLs. Set expirations on temporary data to leverage these policies. - Monitor memory usage: Use the
INFO MEMORYcommand to trackused_memoryandmaxmemoryusage. Tools like RedisInsight can help visualize memory trends. - Tune sampling: Increase
maxmemory-samples(e.g., to 10) for better LRU accuracy in large datasets, but note the CPU trade-off. - Test under load: Simulate memory pressure to ensure your chosen policy behaves as expected, especially for latency-sensitive applications.
Common Pitfalls
Setting maxmemory too low causes constant evictions; too high can destabilize the host. Policies such as volatile-lru and volatile-ttl only work if keys actually have expirations. And under noeviction, writes fail outright — applications that don't handle rejected commands will see those errors cascade.
Conclusion
Redis eviction policies are a powerful tool for managing memory in constrained environments. By understanding your
workload—whether it’s a cache, persistent store, or hybrid—you can select a policy that balances performance, data
retention, and stability. Configure maxmemory, monitor usage, and test your setup to ensure Redis performs optimally
under pressure.