NOTE

5.2 Redis Memory Management

Redis memory consumption, fragmentation, memory inspection, eviction, key expiration, and optimization.

Redis / CacheCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Breakdown of Memory Consumption

1.1. used_memory

  • used_memory = Redis itself + object memory + buffer memory

1.1.1. Redis Itself

The Redis process, generally negligible.

1.1.2. Object Memory

  • sizeof(key) + sizeof(value), which takes the largest share.
  • Solutions
    • Use serialization to reduce object size.
    • Encoding optimization.

1.1.3. Buffer Memory

buffer memory = client buffer + replication backlog buffer + AOF buffer

1.2. Memory Fragmentation

  • used_memory_rss - used_memory = memory fragmentation
    • Modern memory allocators use fixed-range memory blocks and round allocations upward.
  • Solution
    • Restarting can reorganize it.

2. How to Inspect Memory Consumption

  • Use info memory to view used_memory_rss, used_memory, and mem_fragmentation_ratio.
    • mem_fragmentation_ratio = used_memory_rss / used_memory
    • If mem_fragmentation_ratio > 1, memory fragmentation exists.
    • If mem_fragmentation_ratio < 1, Linux has swapped Redis to disk.

3. How to Manage Memory

3.1. Memory Eviction Strategy

3.2. Key Expiration

3.3. Memory Optimization

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub