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. 1.Check fragmentation ratio:
  2. 2.```bash
  3. 3.redis-cli INFO memory | grep fragmentation
  4. 4.`
  5. 5.Enable active defragmentation:
  6. 6.```bash
  7. 7.redis-cli CONFIG SET activedefrag yes
  8. 8.redis-cli CONFIG SET active-defrag-threshold-lower 10
  9. 9.redis-cli CONFIG SET active-defrag-threshold-upper 100
  10. 10.`
  11. 11.Configure in redis.conf:
  12. 12.`
  13. 13.activedefrag yes
  14. 14.active-defrag-ignore-bytes 100mb
  15. 15.active-defrag-threshold-lower 10
  16. 16.active-defrag-threshold-upper 100
  17. 17.active-defrag-cycle-min 5
  18. 18.active-defrag-cycle-max 75
  19. 19.`