Lesson 4 of 9 · 26m
Memory Pressure and Eviction Policies Are Application Semantics
Choose maxmemory behavior deliberately, distinguish cache eviction from data loss, and read the metrics that appear before users notice.
The first time maxmemory bites a team, it rarely looks like Redis documentation. It looks like random users being logged out, missing rate-limit counters, or background jobs disappearing. Redis did exactly what it was told. The failure was treating eviction configuration as infrastructure trivia rather than application semantics.
Start with a memory budget
Redis is in-memory. Your practical ceiling is not just RAM size; it includes fragmentation, replication buffers, persistence forks, client output buffers, and headroom for the operating system:
maxmemory 6gb
maxmemory-policy allkeys-lru
maxmemory-samples 10On a 8 GB host, 6gb is not automatically correct. Fork-based persistence can temporarily copy dirty pages, so a heavily written dataset can consume much more memory during a save. Leave room or accept the outage you engineered.
Eviction policy is a business decision
noeviction writes fail when memory is full
allkeys-lru evict least recently used keys regardless of TTL
allkeys-lfu evict least frequently used keys
volatile-lru evict only keys with TTLs
volatile-ttl evict keys with the shortest remaining TTLallkeys-lru is reasonable when Redis is truly a cache and missing data can be rebuilt. It is dangerous when some keys are durable workflow state. volatile-* protects non-expiring keys, but if most keys lack TTLs the policy can behave like noeviction in practice.
The error tells you which contract broke
OOM command not allowed when used memory > 'maxmemory'That is not a capacity mystery. It is Redis refusing a write under noeviction or failing to free enough memory. Check immediately:
redis-cli info memory | grep -E 'used_memory_human|maxmemory_human|mem_fragmentation'
redis-cli info stats | grep evicted_keys
redis-cli config get maxmemory-policyA nonzero and rising evicted_keys means data loss has already become normal operation. For a pure cache that may be acceptable. For sessions, queues, and locks it is an incident.
Large keys distort eviction and latency
Eviction chooses keys, not portions of keys. A giant hash, set, or stream can consume a large percentage of memory and still be one eviction candidate. It can also block the server while being deleted. Use asynchronous deletion when appropriate:
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yesDo not treat lazy free as permission to hoard huge values. It reduces synchronous pause risk; it does not reduce memory cost.
Field notes
- Monitor
used_memory,maxmemory,evicted_keys, fork latency, and hit ratio together. One metric in isolation lies. - LRU is approximate, not exact.
maxmemory-samplescontrols precision versus CPU overhead. - Keys without TTL in a volatile-only policy are effectively pinned; know whether that was intentional.
- Separate instances by workload when semantics conflict. One Redis can be both cache and system of record, but operations become much harder to reason about.
Was this lesson helpful?