Drupal Performance Optimization (Caching, Varnish)
Drupal has one of the most thoughtful caching systems among CMSs. Cache tags, cache contexts, render cache, dynamic page cache, BigPipe—proper use of these mechanisms gives TTFB < 50 ms even for complex pages.
Drupal Multi-Level Caching
Render cache—individual render arrays (blocks, views, fields) are cached. Invalidated by cache tags.
Dynamic Page Cache—caches pages considering context (logged in/out, language, role). Bypasses rendering but not Drupal bootstrap.
Internal Page Cache—full page cache for anonymous users. Stores HTML on disk/in DB.
External cache (Varnish/Nginx)—cache before Drupal. Full pages without PHP execution.
Internal Page Cache
drush en page_cache -y
Configuration → Performance → Cache pages for anonymous users. Cache time—minimum 10 minutes for content sites.
// Set cache time programmatically
\Drupal::configFactory()
->getEditable('system.performance')
->set('cache.page.max_age', 3600) // 1 hour
->save();
BigPipe: Streaming Rendering
drush en big_pipe -y
BigPipe renders page in two passes: first static parts (fast), then personalized blocks (user sees page sooner). Critical for logged-in users.
Varnish Cache
Varnish works as reverse proxy before Nginx/Apache. Drupal automatically sends headers with cache tags—Varnish caches responses and invalidates needed ones on publish.
apt install varnish
Varnish configuration (/etc/varnish/default.vcl):
vcl 4.1;
import std;
import purge;
backend default {
.host = "127.0.0.1";
.port = "8080"; # Nginx on 8080, Varnish on 80/443
}
sub vcl_recv {
# Don't cache logged in users
if (req.http.Cookie ~ "SESS|SSESS") {
return(pass);
}
# Don't cache POST requests
if (req.method != "GET" && req.method != "HEAD") {
return(pass);
}
# Remove unnecessary cookies
unset req.http.Cookie;
return(hash);
}
sub vcl_backend_response {
# Cache only 200 responses
if (beresp.status != 200) {
set beresp.uncacheable = true;
return(deliver);
}
# TTL from Drupal headers
if (beresp.http.Cache-Control ~ "max-age") {
set beresp.ttl = std.duration(regsub(beresp.http.Cache-Control, ".*max-age=([0-9]+).*", "\1") + "s", 0s);
} else {
set beresp.ttl = 1h;
}
set beresp.grace = 1h;
return(deliver);
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
Drupal + Varnish: Purge Module
composer require drupal/varnish_purger drupal/purge
drush en purge purge_drush purge_ui purge_queuer_coretags purge_processor_cron varnish_purger -y
Varnish Purge sends BAN requests to Varnish on content change. Cache tags auto-generated by Drupal.
Configuration: Configuration → Performance → Varnish Purge → specify Varnish IP and port.
Redis for Drupal Cache
composer require drupal/redis
drush en redis -y
settings.php:
$settings['redis.connection']['host'] = '127.0.0.1';
$settings['redis.connection']['port'] = 6379;
$settings['cache']['default'] = 'cache.backend.redis';
$settings['cache']['bins']['render'] = 'cache.backend.redis';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.redis';
// Keep bootstrap in DB for reliability
$settings['cache']['bins']['bootstrap'] = 'cache.backend.database';
PHP OPcache
opcache.enable=1
opcache.memory_consumption=256
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0
opcache.jit=tracing
opcache.jit_buffer_size=100m
CSS/JS Aggregation
Configuration → Performance:
- Aggregate CSS files: ✓
- Aggregate JavaScript files: ✓
Enable only on production. Disable in development for debugging convenience.
Performance Analysis
# XHProf via drush
composer require drupal/xhprof
drush en xhprof -y
# Profile specific request
drush xhprof:enable && curl https://site.com/heavy-page && drush xhprof:disable
# Slow queries
drush sqlq "SELECT * FROM watchdog WHERE type = 'devel' ORDER BY wid DESC LIMIT 20"
Devel module + Query Count shows SQL query count and execution time per page.
Typical Optimization Results
| Configuration | TTFB | SQL Queries |
|---|---|---|
| No cache | 800–2000 ms | 100–300 |
| Dynamic Page Cache | 50–200 ms | 5–20 |
| Varnish (anonymous) | 1–5 ms | 0 |
| Redis + OPcache | 100–300 ms | 5–15 |
Timeline
Basic optimization (OPcache, Redis, Internal Page Cache, CSS/JS aggregation)—2–3 days. Varnish setup with Purge module and cache tags—plus 2–3 days. Full audit and optimization—5–7 days.







