What's Actually Happening

Redis reaches configured memory limit and cannot accept new writes. OOM errors occur when trying to store more data.

The Error You'll See

```bash $ redis-cli SET key value

(error) OOM command not allowed when used memory > 'maxmemory'. ```

Or:

```bash $ redis-cli INFO memory

used_memory:1073741824 maxmemory:1073741824 ```

Why This Happens

  1. 1.maxmemory limit reached
  2. 2.No eviction policy set
  3. 3.Large keys stored
  4. 4.Memory fragmentation
  5. 5.Unbounded data growth

Step 1: Check Memory Usage

bash
redis-cli INFO memory
redis-cli MEMORY STATS
redis-cli --bigkeys

Step 2: Check Current Limit

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

Step 3: Increase Memory Limit

bash
redis-cli CONFIG SET maxmemory 2gb
# In redis.conf
maxmemory 2gb

Step 4: Configure Eviction Policy

bash
redis-cli CONFIG SET maxmemory-policy allkeys-lru
# Options:
# volatile-lru, allkeys-lru, volatile-lfu, allkeys-lfu
# volatile-random, allkeys-random, volatile-ttl, noeviction

Step 5: Find Large Keys

bash
redis-cli --bigkeys
redis-cli MEMORY USAGE keyname
redis-cli SCAN 0 MATCH prefix:* COUNT 1000

Step 6: Delete Unused Keys

bash
redis-cli DEL key1 key2
redis-cli --scan --pattern "temp:*" | xargs redis-cli DEL
redis-cli FLUSHDB  # Clear all (caution!)

Step 7: Set Key Expiration

bash
redis-cli EXPIRE key 3600
redis-cli SETEX key 3600 value
redis-cli TTL key

Step 8: Optimize Data Structures

```bash # Use hashes for related fields HSET user:1 name "John" email "john@example.com" # Instead of separate keys for each field

# Use sets for unique collections SADD tags "redis" "cache" "database" ```

Step 9: Enable Memory Optimization

bash
# In redis.conf
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
zset-max-ziplist-entries 128

Step 10: Monitor Memory

bash
watch -n 5 'redis-cli INFO memory | grep used_memory'
redis-cli --latency
  • [Fix Redis Connection Refused](/articles/fix-redis-connection-refused)
  • [Fix Redis Authentication Failed](/articles/fix-redis-authentication-failed)