Video in progress

Strong Middle 🏗️ System design

What happens when Redis memory fills up, and how do you choose `maxmemory-policy` for a cache?

When it reaches maxmemory, Redis either evicts keys according to the policy or rejects writes with an error. For a pure cache it's usually allkeys-lru or allkeys-lfu; if the Redis holds a mix of cache and important data, use volatile-* or, better, split them into separate instances.

📖 Answer

The default is noeviction: memory runs out — SET returns an OOM command not allowed error. For a Redis that serves as a cache that is almost always the wrong choice: the application can't write to the cache and starts throwing errors.

The main options:

Policy What it does
allkeys-lru evicts the least recently used
allkeys-lfu evicts the least frequently used
volatile-lru / volatile-lfu only among keys with a TTL
volatile-ttl the one expiring soonest
allkeys-random random

The practical difference between LRU and LFU: a one-time scan (for example, a report read every product once) "washes out" an LRU cache — one-time keys remain instead of the hot ones. LFU is more resistant to this. LRU in Redis isn't exact either — it picks from a sample (maxmemory-samples), but that's good enough in practice.

The risk of volatile-*: if there are many keys without a TTL, there are no candidates left to evict and Redis behaves like noeviction.

What I pay attention to:

  • Cache and data shouldn't share one Redis. Sessions, rate limit counters, queues — eviction silently deletes them. Use a separate instance, or at least leave their keys without a TTL and use volatile-*.
  • Fragmentation. If mem_fragmentation_ratio in INFO memory is above 1.5, the real RSS is much larger than maxmemory — and the container gets OOMKilled. activedefrag yes helps.
  • Replication and persistence buffers take memory too; set maxmemory to ~70–75% of the container limit.
  • If the evicted_keys metric keeps growing and the hit rate is dropping, that's a sign the cache is too small.