Introduction

Redis out of memory when maxmemory limit reached and eviction policy cannot free space. This guide provides step-by-step diagnosis and resolution.

Symptoms

Typical error output:

bash
Error: OOM command not allowed when used_memory > maxmemory
maxmemory limit: 4GB, used_memory: 4.1GB
Eviction policy: noeviction

Common Causes

  1. 1.maxmemory limit too low for dataset
  2. 2.Eviction policy set to noeviction
  3. 3.Keys without TTL accumulating
  4. 4.Large objects not being evicted

Step-by-Step Fix

Step 1: Check Current State

bash
redis-cli INFO memory
redis-cli CONFIG GET maxmemory
redis-cli CONFIG GET maxmemory-policy

Step 2: Identify Root Cause

bash
redis-cli INFO
redis-cli CONFIG GET "*"
redis-cli SLOWLOG GET 10

Step 3: Apply Primary Fix

```bash # Increase maxmemory or change eviction redis-cli CONFIG SET maxmemory 8gb redis-cli CONFIG SET maxmemory-policy allkeys-lru

# Or enable volatile-lru with TTL redis-cli CONFIG SET maxmemory-policy volatile-lru ```

Step 4: Apply Alternative Fix

```bash # Alternative fix: Check configuration redis-cli CONFIG GET "*memory*" redis-cli CONFIG GET "*timeout*"

# Update settings redis-cli CONFIG SET parameter value

# Verify the fix redis-cli INFO ```

Step 5: Verify the Fix

bash
redis-cli INFO memory | grep used_memory_human
redis-cli CONFIG GET maxmemory
# used_memory < maxmemory

Common Pitfalls

  • Not using connection pooling for high-throughput applications
  • Setting maxmemory without eviction policy
  • Using KEYS command in production
  • Not monitoring replication lag

Best Practices

  • Monitor Redis memory and fragmentation regularly
  • Use SCAN instead of KEYS for key enumeration
  • Configure appropriate eviction policy for workload
  • Implement proper backup strategy (RDB or AOF)
  • Redis Connection Refused
  • Redis High Latency
  • Redis Cluster Down
  • Redis Persistence Failed