Introduction
Redis replica lag high when master write rate exceeds replication bandwidth. This guide provides step-by-step diagnosis and resolution.
Symptoms
Typical error output:
bash
Warning: Replication lag high
master_repl_offset: 100000000, slave_repl_offset: 99900000
Lag: 1000000 bytesCommon Causes
- 1.Network bandwidth between master and replica insufficient
- 2.Master write rate exceeding replication speed
- 3.TCP buffer or timeout misconfigured
- 4.Disk I/O on replica slow
Step-by-Step Fix
Step 1: Check Current State
bash
redis-cli INFO replication
redis-cli --latency
redis-cli --latency-historyStep 2: Identify Root Cause
bash
redis-cli INFO
redis-cli CONFIG GET "*"
redis-cli SLOWLOG GET 10Step 3: Apply Primary Fix
```bash # Increase output buffer for replica redis-cli CONFIG SET client-output-buffer-limit "replica 256mb 64mb 60"
# Increase TCP buffer redis-cli CONFIG SET repl-backlog-size 256mb
# Check network latency redis-cli --latency -h <master-ip> ```
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 replication | grep lag
# lag should be 0 or minimalCommon 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)
Related Issues
- Redis Connection Refused
- Redis High Latency
- Redis Cluster Down
- Redis Persistence Failed