Cache Hit Ratio
What is Cache Hit Ratio?
Hit Ratio = Cache Hits / (Cache Hits + Cache Misses) * 100
Target: > 90% for production
Measuring Hit Ratio
// Redis cache hit ratio
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$info = $redis->info();
$hits = $info['keyspace_hits'];
$misses = $info['keyspace_misses'];
$ratio = $hits / ($hits + $misses) * 100;
// Output: Hit ratio: 95.5%
Monitoring Tools
# Redis stats
redis-cli info stats | grep -E 'keyspace_hits|keyspace_misses'
# Check specific database
redis-cli -n 0 info keyspace
redis-cli -n 1 info keyspace # FPC
Cache Performance Metrics
-- Monitor cache operations
SELECT
cache_type,
COUNT(*) as operations,
AVG(execution_time) as avg_time
FROM cache_log
WHERE created_at > DATE_SUB(NOW(), INTERVAL 1 HOUR)
GROUP BY cache_type;
Cache Status Monitoring
Cache Status Command
php bin/magento cache:status
+------------------+----------+
| Cache Type | Status |
+------------------+----------+
| config | Enabled |
| layout | Enabled |
| block_html | Enabled |
| collections | Enabled |
| fpc | Enabled |
+------------------+----------+
Redis Cache Monitoring
# Total keys per database
redis-cli -n 0 dbsize # Default cache
redis-cli -n 1 dbsize # FPC
redis-cli -n 2 dbsize # Sessions
# Memory usage
redis-cli info memory
# Cache size breakdown
redis-cli --bigkeys
Cache Status API
$cacheManager = $objectManager->get(\Magento\Framework\App\Cache\Manager::class);
$types = $cacheManager->getAvailableTypes();
foreach ($types as $type => $status) {
echo "$type: " . ($status ? 'Enabled' : 'Disabled') . PHP_EOL;
}
Cache Debugging Tools
Debug Mode
// Enable cache debugging in env.php
return [
'cache' => [
'frontend' => [
'default' => [
'backend_options' => [
'log_level' => 3 // Verbose logging
]
]
]
]
];
Cache Header Debug
// Add cache debug headers
public function render($resultPage)
{
$response = $this->getResponse();
$response->setHeader('X-Cache-Debug', '1');
$response->setHeader('X-Magento-Cache-Debug', 'HIT');
return $resultPage;
}
Varnish Debug
# Enable Varnish debug
varnishadm param.set debug +esi
# Check cache status
curl -I http://magento-url/
# Look for: X-Varnish: 200 (hit) or 500 (miss)
Debug Commands
# Check cache configuration
php bin/magento config:show | grep cache
# Show cache types
php bin/magento cache:status
# Clear specific cache
php bin/magento cache:clean block_html
# Monitor cache in real-time
watch -n 1 'redis-cli -n 0 info keyspace'
Performance Impact Analysis
Page Load Time with Cache
// Measure page load time
$start = microtime(true);
// ... page render ...
$elapsed = microtime(true) - $start;
// With FPC: 50-100ms
// Without FPC: 500-2000ms
Cache Impact by Type
| Cache Type | Impact Without | Impact With |
|---|---|---|
| config | +200-500ms | 0 |
| layout | +50-100ms | 0 |
| block_html | +100-300ms | 0 |
| fpc | +500-1500ms | 0 |
| Total | +850-2400ms | ~50ms |
Debugging Slow Pages
// Profile cache operations
$start = microtime(true);
$data = $cache->load($key);
$loadTime = microtime(true) - $start;
if ($loadTime > 0.01) { // > 10ms
$logger->warning('Slow cache load: ' . $key . ' took ' . $loadTime . 's');
}
Cache Optimization Checklist
1. Verify all cache types enabled
2. Check Redis memory isn't swapping
3. Monitor cache hit ratio > 90%
4. Check Varnish hit rate
5. Review cache TTL settings
6. Optimize block cache keys
7. Clean stale cache entries
Practice Problems
A page loads slowly despite FPC being enabled. Investigate cache performance.
Quiz
1. What is a healthy cache hit ratio?
2. How do you check Redis cache size?
3. What header shows Varnish cache status?
4. What is the performance impact of disabling FPC?
Flashcards
Question
What is a healthy cache hit ratio?
Click to reveal answer
Answer
> 90% of requests served from cache
Question
How to check Redis cache keys?
Click to reveal answer
Answer
redis-cli dbsize
Question
What header shows Varnish status?
Click to reveal answer
Answer
X-Varnish header (200=hit, 500=miss)
Question
What is the FPC performance impact?
Click to reveal answer
Answer
Reduces page load by 500-1500ms
Question
How to debug cache in real-time?
Click to reveal answer
Answer
watch -n 1 'redis-cli info keyspace'
Revision Notes
Key Takeaways
- 1. Cache hit ratio should be > 90% for optimal performance
- 2. Monitor with redis-cli info stats and dbsize commands
- 3. FPC provides 500-1500ms improvement per page load
- 4. Use X-Varnish header to debug cache hit/miss
- 5. Profile cache load times for slow operations
- 6. Check Redis memory and connection pool health
Interview Tips
- • Explain how to measure and improve cache hit ratio
- • Describe debugging tools for cache issues
- • Discuss the performance impact of different cache types
- • Know how to monitor Redis cache health
Cheat Sheet
Cache Debugging Cheat Sheet
Hit ratio:
redis-cli info stats | grep keyspace
Target: > 90%
Cache size:
redis-cli dbsize
redis-cli -n 1 dbsize # FPC
Debug headers:
X-Varnish: 200 (hit), 500 (miss)
X-Cache-Debug: 1
Performance impact:
- FPC: -500-1500ms
- Config: -200-500ms
- block_html: -100-300ms
Commands:
cache:status
cache:clean type
redis-cli info memory