---
title: "Varnish cache management"
date: "2025-11-28T12:24:12+00:00"
summary: "Optimize Drupal performance with Varnish cache management. Learn caching strategies, purging techniques, and bypass methods for faster sites."
image:
type: "page"
url: "/acquia-cloud-platform/add-ons/node-js/varnish-cache-management"
id: "a68bc04c-8ba1-44d1-92fc-3d7316ac5284"
---

[Varnish Cache](https://varnish-cache.org/) is a caching reverse proxy installed in front of all Cloud Platform load balancing infrastructure. To improve application performance, Varnish caches connections from anonymous users and serves responses from memory. This reduces the need to forward requests to the Apache web infrastructure. For more details, visit [Introduction to Varnish](/acquia-cloud-platform/help/92646-introduction-varnish "Introduction to Varnish").

Varnish caches responses to anonymous user requests. It also caches static assets such as images, JavaScript, and CSS for both anonymous and authenticated users.

Clear varnish cache for specific endpoints
------------------------------------------

When you use ISR or similar cache revalidation on a site served by multiple load-balanced caching nodes, a single `PURGE` request sent to the public hostname may not sufficiently clear the cache. Each cache node maintains its own version of the cached content, and a purge request may only affect one node. To ensure immediate content updates, perform cache purging across all load balancers behind the hostname. Use correct host routing and secure handling when HTTPS is enabled. This method ensures all cache nodes clear their content, which prevents stale pages and ensures all users see the latest version without inconsistencies.

The customer can adapt the following code snippet to clear the varnish cache for specific endpoints.

How this implementation works:

*   **Create a dedicated route**: Expose a secure route within the application. For example, `POST /api/clear-varnish-cache`.
*   **Embed the logic**: Within the route handler, instantiate and execute the following code snippet.
*   **Trigger externally**: After deploying to the development environment, the team can invoke this endpoint using Postman, a curl command, or another HTTP client to clear the cache.

Important security recommendation

Since this endpoint will be accessible over the internet, it must be properly secured. Consider requiring a custom API key or bearer token in the request headers so that only authorized users or systems can trigger cache-clearing operations.

    const dns = require("dns").promises;
    const https = require("https");
    const axios = require("axios");
    /**
     * Purges a specific path from all cache nodes behind a hostname.
     *
     * @param {string} site_name - The Acquia site name (for example, "myappdev" or "myappprod").
     * @param {string} host - The hostname to purge (for example, "myappdev2.com" or "myappdev2.prod.acquia-sites.com").
     * @param {string} [protocol="https"] - The protocol to use, typically "http" or "https".
     * @param {string} [path="/"] - The path to purge (for example, "/blog/my-post").
     * @returns {Promise<Array<{ip: string, status?: number, error?: string}>>} Per-node purge results.
     * @throws {Error} Thrown when the hostname does not resolve to any IPv4 or IPv6 addresses.
     *
     * @example
     * await purgeHost("myappdev2", "www.example.com", "https", "/blog/page");
     */
    async function purgeHost(site_name, host, protocol = "https", path = "/") {
      const ips = [];
      // Resolve IPv4 addresses
      const ACQUIA_DEFAULT_FQDN = `${site_name}.prod.acquia-sites.com`
      try {
        ips.push(...await dns.resolve4(ACQUIA_DEFAULT_FQDN));
      } catch { }
      // Resolve IPv6 addresses
      try {
        ips.push(...await dns.resolve6(ACQUIA_DEFAULT_FQDN));
      } catch { }
      if (!ips.length) {
        throw new Error(`No IPs found for ${ACQUIA_DEFAULT_FQDN}`);
      }
      const results = await Promise.all(
        ips.map(async (ip) => {
          const targetHost = ip.includes(":") ? `[${ip}]` : ip;
          const url = `${protocol}://${targetHost}${path}`;
          const config = {
            method: "PURGE",
            url,
            timeout: 5000,
            headers: {
              'X-Acquia-Purge': site_name,
              'User-Agent': 'Cache-Purge-Agent',
              'Host': host,
            },
            validateStatus: () => true,
          };
          if (protocol === "https") {
            config.httpsAgent = new https.Agent({
              servername: host,
              rejectUnauthorized: false,
            });
          }
          try {
            const res = await axios(config);
            return { ip, status: res.status };
          } catch (err) {
            return { ip, error: err.message };
          }
        })
      );
      console.log(results)
      return results;
    }

Bypass varnish caching
----------------------

On Front End Hosting – Advanced, the Varnish cache layer can be bypassed automatically based on certain cookie patterns:

*   Cookies starting with `SESS` or `SSESS`
*   Cookies starting with `PERSISTENT_LOGIN_`
*   A cookie with the exact key `NO_CACHE`

Authenticated users who send any of these cookies bypass Varnish and connect directly to the application. This provides access to draft or unpublished content for logged-in users, while maintaining strong caching for anonymous users.