How Does Redis Memory Eviction Work?
Redis
Memory Management
Eviction Policies
Performance
What is Memory Eviction?
Redis memory eviction automatically removes data when memory usage hits its limit - much like a library clearing out old books to make room for new ones. This keeps the service running smoothly and prevents out-of-memory errors.
Core Functions
- Prevent Memory Overflow: Sets memory limits via
maxmemory
(unlimited by default) - Maintain Write Capability: Prevents the system from entering a read-only state
- Smart Data Selection: Keeps the most valuable data based on usage patterns
8 Eviction Strategies Explained
Strategy Reference Table (Production Selection Guide)
Strategy Name | Scope | Algorithm | Use Cases | Version |
---|---|---|---|---|
noeviction | None | Reject writes | Data loss intolerant | 2.0+ |
allkeys-lru | All keys | Approx. LRU | General caching (recommended) | 2.8+ |
volatile-lru | Expiring keys | Approx. LRU | Mixed persistence | 2.8+ |
allkeys-random | All keys | Random selection | Uniform access | 2.0+ |
volatile-random | Expiring keys | Random selection | Temporary storage | 2.0+ |
volatile-ttl | Expiring keys | TTL ordering | Time-sensitive data | 2.0+ |
allkeys-lfu | All keys | Approx. LFU | Hot spot data | 4.0+ |
volatile-lfu | Expiring keys | Approx. LFU | Hot temporary data | 4.0+ |
Core Algorithm Implementation
1. Approximate LRU Algorithm
Interview Focus: Understanding Redis's probabilistic sampling for efficient LRU
// Redis source snippet (evict.c)
#define EVICTION_SAMPLES_ARRAY_SIZE 16
unsigned long estimateObjectIdleTime(robj *o) {
return LRU_CLOCK() - o->lru;
}
void evictionPoolPopulate(dict *sampledict, struct evictionPoolEntry *pool) {
// Sample 5 keys into candidate pool
for (int i = 0; i < EVICTION_SAMPLES_ARRAY_SIZE; i++) {
// Select key with maximum idle time
if (bestkey->lru > pool[EVPOOL_SIZE-1].idle) {
evictionPoolPopulateItem(pool, bestkey);
}
}
}
Algorithm Optimization:
- Default sample size: 5 (configurable)
- Fixed-size candidate pool (16 entries)
- Evicts least recently used key from pool
Additional Notes:
- Redis combines global LRU clock with probabilistic sampling to avoid full key scan
- Interview focus: Understanding candidate pool mechanism and its performance benefits
2. Approximate LFU Algorithm
Implementation Details:
- Uses Morris counter with probabilistic decay
- Logarithmic frequency storage (0-255)
- Decay factor configuration (lfu-decay-time)
# Check key's LFU stats
redis-cli object freq key1
Additional Notes:
- Morris counter uses logarithmic growth to prevent counter saturation
- Interview focus: How LFU better captures hot spot data compared to traditional counters
Memory Defragmentation
Trigger Conditions and Configuration:
# Enable active defragmentation
activedefrag yes
# Ignore small fragmentation areas (avoid unnecessary defrag)
active-defrag-ignore-bytes 100mb
# Trigger when used_memory_rss significantly exceeds used_memory
active-defrag-threshold-lower 10
Defragmentation Process: Here's how Redis manages memory fragmentation:
- Fragment Detection: Periodically scans memory to evaluate used_memory_rss vs used_memory ratio
- Candidate Selection: Identifies high-fragmentation areas for data reorganization
- Data Reorganization: Copies valid data to contiguous new memory space
- Memory Reclamation: Gradually releases old, fragmented memory blocks
- Progressive Execution: Performs defragmentation incrementally to prevent blocking
Production Configuration Guide
E-commerce Flash Sale Example
# Memory limit (set to 3/4 of physical memory)
maxmemory 12gb
# Use LFU to retain hot product data
maxmemory-policy allkeys-lfu
# Adjust LFU decay period (1 hour)
lfu-log-factor 10
lfu-decay-time 3600
# Enable defragmentation
activedefrag yes
Monitoring Metrics
# Memory metrics
redis-cli info memory | grep -E "used_memory|maxmemory|mem_fragmentation_ratio"
# Eviction stats
redis-cli info stats | grep evicted_keys
# Defrag status
redis-cli info memory | grep defrag
Common Issues and Solutions
1. Memory Not Released
Symptom: Reached maxmemory but eviction not triggered
Troubleshooting Steps:
# Verify policy isn't noeviction
CONFIG GET maxmemory-policy
# Check key expiration (-1 means no expiry)
TTL mykey
# Analyze memory distribution
redis-cli --bigkeys
2. LFU Hot Spot Failure
Symptom: Hot data getting unexpectedly evicted
Troubleshooting Steps:
# Increase counter precision (lower value = higher precision)
lfu-log-factor 10
# Accelerate frequency decay (minutes)
lfu-decay-time 1800
3. High Memory Fragmentation
Symptom: System refuses new writes despite low memory usage
Troubleshooting Steps:
# Manual emergency defrag
redis-cli memory purge
# Adjust defrag intensity
CONFIG SET active-defrag-cycle-min 5
CONFIG SET active-defrag-cycle-max 10
Performance Tuning Tips
- Capacity Planning: Keep memory usage under 70%
- Key Design: Keep objects under 1MB
- Version Upgrade: Use Redis 4.0+ for better eviction control
- Hybrid Storage: Move cold data to disk storage
Related Resources: