CRITICAL

How to Fix Redis OOM command not allowed (Maxmemory)

Quick Fix Summary

TL;DR

Immediately increase `maxmemory` or change the `maxmemory-policy` to an evicting policy like `allkeys-lru`.

Redis has reached its configured memory limit (`maxmemory`) and the current eviction policy (`maxmemory-policy`) is set to `noeviction`. This prevents new writes to protect data integrity but halts application functionality.

Diagnosis & Causes

  • `maxmemory-policy` is set to `noeviction` (default).
  • Sudden traffic spike filling memory with new keys.
  • Large batch job inserting data without TTLs.
  • Memory leak in the application or Lua scripts.
  • Insufficient `maxmemory` limit for the dataset size.
  • Recovery Steps

    1

    Step 1: Immediate Relief - Change Eviction Policy

    Dynamically switch to a policy that allows eviction to restore write operations. `allkeys-lru` is a safe, general-purpose choice.

    bash
    redis-cli CONFIG SET maxmemory-policy allkeys-lru
    2

    Step 2: Investigate Memory Usage

    Identify what is consuming memory. Use `INFO memory` and `MEMORY STATS` for a high-level view, then drill down with `MEMORY USAGE` on large keys.

    bash
    redis-cli INFO memory | grep -E "(used_memory_human|maxmemory_human|mem_fragmentation_ratio)
    redis-cli MEMORY STATS
    redis-cli --bigkeys
    3

    Step 3: Apply a Permanent Configuration Fix

    Update the Redis configuration file (`redis.conf`) to prevent recurrence. Choose an appropriate policy and set a realistic `maxmemory` limit.

    bash
    # Edit /etc/redis/redis.conf
    maxmemory 4gb
    maxmemory-policy allkeys-lru
    # Then restart Redis: sudo systemctl restart redis-server
    4

    Step 4: Implement Proactive Monitoring

    Set up alerts for memory usage (e.g., >80% of `maxmemory`) to catch issues before they cause OOM errors.

    yaml
    # Example Prometheus Alert Rule
    - alert: RedisMemoryHigh
      expr: redis_memory_used_bytes / redis_memory_max_bytes > 0.8
      for: 5m
    5

    Step 5: Optimize Application Patterns

    Review and fix application logic that leads to unbounded memory growth. Implement TTLs, avoid large monolithic keys, and pipeline commands efficiently.

    bash
    # Use TTLs for transient data
    redis-cli SETEX mykey 3600 "myvalue"
    # Use hashes or lists instead of many separate keys for related data.

    Architect's Pro Tip

    "In containerized environments (K8s/Docker), set `maxmemory` to ~80% of the container memory limit. This prevents the OS OOM killer from terminating the Redis process before its internal policy can act."

    Frequently Asked Questions

    What's the difference between Redis OOM and the OS OOM Killer?

    Redis OOM is a controlled error from Redis itself when `maxmemory` is reached. The OS OOM Killer is a Linux system process that terminates *any* process (including Redis) when the *entire system* runs out of RAM, which is more destructive.

    Which `maxmemory-policy` should I use in production?

    For caching: `allkeys-lru`. For a mixed dataset with persistent keys: `volatile-lru` (evicts only keys with a TTL). For predictable memory usage: `allkeys-random`. Avoid `noeviction` unless data persistence is absolutely critical and you have other safeguards.

    Related Redis Guides