Fetching content from the database and rendering a page takes time. Caching can speed that up by skipping steps that don't need to be repeated.
Page cache
Once the page is rendered, it can be cached until it changes. The final HTML is stored in the cache_page table. This way, the site can quickly serve the page with a single database call instead of loading and rendering each block and node.
Page cache is a core module - settings can be configured at Configuration > Development > Performance. Your only options are to set the max age (1min - 1 day) and clear the cache.
To verify the cache settings, check the response headers in a private window. Open the page, then refresh it to make sure you would get a cached copy. With a 1 day maximum cache age, you should see cache-control: max-age=86400, public
and x-drupal-cache: HIT
.
Max age tells your browser to keep the cached page until the expired time, and will load a fresh copy after that time.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control…
Dynamic page cache
Logged in users can see dynamic content, such as a view that shows content authored by the user. The view should reload each time to get updated results, but rendered entities and additional blocks can be loaded from the cache.
Dynamic page cache is a core module, that starts working once enabled. The page HTML is built with placeholders for the dynamic content, and stored in cache_dynamic_page_cache. When the page is loaded again, it fetches the cache and populates dynamic content before serving.
https://www.drupal.org/docs/8/core/modules/dynamic-page-cache/overview
https://www.drupal.org/docs/drupal-apis/render-api/auto-placeholdering
Big pipe
Big pipe takes dynamic cache a step further by serving the cached page html and then streaming the dynamic components via ajax.
https://www.drupal.org/docs/8/core/modules/big-pipe/overview
Render cache
Render arrays can be created dynamically, so we can't blindly cache and reuse the output. Cacheability must be declared in the array, or else it will always be cached.
https://www.drupal.org/docs/drupal-apis/render-api/cacheability-of-render-arrays
Cache invalidation
Caching is the easy part, just save the rendered output and serve it again upon request. The hard part is clearing the cache only when necessary. Drupal uses cache keys, contexts and tags to declare cache variations to store, and when to invalidate the cache.
https://www.drupal.org/docs/drupal-apis/cache-api
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api…
CDN purge
A CDN will cache the pages it serves and reaches back to Drupal when necessary. When content changes in Drupal, a cache bust call needs to be sent to the CDN to clear the cache and load a fresh copy.