Memory OptimizationAdvanced
1Question 1 of 1
bashHow do you optimize memory consumption in Redis production deployments?
Explanation & Concept
Techniques for optimizing Redis memory footprint: 1. Choose compact encodings: Use small Hashes instead of plain string keys where possible (Redis uses memory-efficient `ziplist` / `listpack`). 2. Set explicit `maxmemory` and appropriate eviction policies (`volatile-lru`). 3. Set TTLs on all temporary or cache keys. 4. Shorten key names (e.g. `u:101:s` instead of `user_session_tracker:101:session_data`). 5. Disable or tune active defragmentation (`activedefrag yes`).
Code Example
bash
# Check memory fragmentation and memory usage metrics
INFO memory
# Key indicators:
# used_memory_human: 1.45G
# used_memory_peak_human: 2.10G
# mem_fragmentation_ratio: 1.12 (Ideal is between 1.0 and 1.5)
# Inspect memory consumption of an individual key
MEMORY USAGE user:101:profileHow It Works & Key Takeaways
A fragmentation ratio significantly above 1.5 indicates memory wasted by the OS allocator. Enabling `activedefrag yes` cleans up fragmented allocation gaps online.