Varnish Cache 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.
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 following example demonstrates one method to achieve this:
import dns from "dns/promises";
import https from "https";
import axios from "axios";
/**
* Purges a specific path from all cache nodes behind a hostname.
*
* @param {string} host - The public hostname of the site (e.g. "example.com")
* @param {string} [protocol="https"] - The protocol to use ("http" or "https")
* @param {string} [path="/"] - The path that should be purged (e.g. "/blog/my-post")
*
* @returns {Promise<Array>}
* Returns an array of results for each resolved cache node, including
* HTTP status codes or error messages.
*
* @example
* await purgeHost("www.example.com", "https", "/blog/page");
*/
export async function purgeHost(host, protocol = "https", path = "/") {
const ips = [];
// Resolve IPv4 addresses
try {
ips.push(...await dns.resolve4(host));
} catch {}
// Resolve IPv6 addresses
try {
ips.push(...await dns.resolve6(host));
} catch {}
if (!ips.length) {
throw new Error(`No IPs found for ${host}`);
}
// Send parallel PURGE requests to all resolved IPs
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: {
Host: host,
"User-Agent": "Cache-Purge-Agent",
},
validateStatus: () => true,
};
// Required for HTTPS when using direct IP + hostname routing
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 };
}
})
);
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.