The MAXMEMORY noeviction policy in Redis is failing to block writes because a background eviction process is still running, preventing new data from being written.

Here’s a breakdown of why this happens and how to fix it:

1. The Real Culprit: maxmemory-samples

  • Diagnosis: The most common reason noeviction seems to fail is that Redis, by default, doesn’t check every key when maxmemory is hit. Instead, it samples a small number of keys (maxmemory-samples, default 5) to decide if eviction is needed. If the sampled keys are not eligible for eviction (e.g., they are recent or have no TTL), Redis might continue accepting writes even if the memory limit is technically exceeded, leading to a crash.
  • Fix: Increase maxmemory-samples to a higher value. A common recommendation is maxmemory-samples 10.
  • Why it works: A larger sample size increases the probability that Redis will find keys eligible for eviction when the memory limit is approached, thus triggering the eviction process more reliably before memory runs out completely.
  • Command: Edit your redis.conf file and add or modify the line:
    maxmemory-samples 10
    
    Then, restart your Redis instance.

2. The maxmemory Limit Itself is Too High (Relative to Available RAM)

  • Diagnosis: You’ve set maxmemory very close to your server’s total RAM, leaving no buffer for Redis’s internal data structures, operating system overhead, or other processes. When Redis tries to evict, it needs a small amount of temporary memory to do so, and if there’s no headroom, even eviction can fail.
  • Fix: Lower your maxmemory setting. If your server has 4GB RAM, don’t set maxmemory to 3.9GB. Aim for maxmemory 3GB or maxmemory 3.5GB to leave breathing room.
  • Why it works: Providing a buffer ensures that Redis can perform its eviction operations without immediately hitting an out-of-memory condition itself, allowing the noeviction policy to eventually block writes.
  • Command: Edit your redis.conf file and set:
    maxmemory 3GB
    
    (Replace 3GB with an appropriate value based on your server’s total RAM). Restart Redis.

3. Eviction Strategy Mismatch

  • Diagnosis: While noeviction is selected, the underlying eviction mechanism might be inefficient for your data. If you have many keys without TTLs, Redis has nothing to evict. noeviction means "don’t evict anything," but it assumes that if memory is full, it can evict if it were configured to do so. If there’s truly nothing to evict, it will eventually fail.
  • Fix: Temporarily switch to an eviction policy that can evict, like allkeys-lru, to clear out memory. Then, re-evaluate your data strategy. If you need noeviction, you must have keys with TTLs or a plan to manage memory proactively.
  • Why it works: allkeys-lru will aggressively remove the least recently used keys, making space. This is a temporary measure to get Redis back online; it doesn’t solve the long-term noeviction problem if your data model doesn’t support it.
  • Command: In redis-cli:
    CONFIG SET maxmemory-policy allkeys-lru
    
    After memory is freed, you can then decide if noeviction is truly appropriate or if you need a different strategy. If you must use noeviction, ensure you have TTLs on your keys.

4. Background Save/AOF Rewrite Interference

  • Diagnosis: Redis performs background operations like RDB saves (BGSAVE) and AOF rewrites (BGREWRITEAOF). These operations fork the main Redis process, which can temporarily double the memory usage. If you’re already at your maxmemory limit, this fork can exhaust available RAM and cause the parent process to crash.
  • Fix: Temporarily disable or postpone background saves/rewrites when you anticipate hitting maxmemory. You can also configure stop-writes-on-bgsave-error yes in redis.conf to make Redis explicitly stop writes during a BGSAVE if it fails due to memory pressure.
  • Why it works: Preventing or explicitly halting operations that consume significant extra memory during critical memory states allows Redis to manage its primary write/read operations and eviction attempts more gracefully.
  • Command:
    • To disable RDB snapshots (not recommended long-term): Edit redis.conf, comment out the save lines.
    • To disable AOF (not recommended long-term): Edit redis.conf, set appendonly no.
    • To ensure writes stop on save errors: Edit redis.conf and add:
      stop-writes-on-bgsave-error yes
      
    Restart Redis after configuration changes.

5. maxmemory-policy not actually set to noeviction

  • Diagnosis: It sounds basic, but sometimes the configuration isn’t applied correctly or was reverted. You might think it’s set to noeviction, but it’s actually using a different policy that is evicting, or worse, a policy that also doesn’t evict but has other issues.
  • Fix: Explicitly set the policy.
  • Why it works: Ensures the intended policy is active.
  • Command: In redis-cli:
    CONFIG SET maxmemory-policy noeviction
    
    You can verify with CONFIG GET maxmemory-policy.

6. External Memory Pressure

  • Diagnosis: The maxmemory setting in Redis only controls Redis’s own memory usage. The operating system and other processes on the server might be consuming significant RAM. If the OS starts swapping, Redis performance degrades drastically, and it might appear to be blocking writes due to memory pressure even if its internal maxmemory limit hasn’t been strictly breached yet.
  • Fix: Monitor overall system memory usage. Reduce memory consumption by other processes, or add more RAM to the server. Ensure vm.swappiness is set appropriately (e.g., 10 or 20 on production systems) to discourage swapping.
  • Why it works: By ensuring the OS has sufficient memory and is not swapping, Redis can operate within its configured limits without external interference.
  • Command: Check system memory with free -h and top. Adjust swappiness:
    sudo sysctl vm.swappiness=10
    
    To make it permanent, edit /etc/sysctl.conf.

After applying these fixes, you should find that Redis correctly blocks writes when maxmemory is reached under the noeviction policy. The next error you’ll encounter is likely related to the specific operation that’s failing due to the block, such as OOM command not allowed when used memory > 'maxmemory'.

Want structured learning?

Take the full Redis course →