Memory Optimization
Redis memory is your primary bill and failure mode — maxmemory triggers eviction or OOM.
Introduction
Redis memory is your primary bill and failure mode — maxmemory triggers eviction or OOM. Right-sizing types (hash not JSON blob), eviction policy choice, and active defrag can cut ElastiCache costs 20–30% without code rewrites.
Run redis-cli --bigkeys and MEMORY DOCTOR monthly. Fragmentation ratio above 1.5 means RSS exceeds logical memory — activedefrag or restart during maintenance.
Every key without TTL is a future incident.
Understanding the topic
Key concepts
- used_memory vs used_memory_rss — fragmentation.
- maxmemory + maxmemory-policy.
- allkeys-lru vs volatile-lru vs noeviction.
- Encodings — intset, listpack, embstr savings.
- activedefrag — online defragmentation.
- MEMORY USAGE key — bytes per key.
Step-by-step explanation
- Allocator (jemalloc) tracks logical usage.
- Updates cause holes — RSS grows.
- maxmemory hit triggers eviction algorithm.
- noeviction returns errors on write.
- Defrag relocates keys compacting memory.
Syntax reference
Common commands
- --bigkeys off-peak only.
- noeviction for queue workloads needing no silent loss.
- allkeys-lru when most keys should be evictable.
INFO memoryMEMORY DOCTORMEMORY USAGE session:abcredis-cli --bigkeys -i 0.1CONFIG SET maxmemory 4gbCONFIG SET maxmemory-policy allkeys-lru
Informative example
Find top memory consumers and compare hash vs JSON string:
redis-cli --bigkeys# Sample output shows biggest string/hash/zsetMEMORY USAGE user:42:string_jsonMEMORY USAGE user:42:hash_fields# Hash often smaller when multiple fields updated
Migrate hot JSON keys to hash field-by-field in background job. Monitor used_memory drop.
Real-world use
Real-world use cases
- Reduce AWS ElastiCache bill.
- Prevent OOM before product launch.
- Choose HLL over SET for UV.
- Eviction tuning for pure cache tier.
- Fragmentation fix after big delete.
Best practices
- Set maxmemory 75–80% RAM on self-hosted.
- TTL every ephemeral key.
- allkeys-lru for cache; noeviction for job queues.
- Enable activedefrag when supported.
- Right type and encoding awareness.
- Alert at 80% used_memory.
Common mistakes
- noeviction + no maxmemory on cache cluster.
- Giant keys never split or trimmed.
- Ignoring fragmentation until OOM kill.
- volatile-lru but keys lack TTL — never evict.
Advanced interview questions
Q1Beginnermaxmemory purpose?
Q2Beginnerallkeys-lru vs volatile-lru?
Q3IntermediateMemory fragmentation?
Q4IntermediateReduce session memory?
Q5AdvancedEviction policy for job queue?
Summary
Memory = cost and reliability boundary.