Introduction
Memory fragmentation can cause Redis to consume significantly more physical memory than the actual data size. This guide explains fragmentation and how to address it.
Symptoms
- used_memory much lower than used_memory_rss
- High mem_fragmentation_ratio (>1.5)
- OS OOM despite Redis showing available memory
Step-by-Step Fix
- 1.Check fragmentation ratio:
- 2.```bash
- 3.redis-cli INFO memory | grep fragmentation
- 4.
` - 5.Enable active defragmentation:
- 6.```bash
- 7.redis-cli CONFIG SET activedefrag yes
- 8.redis-cli CONFIG SET active-defrag-threshold-lower 10
- 9.redis-cli CONFIG SET active-defrag-threshold-upper 100
- 10.
` - 11.Configure in redis.conf:
- 12.
` - 13.activedefrag yes
- 14.active-defrag-ignore-bytes 100mb
- 15.active-defrag-threshold-lower 10
- 16.active-defrag-threshold-upper 100
- 17.active-defrag-cycle-min 5
- 18.active-defrag-cycle-max 75
- 19.
`