| name | drupal-cache-debugging |
| description | Drupal cache debugging techniques and troubleshooting workflows. Use when asked about X-Drupal-Cache headers interpretation, finding max-age 0 sources, WebProfiler usage, cache hit/miss analysis, stale content debugging, or performance profiling cache-related issues. |
Drupal Cache Debugging
Systematic approaches to identify and resolve caching issues.
When to Use
- Pages not caching for anonymous users
- X-Drupal-Dynamic-Cache shows UNCACHEABLE
- Content is stale after edits
- Need to find source of max-age:0
- Performance profiling cache effectiveness
Debug Headers Setup
$settings['http.response.debug_cacheability_headers'] = TRUE;
Warning: Disable in production - headers can exceed 16KB.
Headers Reference
| Header | Values | Meaning |
|---|
X-Drupal-Cache | HIT/MISS | Page Cache (anonymous) |
X-Drupal-Dynamic-Cache | HIT/MISS/UNCACHEABLE | Dynamic Page Cache |
X-Drupal-Cache-Tags | Space-separated tags | All cache tags |
X-Drupal-Cache-Contexts | Space-separated contexts | Applied contexts |
X-Drupal-Cache-Max-Age | Seconds or -1 | Effective max-age |
Examples
Example 1: Quick Health Check
Input: "How do I quickly check if caching is working?"
Output:
curl -sI https://site.com/ | grep -iE 'x-drupal|cache-control'
curl -sI -H "Cookie: SESSabc=xyz" https://site.com/ | grep X-Drupal-Dynamic-Cache
Example 2: Debug Page Not Caching (Anonymous)
Input: "Anonymous pages always show X-Drupal-Cache: MISS"
Output:
curl -sI https://site.com/ | grep -i set-cookie
drush pm:list | grep page_cache
grep -r "page_cache_kill_switch" web/modules/custom/
grep -r "getSession\|session_start\|\$_SESSION" web/modules/custom/
Example 3: Debug UNCACHEABLE Response
Input: "X-Drupal-Dynamic-Cache shows UNCACHEABLE, how do I fix it?"
Output:
curl -sI https://site.com/ | grep X-Drupal-Cache-Max-Age
Example 4: Debug Stale Content After Edit
Input: "I edited a node but old content still shows"
Output:
drush cr
curl -sI https://site.com/node/1 | grep X-Drupal-Cache
curl -sI https://site.com/node/1 | grep X-Drupal-Cache-Tags
drush cache-tag-invalidate node:1
curl https://site.com/node/1 | grep "expected new content"
drush p:queue-stats
Example 5: Find max-age:0 Source with Temporary Logging
Input: "I can't use Xdebug, how else can I find max-age:0?"
Output:
public static function mergeMaxAges($a, $b) {
if ($a === 0 || $b === 0) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 10);
$simplified = array_map(function($t) {
return ($t['class'] ?? '') . '::' . ($t['function'] ?? '') . ' in ' . ($t['file'] ?? '');
}, $trace);
\Drupal::logger('cache_debug')->warning('max-age:0 merge: @trace', [
'@trace' => implode("\n", $simplified),
]);
}
return min($a, $b);
}
Example 6: Enable Render Debug Comments
Input: "How do I see cache info per render element in HTML?"
Output:
parameters:
twig.config:
debug: true
renderer.config:
debug: true
Example 7: WebProfiler Analysis
Input: "How do I use WebProfiler for cache debugging?"
Output:
composer require drupal/devel
drush en devel webprofiler
Example 8: Database Cache Inspection
Input: "How do I inspect cache entries directly in database?"
Output:
SELECT cid, expire, tags
FROM cache_render
WHERE cid LIKE '%node%'
LIMIT 10;
SELECT cid, created, expire
FROM cache_render
WHERE tags LIKE '%node:123%';
SELECT 'cache_render' as bin, COUNT(*) as entries FROM cache_render
UNION ALL
SELECT 'cache_page', COUNT(*) FROM cache_page
UNION ALL
SELECT 'cache_dynamic_page_cache', COUNT(*) FROM cache_dynamic_page_cache;
Example 9: Drush Cache Commands
Input: "What drush commands help with cache debugging?"
Output:
drush cr
drush cache:clear render
drush cache:clear page
drush cache:clear dynamic_page_cache
drush cache:clear discovery
drush cache-tag-invalidate node:1
drush cache-tag-invalidate "config:system.site"
drush php:eval "print_r(\Drupal::cache('render')->get('entity_view:node:1:full'));"
drush php:eval "print_r(array_keys(\Drupal::getContainer()->getParameter('cache_bins')));"
Debugging Decision Tree
Page not caching?
├── Anonymous user?
│ ├── X-Drupal-Cache: MISS always?
│ │ └── Check for session cookies, kill switch
│ └── X-Drupal-Cache: HIT but stale?
│ └── Check cache tags, invalidation
└── Authenticated user?
├── X-Drupal-Dynamic-Cache: UNCACHEABLE?
│ └── Find max-age:0 source
├── X-Drupal-Dynamic-Cache: MISS always?
│ └── Check if module enabled, cache bin working
└── Dynamic Cache working but slow?
└── Check for missing lazy builders on personalized content
Common Issues Quick Reference
| Symptom | Likely Cause | First Check |
|---|
| Always MISS (anonymous) | Session created | curl -I for Set-Cookie |
| Always UNCACHEABLE | max-age:0 | X-Drupal-Cache-Max-Age header |
| Stale after edit | Missing tags | X-Drupal-Cache-Tags header |
| Per-user cache explosion | user context | X-Drupal-Cache-Contexts header |
| BigPipe not streaming | Server buffering | Check Nginx/Apache config |