How to Choose the Right Redis Memory Eviction Policy and Size Your Cache
When expired keys accumulate and lazy deletion can't keep up, Redis relies on configurable memory eviction policies and proper maxmemory settings, and selecting the appropriate strategy along with an optimal cache size (typically 15‑30% of total data) ensures stable performance and avoids out‑of‑memory errors.
Configuring maxmemory
The total memory limit for a Redis instance is set with the maxmemory directive. It can be changed at runtime using CONFIG SET maxmemory 4gb or made permanent by adding maxmemory 4gb to redis.conf. A value of 0 removes the limit on 64‑bit systems; on 32‑bit systems the implicit ceiling is about 3GB.
Redis eviction policies
Redis provides eight eviction strategies. They fall into two broad categories: a policy that never evicts, and seven policies that actively evict keys.
noeviction
When memory is exhausted, Redis returns an error and does not evict any keys.
Volatile (expire‑set) policies
volatile-lru : evicts the least‑recently‑used keys that have an expiration time.
volatile-lfu : evicts the least‑frequently‑used keys that have an expiration time (added after Redis 4.0).
volatile-random : randomly evicts an expiring key.
volatile-ttl : evicts the key whose TTL is closest to expiring.
All‑keys policies
allkeys-lru : evicts the least‑recently‑used key regardless of expiration.
allkeys-lfu : evicts the least‑frequently‑used key.
allkeys-random : randomly evicts any key.
Eviction execution flow
1. Client sends a new command to the server. 2. Server checks current memory usage; if it exceeds maxmemory , it selects keys to evict according to the configured policy. 3. The original command is then executed.
When to use specific policies
allkeys-lru : ideal when the workload exhibits a clear hot‑cold data split; it keeps frequently accessed data in memory.
allkeys-random : suitable for uniformly random access patterns with no distinct hot set.
volatile-lru : best for scenarios where some keys must never be evicted (e.g., pinned news or videos) and only expiring keys are candidates for eviction.
Setting an expire on a key consumes a small amount of memory; using allkeys-lru can be more memory‑efficient. For workloads that mix non‑evictable and fully evictable data, deploying separate Redis instances or clusters for each type is recommended.
Cache size recommendation
Cache size should be the smallest memory that satisfies performance requirements. Because of the 80/20 rule, roughly 20 % of the data generates 80 % of the traffic, but real‑world variations mean a safe range is 15 %–30 % of the total data volume.
volatile‑lru vs volatile‑lfu
volatile-lru uses a classic LRU algorithm to discard the least‑recently‑used expiring keys. volatile-lfu builds on LRU by also considering access frequency, removing the keys with the lowest usage count.
References: 1. https://redis.io/docs/manual/eviction/ 2. Redis Core Technology and Practice
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
