Redis Memory Optimization: Eviction Policies and maxmemory Configuration
Fast recall
Quick revision
Core answer
Operational awareness of Redis memory management, eviction policies, and the implications of hitting maxmemory for application behavior.
Not setting maxmemory at all, allowing Redis to consume all available RAM and trigger OOM kills, or using the default noeviction policy with a cache workload.
Mention Redis's memory encoding optimizations (ziplist/listpack for small collections), the OBJECT ENCODING command to inspect encodings, and Redis 7's listpack replacing ziplist.
Interview-ready guide
Interview answer
Memory management is one of the most operationally important aspects of running Redis in production. Redis stores all data in RAM, so running out of memory stops it from accepting new writes — and the eviction policy determines what happens at that point.
Eviction policies — what to evict when memory is full
allkeys-lruevicts the least recently used key across all keys — the right default for a cache where all data has equal priority; Redis automatically removes cold data as memory fillsvolatile-lruevicts the LRU key only among keys with a TTL set — protects keys without TTL from eviction; useful when the same Redis instance holds both cached data (TTL set) and persistent data (no TTL)noevictionrejects new writes with an error when memory is full — appropriate for a session store or queue where losing any data would be catastrophic; requires monitoring to prevent hitting the limitallkeys-lfuevicts the least frequently used key — better than LRU for access patterns where old keys are still popular; Redis 4.0+
Data structure choices affect memory
- A Hash with fewer than 128 fields and small values is stored as a compact ziplist in memory — much more memory-efficient than storing each field as a separate String key;
HSET user:123 name Alice email alice@example.comuses less memory thanSET user:123:name Alice+SET user:123:email alice@example.com - Lists, Sets, and Sorted Sets also use compact encoding below configured size thresholds; stay below
hash-max-listpack-entries(128) to keep ziplist encoding
Key expiry and memory pressure
- Redis expires keys lazily (checked on access) and actively (periodic scan of a sample of expiring keys) — memory isn't immediately reclaimed when a key's TTL passes; under memory pressure, set aggressive expiry policies
- Use
OBJECT ENCODING keyto check a specific key's encoding;MEMORY USAGE keyto see how much memory one key consumes;MEMORY DOCTORfor a high-level diagnosis of memory issues
Redis Memory Optimization: Eviction Policies and maxmemory Configuration
Direct Answer
Redis memory optimization combines the right eviction policy, appropriate data structure choices, key expiry discipline, and maxmemory limits. The eviction policy determines what Redis does when it hits its memory limit — allkeys-lru evicts the least-recently-used key regardless of TTL, while volatile-lru only evicts keys that have a TTL set. For a cache-only Redis, allkeys-lru is the standard choice; for a mixed cache and persistent data store, volatile-lru protects keys without TTLs from eviction.
Code Example
# redis.conf — Memory management
maxmemory 6gb # 70-80% of available RAM
maxmemory-policy allkeys-lru # Evict least recently used
maxmemory-samples 10 # Sample size for LRU approximation (default 5)
# Lazy eviction — free memory in background thread
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
# Compact encoding thresholds
hash-max-listpack-entries 128 # Hash uses listpack below this field count
hash-max-listpack-value 64 # Hash uses listpack below this value size
list-max-listpack-size -2 # ~8 KB per listpack node
zset-max-listpack-entries 128
zset-max-listpack-value 64
set-max-intset-entries 512
Monitor memory usage and eviction rates:
# Check current memory stats
redis-cli INFO memory | grep -E "used_memory_human|maxmemory_human|mem_fragmentation"
# Watch eviction rate in real-time
redis-cli INFO stats | grep evicted_keys
# Inspect encoding of a specific key
redis-cli OBJECT ENCODING user:42
redis-cli MEMORY USAGE user:42
Common Mistakes & Pitfalls
Not setting maxmemory at all, allowing Redis to consume all available RAM and trigger OOM kills, or using the default noeviction policy with a cache workload.
Check your understanding
Answer these without looking back at the article.
- What is the difference between LRU and LFU eviction and when does LFU perform better?
- How does Redis's lazyfree feature prevent eviction from blocking the main thread?
- What internal encoding does Redis use for a small Hash and what size thresholds trigger a switch to a hash table?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.