0
0
RedisDebug / FixBeginner · 4 min read

How to Fix Redis Out of Memory Error Quickly

To fix Redis out of memory errors, increase the max memory limit with maxmemory or enable an eviction policy like allkeys-lru to remove old keys automatically. Also, check for memory leaks or large keys and clean unnecessary data to free memory.
🔍

Why This Happens

Redis runs out of memory when it tries to store more data than the configured memory limit allows. This can happen if the maxmemory setting is too low or if no eviction policy is set to remove old data. Large keys or memory leaks in your application can also cause this error.

redis
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "1000000"
127.0.0.1:6379> SET bigkey "<very large value>"
(error) OOM command not allowed when used memory > 'maxmemory'.
Output
(error) OOM command not allowed when used memory > 'maxmemory'.
🔧

The Fix

Increase the maxmemory setting to allow Redis to use more memory. Also, set an eviction policy like allkeys-lru so Redis can remove the least recently used keys when memory is full. This prevents errors by freeing space automatically.

redis
127.0.0.1:6379> CONFIG SET maxmemory 20000000
OK
127.0.0.1:6379> CONFIG SET maxmemory-policy allkeys-lru
OK
127.0.0.1:6379> SET bigkey "<very large value>"
OK
Output
OK
🛡️

Prevention

To avoid out of memory errors, monitor Redis memory usage regularly and set a reasonable maxmemory limit based on your server capacity. Use eviction policies like volatile-lru or allkeys-lru to manage memory automatically. Avoid storing very large keys or unnecessary data, and consider using Redis memory optimization commands like MEMORY USAGE to find big keys.

⚠️

Related Errors

Other memory-related errors include:

  • Memory fragmentation: Causes inefficient memory use; fix by restarting Redis or tuning allocator.
  • Slow commands due to large keys: Avoid very large strings or hashes.
  • Persistence issues: Large datasets can slow RDB or AOF saves; tune save intervals.

Key Takeaways

Increase Redis maxmemory setting to allow more data storage.
Set an eviction policy like allkeys-lru to free memory automatically.
Monitor memory usage and avoid storing very large keys.
Use Redis commands to find and clean big or unnecessary data.
Regularly tune Redis configuration to match your workload.