Drupal's page cache is great while it's populated, but once a page is invalidated (for example when its content changes, or after a cache clear), the next request is blocked until the page builds. On a busy site, or a site which is being crawled aggressively, this can cause a traffic stampede to build multiple pages, which in turn can overwhelm most web servers. This module will continue to serve cached pages after they become "stale" (that is, after the content they show has changed, or after the cache has been cleared), but in the background will rebuild the page, without blocking requests. Background page refreshes happen immediately at the end of the current request, up to a maximum of two (configurable in admin > config > development > performance) concurrent build sessions; if more than this number of stale pages need to be built at the same time, further requests are deferred to a queue which is processed by cron.

The problem

Drupal's internal page cache stores each anonymous page as permanent, together with its cache tags. When a tag the page depends on is invalidated, or the cache is cleared, the next visitor has to wait while Drupal builds the whole page again. Invalidation is frequent on any site with listings. Saving a single node invalidates node_list, which affects every page that lists content, and every deployment that runs drush cr empties the page cache entirely.

What this module does

When a visitor asks for a page that is: At most two pages are rebuilt at once, whether on request or by cron. A stale page that finds both slots busy is added to a queue and rebuilt by cron. A queued page that is valid again by the time cron reaches it, or has been purged since, is skipped. A full cache rebuild (drush cr) no longer deletes cached pages. It marks them stale, so the first visit to each page after a deployment is served instantly and the page is rebuilt behind it. Rarely visited pages benefit most. A stale page is served however old it is, so it stays fast even if nobody has requested it for a long time.

Cache-Control on stale responses

A stale response carries a short Cache-Control: public, max-age=N. N is the smallest of the stale max-age option (60 seconds by default), the max-age the page was stored with, and the site's page cache maximum age (the "Browser and proxy cache maximum age" setting on the Performance page). That setting does not decide when a page becomes stale, only how long a browser or CDN may keep it. Without this, a CDN or reverse proxy in front of the site would keep the stale copy for the full page maximum age. Fresh and rebuilt pages keep the header they always had. A site whose page maximum age is 0, and any page sent as private or no-cache, is left exactly as it was.

Requirements

Settings

All options are optional. They are on the Performance page (admin > config > development > performance), in a "Stale-while-revalidate page cache" section. Any option can also be fixed in settings.php with the key shown below. A value in settings.php always wins, and the Performance page then shows that option read-only, with a note saying why.

Retention and unlimited retention

A stale page is served however old it is, because refusing an old copy only swaps a cheap stale serve for an uncapped build on the spot. How long the cache keeps a page is a separate, housekeeping setting ("Keep pages for"). Every rebuild stores the page again with a fresh lifetime, so a page that is visited keeps being kept, and only a page left alone for the whole period is dropped. On the database backend an expired page may still be served until garbage collection removes it. Choosing "Unlimited" stores pages without a lifetime. It suits archive sites that are rarely visited and want minimal compute: a page nobody has asked for in years is still served instantly, and rebuilt behind the response. The trade-off is that pages nobody visits are never reclaimed, so crawler junk and query-string variants accumulate in a database-backed cache. Bound the cache some other way. Your cache backend may also evict pages when it runs out of memory, whatever this setting says. With Redis and an allkeys-lru policy, the stale pages nobody is reading are the first to go.

When the module does nothing

The module is bypassed while the page cache maximum age ("Browser and proxy cache maximum age" on the Performance page) is set to <no caching> (0). That is how development and staging environments switch caching off, so the module stays out of the way there: it behaves exactly like core, no stale page is served, nothing is rebuilt in the background, and clearing caches deletes cached pages. A configuration override in settings.php counts too. Drupal's own default for that setting is 0, so a freshly installed site is bypassed until a maximum age is set. The Status report has a "Stale page cache" entry that says whether the module is Active, Bypassed (maximum age off) or Switched off, and the Performance page shows the same notice at the top of the module's section.

Where pages are stored

A stale page can only be served if the store still has it. A database table keeps pages until they are deleted. Redis, Memcache and APCu are memory-bound: when they fill up they discard the entries nobody has read recently, which is exactly what a stale page is. Retention is therefore a promise only a persistent store can keep. The module inspects the page cache backend actually in use and reports it at the top of its section on the Performance page and on the Status report (admin > reports > status). For Redis it reads the memory limit, eviction policy, evicted key count and hit rate from the server. If those settings cannot be read, the storage is treated as at risk rather than as safe. The Status report entry is informational for a store that can evict, and a warning if "Unlimited" is chosen as the retention on a store that is known to evict. A database-backed page cache is reported as fine.

Things to know

Known limitations

Monitoring

How it works

Tests

The module ships with unit and kernel tests. The kernel tests need SIMPLETEST_DB and check the service alterations against a compiled container and a real database cache bin, including that a cache flush leaves pages stale rather than deleting them.