Understanding Redis Keyspace Fragmentation

Redis relies on the system allocator (like jemalloc or glibc malloc) to manage memory. When large numbers of keys are inserted and deleted dynamically, memory blocks become fragmented, making it difficult for Redis to efficiently reuse freed memory.

Common Causes of Keyspace Fragmentation

  • High churn of short-lived keys: Frequent insertions and deletions create fragmented memory regions.
  • Large key sizes with varied lengths: Irregular data structures prevent efficient compaction.
  • Defragmentation limitations: Even when Redis runs defragmentation, it might not reclaim all memory efficiently.

Diagnosing Keyspace Fragmentation

Checking Fragmentation Ratio

Run:

INFO memory

Look for the mem_fragmentation_ratio. A value above 1.5 indicates severe fragmentation.

Analyzing Memory Usage

Use:

MEMORY STATS

Check for high allocator overhead.

Fixing Keyspace Fragmentation

Triggering Active Defragmentation

Enable active defragmentation:

CONFIG SET activedefrag yes

Reshuffling Data Structures

Convert large objects into smaller, more uniform structures:

HSET user:1001 name "Alice" age "30"

Forcing Memory Release

Restart Redis to free fragmented memory:

SAVE
SHUTDOWN

Then restart.

Preventing Future Fragmentation

  • Use activedefrag in high-churn environments.
  • Regularly monitor memory metrics.
  • Avoid unnecessary large keys and varied data structures.

Conclusion

Redis keyspace fragmentation can degrade performance over time, but by using active defragmentation, restructuring data, and monitoring memory usage, developers can mitigate its impact.

FAQs

1. Why is Redis memory usage not decreasing after deleting keys?

Due to fragmentation, freed memory may not be immediately reusable.

2. How do I check Redis memory fragmentation?

Run INFO memory and check the mem_fragmentation_ratio.

3. Does restarting Redis fix fragmentation?

Yes, but it causes downtime. Consider enabling active defragmentation instead.

4. What is the best way to prevent keyspace fragmentation?

Use uniform data structures and avoid frequent large key deletions.

5. Is active defragmentation always necessary?

Only if your workload involves frequent insertions and deletions leading to fragmentation.