Oct 19, 2017

Drupal 8 and Redis, part 3: clearing the external cache

Alexey Doronin
Founder, developer, designer

In the first two articles of this series we looked at how to make Nginx serve pages directly from Redis, which helps to dramatically speed up your website and reduce load on the web server. This article will show you how to clear Redis Page Cache. Actually, the way we are going to do it is practically the same as any standard method of invalidating external caches, like a CDN cache or the Varnish cache.

  1. Drupal 8 and Redis, part 1: NGINX serve content directly out of a Redis cache
  2. Drupal 8 and Redis, part 2: a review of the module and NGINX configuration
  3. Drupal 8 and Redis, part 3: clearing the external cache

So, we configured NGINX, then installed and enabled our experimental Redis Page Cache module which saves plain versions of web pages to Redis.

To clear the cache we will need these Drupal modules:

We enable all these modules and also Purge UI.

The method in a nutshell

We have our page cache in Redis — we name it Redis Page Cache. This is essentially an external cache, as was pointed out in the previous articles. We can clear it using a rather common approach: HTTP requests with specified parameters to clear (or invalidate) cache.

To do that, we will use Generic HTTP Purger. It sends HTTP requests to an external service, telling it to invalidate certain data.

But records in Redis Page Cache don't have any tags or cache expiration time. How can we track changes and send requests to purge specific web pages?

By using URLs queuer! This module creates a table with URLs and corresponding Drupal cache tags, then forms a queue for purging based on this table. In other words, it is an intermediary between web page addresses and Drupal cache tags.

The module trains its traffic registry by collecting URLs from requests that miss cache inside Drupal's own page cache. These URLs are stored along with their associated cache tags inside this same traffic registry. Now when Drupal invalidates a set of tags because someone edited a page, these tags are used to fetch URLs from the registry and are added to the Purge queue.

That's exactly what we need.

Configuring Generic HTTP Purger

The Purge settings are located at /admin/config/development/performance/purge 

Here we press Add purger and choose HTTP Purger:

Then we specify the following settings:

1. Name: any name will do 

2. Type: Url

3. Hostname: your project's domain

4. Port

5. Path: /rpcache/rpcache-clear

6. Request Method: POST

7. Scheme: the protocol

Now we go to the Headers tab:

Here we must specify headers and their values as shown in the picture above.

X-RPCache-Type — [invalidation:type]
X-RPCache-Url — [invalidation:expression]

So, we did a purger which sends POST requests to http://yourdomain.com/rpcache/rpcache-clear. The request headers will have information about which URL to delete from cache.

You might have guessed that Redis Page Cache has an endpoint with the address /rpcache/rpcache-clear, It receives the requests and purges specified web pages from Redis.

Configuring URLs queuer

Configuring this module correctly is very important. Other modules either work or not, but this module, when not configured properly, can cause problems with clearing the external cache in Redis.

That's why you have to read the description of this module very carefully. 

The page /admin/config/development/performance/purge, has the following settings in QUEUE tab:

They must correspond to the settings for Redis Page Cache at /admin/config/development/rpcache

We have to check and edit the list of URLs that shouldn't be processed by the URLs queuer. These web pages will not populate the traffic registry that monitors caching. 

They also must be excluded in the cache settings at /admin/config/development/rpcache, otherwise the mechanism that purges them won't work.

Clearing the cache

As the Drupal cache is getting updated, we see elements from /admin/config/development/performance/purge queued for purging:

You can clear the external cache by using the drush p-queue-work command, it is shown in the same column on the top. It will start the purger that we configured before, which will send commands to clear the external cache.

This is how it works: let's say we edited a web page and Drupal invalidated it's cached version. URLs queuer matched its tags and the address of the web page, then added it to the queue for external cache purging.   

In conclusion, let's compare this mechanism to Varnish:

 Redis Page CacheVarnish
Configuration simplicity+/-
We need a dynamic Nginx module and several Drupal modules
We need to configure Varnish, and also need additional software and SSL settings
Performance +-
Clearing the cache+/-
Through Purge. Supports tags. The drawback is the dependance on URLs queuer and its limitations
Through Purge. Supports tags.
Easy to debug+-