Drupal Performance Optimization (Caching, Varnish)

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_crm_chasseurs_493_0.webp
    CRM development for Chasseurs
    847
  • image_website-sbh_0.png
    Website development for SBH Partners
    999
  • image_website-_0.png
    Website development for Red Pear
    451

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.