Dynamic Page Cache overview

Last updated on
17 May 2017

Drupal 8 provides the Dynamic Page Cache module that is recommended for websites of all sizes. It caches pages minus the personalized parts, and is therefore useful for all users (both anonymous & authenticated).

We can find this core module at: core/modules/dynamic_page_cache.

This feature improves performance because it speeds up the site. Pages requested by users (anonymous or authenticated) are stored the first time they are requested and then reused. Personalized parts are excluded automatically: they are turned into placeholders automatically. Depending on your site configuration and the complexity of particular pages, Dynamic Page Cache may significantly increase the speed of your site, even for authenticated users.

The module requires no configuration. Every part of the page contains metadata that allows Dynamic Page Cache to figure this out on its own. In a nutshell: Dynamic Page Cache respects cache contexts.

Compared to Drupal 7

Drupal 7 did not have Dynamic Page Cache. Nor was it possible to implement something like Dynamic Page Cache. Dynamic Page Cache is possible thanks to advances in Drupal 8's APIs. Everything that is rendered or used by rendering in Drupal 8, provides cacheability metadata. While rendering the individual parts of the page into the final response, cacheability metadata is gathered. It is thanks to this cacheability metadata that Dynamic Page Cache can cache pages, even for authenticated users.

For Drupal 7 there is the Authenticated User Page Caching (Authcache) module. It achieves something similar, but at great cost. It requires a huge amount of work: all code (contrib & custom) must be analyzed to ensure all aspects that cause variations in the output (rendered pages) are taken into account. All of these factors must then be passed to Authcache so that it can calculate its authcache key correctly. The key properties that you specify there are conceptually similar to Drupal 8’s cache contexts. However, it is very easy to miss anything, because it’s you as a site owner that is responsible for identifying all the variations (and corresponding key properties) across all modules across all pages. By default, Authcache assumes the only thing that your site varies by, are a site’s base URL and a user’s roles. So unless your site is very simple (for example not even multilingual), you will need an enormous amount of knowledge/research in order to be able to use Authcache. See Dynamic Page Cache in 8 vs. Authcache in 7 for a more detailed comparison.

Disabling while developing

In example.settings.local.php, this section can be found:

/**
 * Disable Dynamic Page Cache.
 *
 * Note: you should test with Dynamic Page Cache enabled, to ensure the correct
 * cacheability metadata is present (and hence the expected behavior). However,
 * in the early stages of development, you may want to disable it.
 */
# $settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

Uncomment the commented-out line to disable the dynamic page cache. But please do pay attention to the comment!

See also