Advertisement
Advanced Redis, Redis Memory Optimization

What is memory fragmentation in Redis, and how do you detect and address it?

Short answer

Memory fragmentation happens when the memory allocator used by Redis leaves gaps between allocated pieces of memory that cannot be efficiently reused, causing Redis's actual memory usage as reported by the operating system to be noticeably higher than the amount of data it is…

Save Redis mock interview
Advertisement
AModel answer
Memory fragmentation happens when the memory allocator used by Redis leaves gaps between allocated pieces of memory that cannot be efficiently reused, causing Redis's actual memory usage as reported by the operating system to be noticeably higher than the amount of data it is actually storing, which you detect by checking the mem_fragmentation_ratio and can often address by enabling Redis's active defragmentation feature.
Example

INFO memory | grep mem_fragmentation_ratio CONFIG SET activedefrag yes

Real-world example
A team notices their Redis instance's memory usage reported by the operating system was significantly higher than expected, discovers a high fragmentation ratio, and resolves much of the issue by enabling active defragmentation.
Likely follow-ups
  • What fragmentation ratio value is considered acceptable versus concerning?
  • What is the performance cost of running active defragmentation?
Related topics