DatabaseHigh
Fix: Redis Memory Limit Exceeded
Redis returns OOM command not allowed when used memory > maxmemory
!Symptoms
- Redis returns OOM command not allowed when used memory > maxmemory
- Write operations fail but reads still work
- Application cache errors or increased latency
- Redis logs show 'Can't save in background: fork: Cannot allocate memory'
?Root Causes
- No TTL set on cached keys, causing unbounded growth
- maxmemory-policy set to noeviction (default) so Redis refuses writes
- Large data structures (sorted sets, lists) growing without bounds
- Background save (RDB/AOF) requiring fork doubles memory usage temporarily
- Memory fragmentation ratio is high
#Diagnosis Steps
- 1Check memory usage: `redis-cli INFO memory`
- 2Check maxmemory setting: `redis-cli CONFIG GET maxmemory`
- 3Check eviction policy: `redis-cli CONFIG GET maxmemory-policy`
- 4Find large keys: `redis-cli --bigkeys`
- 5Check memory fragmentation ratio in INFO memory output
>Fix
- 1Set an eviction policy: `redis-cli CONFIG SET maxmemory-policy allkeys-lru`
- 2Increase maxmemory if the server has available RAM
- 3Delete unnecessary keys: identify and remove unused data
- 4Add TTL to keys that should expire: `EXPIRE key seconds`
- 5Disable background saving if fork is the issue: configure AOF instead of RDB
*Prevention
- Always set maxmemory and an appropriate eviction policy
- Set TTL on all cache keys at write time
- Monitor Redis memory usage with alerts at 80% of maxmemory
- Use Redis Cluster for horizontal scaling when a single node is insufficient
- Regularly audit key patterns and sizes with redis-cli --bigkeys
Related Error Messages
OOM command not allowed when used memory > 'maxmemory'MISCONF Redis is configured to save RDB snapshotsCan't save in background: fork: Cannot allocate memoryERR max number of clients reached