Introduction to Caching in Drupal.

A global picture

Luis Ruiz Peidró


Targets

Targets

To understand the importance of caching in Drupal.

To understand the basic processes and concepts.

To have a global overview of the possible cache layers that can be found in a Drupal project.

Introduction

Definition

It is a data storage layer that stores the results of operations so that when they are required again, there is no need to redo them.

Why perform an operation more than once if the result is the same?

Cache

Benefits

  • Increase data availability (reduce the page load time...).
  • Resource savings (it is not need to rebuild the page...).
  • Help to earn money (improving user experience...).
  • Help to save money: (infrastructure costs can be reduced...).

Why is so important in Drupal

Cache

PHP and Drupal

PHP

Op Cache
  • OPcache extensions.
  • Store compiled PHP script in shared memory.
  • It is essential for any Drupal site to improve performance.
Op Cache
  • It is used to cache data in memory.

Drupal Cache System

Op Cache
  • Cache backend.
  • Cache bin.
  • Cached Object Dependencies.
  • Cached Invalidation.
  • Interaction with the Cache API.
  • Cache Dependency Propagation.
  • Response caching (Internal Page Cache, Dynamic Page Cache).

Cache backend

Definition

  • Manage cached data (storing, retrieving, invalidating).
  • Intermediary layer between the data source and the one requesting the data.
  • The main feature: how and where it stores the data (database, memory...).

Types (examples)

  • Database Backend: This is the default Cache Backend. It uses the Drupal database.
  • Chained Fast Backend: Two cache layers: in memory and in database.
  • Memory Backend: Stores cached data in memory using a PHP array (context unit tests).

Cache bin

  • Act like boxes in the Drupal cache system.
  • Instead of having all cached data in one place. For example in a database table, we can store them in different compartments.
  • Cache Bins are defined by what type of data they store.

Benefits

  • Having things more organized.
  • If we need to empty the caches, we do not need to delete all cached objects.
  • Having the ability to use different backends for each need.

Cache bin defined in core.services.yml

  cache.config:
  class: Drupal\Core\Cache\CacheBackendInterface
  tags:
    - { name: cache.bin, default_backend: cache.backend.chainedfast }
  factory: ['@cache_factory', 'get']
  arguments: [config]
  cache.entity:
  class: Drupal\Core\Cache\CacheBackendInterface
  tags:
    - { name: cache.bin }
  factory: ['@cache_factory', 'get']
  arguments: [entity]

Cache bin examples

  • cache.bootstrap: stores necessary information for Drupal's execution that varies very little.
  • cache.config: configuration and configuration entities.
  • cache.entity: Stores the values of existing content entities in Drupal.
  • cache.render: contains cached HTML strings like cached blocks, which can grow to large sizes.
Op Cache

Cache objects dependencies

Invalidation

  • Cache tags:establishes a dependency on the sources.
  • Max-Age: establishes a temporary dependency, which is an expiration date for the cache object.

Variations

  • Cache context: establishes a dependency on the context, creating variations based on the context (role, url.query, language).

Cache metadata examples

Cache tags

  • node:5
  • user:3
  • node_list
  • node_list:article
  • config:system.performance

Cache contexts

  • user.roles
  • theme
  • languages
  • url

Cache invalidation

Flush caches: remove cached objects directly, for example, using the command “drush cr”.

Cache invalidation: the process where cached data is invalidated because it has become outdated, with the aim of regenerating it with updated data:

  • Cache Max Age: sets an expiration date for the cached object. Once this date is passed, the system understands the object is obsolete.
  • Cache tags: set tags to associate cached objects with the source. This way, when the source is modified, such as an article, it identifies which cached objects need to be invalidated.

Interaction with the Cache API

Directly

Interacting directly with the Cache API of Drupal, taking care of caching objects, retrieval, and invalidation ourselves.

  
   $cid = 'my_module_example:' . \Drupal::languageManager()
    ->getCurrentLanguage()->getId();
   $data = NULL;
   if ($cache = \Drupal::cache()->get($cid)) {
     $data = $cache->data;
   }
   else {
     $data = my_module_complicated_calculation();
     \Drupal::cache()->set($cid, $data);
   }
  

Interaction with the Cache API

Indirectly

Indirectly through the Render API. This second system is the most common.

  
  $renderer = \Drupal::service('renderer');
  $config = \Drupal::config('system.site');
  $current_user = \Drupal::currentUser();
  $build = [
    '#markup' => t('Hi, %name, welcome back to @site!', [
      '%name' => $current_user->getUsername(),
      '@site' => $config->get('name'),
    ]),
    '#cache' => [
      'contexts' => [
        'user',
      ],
    ],
  ];

  $renderer->addCacheableDependency($build, $config);
  $user_entity = \Drupal\user\Entity\User::load($current_user->id());
  $renderer->addCacheableDependency($build, $user_entity);
  

Cache Dependency Propagation

Op Cache

The dependencies defined in the different components are propagated to their ancestors for the invalidation to be effective.

Response caching

Internal Page Cache

  • Anonymous user.
  • Static pages.
  • Cache context and max age does not work.
  • Small and medium sites.

Dynamic Page Cache

  • Anonymous user and logged users.
  • Enable in any case.
  • Dynamic pages.

How does Dynamic Page Cache work?

Op Cache

It caches response objects with dynamic components by replacing those components with placeholders.

It waits until the last moment to render those components and replace the placeholders.

External Caching System

In memory cache

In memory cache

Buy default, Drupal stores the cache in the database.

Memcached and Redis stores cache in server memory.

  • Faster cache retrieval.
  • Significantly reduced drupal database requests.
  • Memory management.

Caching reverse proxy

Caching reverse proxy
  • Improved performance and speed.
  • Efficient resource utilization: reduce the requests to the origin server.
  • Improves website security.

CDN (Content Delivery Network)

CDN
  • Improving website load times.
  • Reducing bandwidth costs.
  • Increasing content availability and redundancy.
  • Improving website security.

Complex architecture

CDN

Cache invalidation in complex architecture

Max Age (TTL). The easiest way:

  • It can be Communicated to the different caches in the headers of the response.
  • The cache system recreate, or ask the source if it has to be recreated.
  • There are some delays between the update of the source, and the recreation of the cache.

Cache tags:

  • Send the cache tags in the response headers.
  • Drupal has to inform about invalidations.

URLs:

  • Drupal requests the cache to remove the cached objects with a specific URL.

Module Purge

In memory cache

purge_purger_httptc, purge_queuer_url, acquia_purge, akamai, cloudflare, cloudfront_purger, fastlypurger, keycdn, varnish_purge, max_cdn_cache, Nginx_cache_clear, purge_file

Response headers

Why are response headers useful:

  • To send configuration about caching to other systems, for example, Browser, CDN, etc. (Max Age, Cache tags).
  • For us, to debug.
In memory cache

Types of headers:

  • Standard HTTP headers.
  • Not standard HTTP headers prefixed with “X-” or another prefix, like “cf-”, added by Drupal and other cache layers.
Headers

Drupal headers

Standar Drupal headers:

  • X-Drupal-Dynamic-Cache: Indicates if the response object is cached by the Internal Dynamic Page Cache.
  • X-Drupal-Cache: This parameter only shows when the Internal Page Cache is active and for anonymous users.

States:

  • HIT
  • MISS
  • UNCACHEABLE

Debugging Drupal headers

services.yml => "http.response.debug_cacheability_headers" => TRUE
Headers

Cache Review

Headers

Summary

Summary

Headers

Conclusion

A good implementation of caching is essential in any Drupal project and involves both the development and infrastructure teams

Questions

Gracias a los sponsor PLATINO

Gracias a los sponsor GOLD, SILVER y BRONZE

¡Gracias!