Redis Memory Exhaustion Overview
Symptoms
Indicators:
├── Redis memory usage > 90%
├── Evictions increasing
├── Cache hit rate dropping
├── Application slowdown
├── OOM errors
└── Redis restarts
Impact:
├── Cache misses increase
├── Database load spikes
├── Application timeouts
└── Session loss
Detection
# Check Redis memory
redis-cli info memory
redis-cli info stats | grep evicted_keys
# Check memory usage by key
redis-cli --bigkeys
redis-cli --memkeys
# Check memory fragmentation
redis-cli info memory | grep mem_fragmentation_ratio
Common Causes
1. Cache not expired (no TTL)
2. Memory leak
3. Large values stored
4. Too many keys
5. Key proliferation
6. Session accumulation
7. Queue message buildup
8. Configuration issues
Memory Analysis
Analyze Memory Usage
# Get memory statistics
redis-cli info memory
# Sample output:
# used_memory: 1073741824 (1GB)
# used_memory_human: 1.00G
# used_memory_rss: 1200000000 (1.2GB)
# mem_fragmentation_ratio: 1.12
# evicted_keys: 0
# Check memory by database
redis-cli INFO keyspace
# Check specific key memory
redis-cli MEMORY USAGE product_123
Find Large Keys
# Find large keys
redis-cli --bigkeys
# Find keys by pattern
redis-cli KEYS 'product_*' | wc -l
redis-cli KEYS 'session_*' | wc -l
redis-cli KEYS 'cache_*' | wc -l
# Check key expiration
redis-cli TTL product_123
# Check key type
redis-cli TYPE product_123
Identify Patterns
// Analyze cache patterns
$patterns = [
'product' => ['count' => 0, 'memory' => 0],
'session' => ['count' => 0, 'memory' => 0],
'cache' => ['count' => 0, 'memory' => 0],
'queue' => ['count' => 0, 'memory' => 0]
];
$keys = $this->redis->keys('*');
foreach ($keys as $key) {
$pattern = explode('_', $key)[0];
$patterns[$pattern]['count']++;
$patterns[$pattern]['memory'] += $this->redis->memoryUsage($key);
}
// Sort by memory usage
usort($patterns, function($a, $b) {
return $b['memory'] - $a['memory'];
});
Resolution Steps
Immediate Actions
# Flush specific pattern
redis-cli KEYS 'temp_*' | xargs redis-cli DEL
# Set TTL on keys without expiration
redis-cli KEYS 'product_*' | while read key; do
ttl=$(redis-cli TTL $key)
if [ $ttl -eq -1 ]; then
redis-cli EXPIRE $key 3600
fi
done
# Evict least recently used keys
redis-cli CONFIG SET maxmemory-policy allkeys-lru
# Restart Redis (if needed)
systemctl restart redis
Memory Optimization
# Reduce memory usage
redis-cli CONFIG SET maxmemory 2gb
redis-cli CONFIG SET maxmemory-policy allkeys-lru
# Enable compression
redis-cli CONFIG SET hash-max-ziplist-entries 128
redis-cli CONFIG SET hash-max-ziplist-value 64
# Optimize data structures
redis-cli CONFIG SET list-max-ziplist-size -2
redis-cli CONFIG SET set-max-intset-entries 512
Application Optimization
// Optimize cache usage
public function optimizeCache()
{
// 1. Set TTL on all cache entries
$this->cache->save($data, $key, [], 3600);
// 2. Use smaller cache keys
$key = 'p_' . $productId; // Instead of 'product_' . $productId
// 3. Compress large values
$compressed = gzencode(serialize($data));
$this->cache->save($compressed, $key);
// 4. Use Redis hash for related data
$this->redis->hSet('product:' . $id, 'name', $name);
$this->redis->hSet('product:' . $id, 'price', $price);
// Instead of separate keys
}
Prevention Strategies
Monitoring Setup
// Monitor Redis metrics
$metrics = [
'redis_memory_used' => [
'warning' => 80, // percent
'critical' => 90
],
'redis_evicted_keys' => [
'warning' => 100,
'critical' => 1000
],
'redis_connected_clients' => [
'warning' => 100,
'critical' => 200
]
];
// Alert on memory usage
$alerts = [
'redis_memory' => [
'metric' => 'redis_memory_used',
'threshold' => 85,
'duration' => '5m',
'action' => 'notify'
]
];
Best Practices
// 1. Always set TTL
$this->cache->save($data, $key, [], 3600);
// 2. Use appropriate data structures
// Hash for related data
$this->redis->hSet('product:' . $id, 'name', $name);
// Set for unique lists
$this->redis->sAdd('cart:' . $cartId, $productId);
// 3. Monitor memory usage
$memoryUsage = $this->redis->info('memory');
if ($memoryUsage['used_memory_percent'] > 80) {
$this->alert('Redis memory high');
}
// 4. Regular cleanup
public function cleanupExpiredKeys()
{
$keys = $this->redis->keys('*');
foreach ($keys as $key) {
$ttl = $this->redis->ttl($key);
if ($ttl === -1) { // No expiration
$this->redis->expire($key, 3600);
}
}
}
Capacity Planning
// Monitor growth
$growth = $this->getMemoryGrowth();
// Predict when memory will be full
$daysUntilFull = $this->predictMemoryFull($growth);
if ($daysUntilFull < 7) {
$this->alert('Redis memory will be full in ' . $daysUntilFull . ' days');
$this->plan->addTask('Scale Redis memory');
}
Practice Problems
Redis memory hits 95% with 10K evictions/hour and cache hit rate dropping to 60%.
Solution
// Response:
// 1. Analyze: --bigkeys, check TTLs
// 2. Immediate: Set TTL on keys w/o expiry
// 3. Fix: Enable allkeys-lru
// 4. Optimize: Compress large values
// 5. Monitor: Memory, evictions, hit rate
// 6. Prevent: TTL policy, capacity planning Quiz
1. What indicates Redis memory exhaustion?
2. What is the first step when Redis runs out of memory?
3. What is allkeys-lru eviction policy?
4. How to prevent Redis memory issues?
Flashcards
Question
Redis memory exhaustion signs?
Click to reveal answer
Answer
Evictions increasing, cache hit rate dropping
Question
First step on memory exhaustion?
Click to reveal answer
Answer
Check keys without TTL and large keys
Question
allkeys-lru policy?
Click to reveal answer
Answer
Evict least recently used keys when memory full
Question
Prevent Redis memory issues?
Click to reveal answer
Answer
Set TTL, monitor memory, efficient data structures
Question
Find large Redis keys?
Click to reveal answer
Answer
redis-cli --bigkeys
Revision Notes
Key Takeaways
- 1. Signs: Evictions increasing, cache hit rate dropping
- 2. First step: Check keys without TTL and large keys
- 3. Fix: Set TTL, use allkeys-lru, optimize data structures
- 4. Monitor: Memory usage, evictions, hit rate
- 5. Prevent: TTL, monitoring, capacity planning
Interview Tips
- • Explain Redis memory management
- • Discuss eviction policies
- • Know debugging commands
- • Understand prevention strategies
Cheat Sheet
Redis Memory Exhaustion
- Signs: Evictions ↑, hit rate ↓
- First: Check keys w/o TTL, large keys
- Fix: Set TTL, allkeys-lru, optimize
- Monitor: Memory, evictions, hit rate
- Cmds: --bigkeys, MEMORY USAGE, INFO