Skip to main content

Mastering page and block Caching in Drupal

Slow pages lose users. For Drupal sites, smart caching flips that script, turning heavy, database-driven pages into quick, memory-efficient responses. Whether it’s a blog, nonprofit platform, or enterprise CMS, caching is essential to performance.

Picture this: a Drupal 10 site serving dynamic content to thousands daily. Without caching, every page view hits the backend, increasing load times and straining servers. With internal and dynamic caching in place, pages for anonymous and authenticated users are served in milliseconds, not seconds.

In this article, we’ll break down:

These aren’t optional tweaks. They’re foundational for performance, reliability, and scale.

Internal page Cache

Internal Page Cache is Drupal’s way of speeding up pages for anonymous users. When someone visits a page, Drupal saves the fully rendered HTML. If another user visits the same page, Drupal serves that saved version instantly, skipping the usual backend processing.

This reduces load times and server work, especially on high-traffic sites. It’s simple, fast, and only applies to users who aren’t logged in.

How it works

Further Reading:
Check out the official Internal Page Cache documentation for more details.

A practical use case: internal page Cache

The scenario

Imagine you manage a community news site that serves a large volume of anonymous visitors. Frequently, the site displays the same news articles repeatedly. Without caching, each page load requires Drupal to rebuild the entire page dynamically, which increases server load and slows down response times.

The goal

To optimise performance for anonymous users by reducing the processing time required for each page request. The aim is to serve fully rendered pages directly from the cache, thereby minimising database queries and PHP processing cycles.

Considerations

Sample implementation

Although Internal Page Cache is largely managed by Drupal core, you can fine-tune certain configurations in your settings.php:

This configuration ensures that the full HTML page is cached and reused for subsequent requests. For more detailed guidelines, refer to the Internal Page Cache documentation.

Dynamic page Cache

Dynamic Page Cache is designed for handling pages for authenticated users, whose pages often include dynamic or personalised content. It caches page fragments instead of the entire page, making it possible to mix cached and dynamic content seamlessly.

Key characteristics

Further reading:
Visit the Dynamic Page Cache documentation for further details.

A practical use case: dynamic page Cache

The scenario

Consider an e-commerce platform where registered users see personalised information, such as greeting messages, recent order details, or custom promotions. In such cases, parts of the page remain the same across many users, while other parts vary.

The goal

To improve performance for authenticated users by caching as much of the page as possible without compromising personal or dynamic content. By caching static fragments, the platform reduces the load on the server while still delivering personalised content where necessary.

Considerations

Sample implementation

Dynamic Page Cache is enabled in Drupal core, but you can complement it by configuring it in your settings. For example, you might not change its default behavior in settings.php but ensure that your custom modules use cache tags and contexts appropriately when rendering parts of the page.

In your custom module or theme, when rendering a personalized block, you might include cache metadata like this:

This example ensures that while the personalised greeting is rendered dynamically, it still benefits from partial caching of related fragments. For more details, visit the Dynamic Page Cache documentation.

Block Caching strategies

Blocks are critical components of a Drupal site used in sidebars, headers, footers, and more. Proper caching of blocks can reduce server load considerably by ensuring that frequently used content is generated only once.

Cache Settings for Blocks

Drupal offers several caching configurations for blocks:

Further reading:
Learn more about block caching and cache tags on Drupal.org at Cache Tags.

A practical use case: block Caching

The scenario

Imagine you have a custom block on a dashboard that displays quick links and summary statistics. Since different user roles (such as administrators versus regular users) need to see different information, caching must be configured to account for these variations.

The goal

To cache the block efficiently while allowing for role-based customisations. The objective is to minimise repeated block rendering without serving incorrect data to different user types.

Considerations

Sample implementation

Below are two examples. The first uses hook_block_alter() in a custom module to adjust the cache settings of an existing block, and the second shows how to define caching when creating a new block in your theme or module.

Alternatively, when defining a custom block:

These examples ensure that the block content is cached efficiently while still allowing for customization based on user context.

Configuration examples

Below are additional examples that show how to enable and adjust page caching directly from your settings.php:

Enabling and configuring page Caching‍

Performance considerations

Potential pitfalls

Best practices

Debugging and verification

Tools for Caching insights

Sample debugging code‍

Series navigation

This article is part of our comprehensive 10-part series on Drupal caching:

What’s next?

Page and block caching are essential tools in Drupal’s performance playbook. When implemented properly, they can reduce server strain, minimise database queries, and significantly improve how quickly pages are delivered to users.

Internal Page Cache serves pre-rendered HTML to anonymous visitors, allowing Drupal to bypass the full page build process. Dynamic Page Cache complements this by caching reusable parts of a page for logged-in users, even when the content is personalised. Combined with effective block caching, by configuring cache contexts, tags, and expiration times, Drupal can handle high volumes of traffic with far less effort on the backend.

Some sites have seen load times drop by as much as 80% for repeat page views just by using these built-in caching systems. That kind of performance boost improves everything from user experience to SEO.

To apply these strategies effectively:

Always tailor your caching setup to the structure and needs of your site, and reassess it as your content, audience, or features evolve.

We'd love to talk about your business objectives

Written by