The requirement is to cache content for as long as possible, but update promptly when content changes are made in Drupal.
Note that in this setup I’m using Acquia Edge powered by Cloudflare, but the strategy would be similar for Acquia Edge powered by Akamai.
The objective is to bootstrap Drupal as infrequently as possible, by relying on the Varnish and CDN layers, but always to serve fresh content: Cache pages in Varnish for a long time, but provide a method (Purge) to invalidate Varnish when a content change is made in Drupal. At the CDN layer, the goal is to make sure the current version of a page is delivered to the browser, by checking Varnish frequently to see if a new version is available. This check is a lightweight call to Varnish. If Varnish does not have a new version, the CDN-cached version is delivered.
- Disable Internal Page Cache (page_cache) module.
- Enable Internal Dynamic Page Cache (dynamic_page_cache) module.
- Enable Acquia Purge (acquia_purge) module (which requires Purge module)
- Ignore the recommendation to set a cache max-age of a year.
- Install HTTP Cache Control (http_cache_control) module.
Drupal Performance Settings:
- At Configuration >> Performance
- Set Browser cache maximum age (max-age) to zero, or 1 minute.
- Set Shared cache max age (s-max-age) to one month (or something fairly long, such as a day or a week).
- Note: http_cache_control requires custom code or a patch to set max-age longer than 1 day, as the maintainer does not recommend extremely long max-age values. See this issue: https://www.drupal.org/project/http_cache_control/issues/3173020
- Set a Page Rule in Cloudflare, with:
- Browser Cache TTL: Respect existing headers
- Edge Cache TTL: 1 minute (or an acceptable update time, such as 5 minutes)
- Cache Level: Cache Everything
- Bypass Cache on Cookie:
- Set additional page rules to override for any special cases such as admin paths, etc., as needed.
- Varnish performs the function that page_cache is intended to serve, so page_cache is not needed, and should be turned off to reduce server resource utilization.
- The dynamic_page_cache module caches certain non-personalized parts of the site for both anonymous and authenticated users, so this should be enabled.
- Acquia Purge sends cache invalidation requests to the Varnish layer when content changes are made in Drupal, prompting Varnish to load the new version, even if the s-max-age time hasn’t been reached yet.
- The http_cache_control module provides a setting for s-max-age, which Varnish and CDN will respect. In combination with Purge, this allows pages to be cached in Varnish for a long time, but updated in Varnish with a new version when Purge sends an invalidation request based on cache tags.
- There is no method to force invalidation of a site visitor’s local browser cache, so instead we set the browser cache max age very low. The max-age header is respected by local browsers. Setting this to zero or very short prevents a user from seeing an old version of the page from local cache.
- At the CDN layer, we override the s-max-age value by specifying a short Edge Cache TTL. This causes the CDN to check Varnish frequently for a new version of the page, so that changes in Drupal pushed to Varnish via cache invalidation are reflected promptly in the CDN, as well.
- The Bypass Cache on Cookie setting bypasses caching for Drupal authenticated visitors.