12. Common Issues & Troubleshooting
Q1: Connection Refused
bash
# Error message
Could not connect to Redis at 127.0.0.1:6379: Connection refused
# Troubleshooting steps
# 1. Check if Redis is running
sudo systemctl status redis-server
ps aux | grep redis
# 2. Check if port is listening
ss -tlnp | grep 6379
# 3. Check firewall
sudo ufw allow 6379
sudo iptables -L -n | grep 6379
# 4. Check bind configuration
CONFIG GET bindbash
redis-server.service - Advanced key-value store
Active: active (running) since Sat 2024-01-01 10:00:00 CST
LISTEN 0 128 127.0.0.1:6379 0.0.0.0:* users:(("redis-server",pid=1234,fd=6))
1) "bind"
2) "127.0.0.1"Q2: Out of Memory (OOM)
bash
# Error message
OOM command not allowed when used memory > 'maxmemory'.
# Troubleshooting
INFO memory
# Look at: used_memory_human, maxmemory_human, mem_fragmentation_ratio
# Solutions
# 1. Increase maxmemory
CONFIG SET maxmemory 8gb
# 2. Set a reasonable eviction policy
CONFIG SET maxmemory-policy allkeys-lru
# 3. Clean up expired data
SCAN 0 MATCH "temp:*" COUNT 1000
# 4. Check fragmentation ratio (if > 1.5, consider cleanup)
CONFIG SET activedefrag yesbash
# Memory
used_memory:4294967296
used_memory_human:4.00G
maxmemory:4294967296
maxmemory_human:4.00G
mem_fragmentation_ratio:1.25
OK
OKQ3: Master-Replica Sync Delay
bash
# Check replication offset difference
INFO replication
# Key fields to watch:
# master_repl_offset vs slave_repl_offset
# Larger difference = more delay
# Solutions
# 1. Ensure sufficient network bandwidth
# 2. Avoid big key writes (writing a List with 1 million elements at once)
# 3. Increase repl_backlog_size
CONFIG SET repl-backlog-size 256mbbash
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.102,port=6379,state=online,offset=1234567,lag=5
master_repl_offset:1234567
OKQ4: Cache Penetration / Breakdown / Avalanche
| Problem | Description | Solution |
|---|---|---|
| Cache Penetration | Querying data that doesn't exist — both cache and database return nothing | Bloom filter / Cache null values (with short TTL) |
| Cache Breakdown | Hot key expires instantly, flooding requests hit the database | Mutex lock (SETNX) / Logical expiration / Never expire |
| Cache Avalanche | Many keys expire simultaneously, all requests hit the database | Add random value to TTL / Multi-level cache / Circuit breaker & degradation |
bash
# Prevent cache avalanche: add random offset to expiration
SET product:1001 "{json_data}" EX 3540 # 3600 - random(0, 120)
# Prevent cache breakdown: distributed lock
# Only one thread queries database and writes to cache
SET lock:product:1001 "1" NX EX 5