Incident Response

Redis OOM "command not allowed": maxmemory and Eviction Policies Explained

Writes start failing with "OOM command not allowed", or the kernel kills Redis in the middle of the night. Both come down to memory limits nobody set on purpose. How maxmemory works, which eviction policy to pick and how to size Redis so it stays up.

· 11 min read · AG Data Team

If your application suddenly logs OOM command not allowed when used memory > 'maxmemory', Redis is doing exactly what it was told to do: it has hit its memory limit and is refusing writes instead of evicting keys. The opposite failure is worse: no limit at all, Redis grows until the Linux OOM killer ends it, and your cache or queue disappears in one go. Both have the same root cause: a Redis maxmemory and eviction policy setup that nobody chose deliberately. This guide covers how the limit works, how to pick an eviction policy and how to size memory realistically. Everything here applies to Valkey as well.

What the OOM error actually means

Redis checks memory usage whenever a client runs a command that adds data. If usage is above maxmemory, Redis applies the configured maxmemory-policy. With the default policy, noeviction, nothing is evicted: the server "will return an error when you try to execute commands that cache new data", while reads keep working (Redis docs: key eviction).

So the error isn't a crash. It's Redis protecting itself. The question is whether that is the behaviour you want, and the answer depends on what you store in it.

First decide: is this Redis a cache or a database?

This is the decision everything else follows from.

The eviction policies, in plain words

The trap: volatile-* policies behave like noeviction if no keys have a TTL. Teams set volatile-lru, forget that half the application writes keys without an expiry, and get OOM errors anyway. If you choose a volatile policy, check that the keys you expect to be evicted really have TTLs.

Redis LRU and LFU are approximations: Redis samples a few keys (maxmemory-samples, default 5) and evicts the best candidate among them. Raising the sample count to 10 gets closer to true LRU at some CPU cost. For most workloads 5 is fine.

Sizing maxmemory: why "all the RAM" is wrong

On 64-bit systems maxmemory defaults to 0, meaning no limit. That's how Redis ends up killed by the kernel. Set it explicitly, and leave room for things that are not your data:

  1. Overhead and fragmentation. The Redis admin guide says it directly: "if you think you have 10 GB of free memory, set it to 8 or 9" (Redis administration).
  2. Replication and AOF buffers. Memory for buffers waiting to go to replicas or the AOF file is not counted towards maxmemory. With replication or persistence, leave some RAM free for them. INFO memory shows the current amount as mem_not_counted_for_evict.
  3. Fork during saves. When Redis writes an RDB snapshot or rewrites the AOF, it forks. In a write-heavy application, "Redis can use up to 2 times the memory normally used" during the save, because modified pages get copied. Size for this, or the save is exactly when the OOM killer strikes.
  4. Other processes on the box: monitoring agents, Sentinel, the OS page cache for persistence files.

The admin guide also recommends vm.overcommit_memory = 1 so background saves don't fail on fork(), and disabling Transparent Huge Pages, which can hurt memory usage and latency.

Diagnosing a Redis that is running out of memory

redis-cli INFO memory
redis-cli INFO stats
redis-cli MEMORY DOCTOR
redis-cli --bigkeys

What to do when it's happening right now

  1. If it's a pure cache, switching to an evicting policy at runtime is quick: CONFIG SET maxmemory-policy allkeys-lru. Then persist it in the config file.
  2. If it holds important data, do not switch to eviction in a panic: you would be deleting data to make the error go away. Raise maxmemory if the host really has free RAM, or scale up, then find out what grew.
  3. Look for the key that exploded. A runaway list or set is common and fixing that one key often frees most of the memory.
  4. Check for keys without TTLs that were supposed to have them.

Prevent it from happening again

Key takeaways

Redis Valkey Memory Eviction
Work with us

Redis running out of memory?

We size, tune and troubleshoot Redis and Valkey in production: memory limits, eviction, persistence, replication and failover.

Get in touch More from the blog