Configuring Redis Caching with Drupal 8

Introducing Redis

Caching is a frequent topic of discussion when attempting to make Drupal, or any modern web application, fast. Drupal relies on database tables to cache content such as markup by default. While this is certainly better than not caching, the database is a major choke-point that we would like to bypass to speed up the serving of pages.

One of the most recommended options for in-memory caching is memcached:

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Redis, which is an advanced key-value store, can be thought of in this context as a drop-in replacement for memcached.

Is It Time to Ditch Memcached for Redis?

Memcached has served us well, but Redis offers what I see as two big benefits over memcached:

Either one of these could be the killer feature for you, but in my experience, for sites with large amounts of editorial data and not-insignificant caching concerns, the possibility of persisting that cached data is what gets people excited.

To contrast this behavior, if you are using memcached for your cache and you restart it, the data store is going to be empty when it returns. While this can sometimes be convenient when troubleshooting, in the real world this could bring your site to a crawl if it occurs unexpectedly. The ability to restart Redis, and not worry about having an empty cache is worth the price of admission on its own.

Note: It is possible to disable persistence in Redis if you so choose.

Implementing Redis for Drupal

So how do we get this going? Here’s a secret, it’s even easier than setting up memcached.

  1. Install Redis. Redis can be configured on the same box as your web server or on its own. To install Redis on Ubuntu run the following commands:

    sudo apt-get update && sudo apt-get upgrade

    sudo apt-get install software-properties-common

    sudo add-apt-repository ppa:chris-lea/redis-server

    sudo apt-get update && sudo apt-get upgrade

    sudo apt-get install redis-server
  2. Configure Redis. Redis is ready to run and is configured to persist data out of the box, but if you wish to tweak its settings you can start with Linode’s guide for installing and configuring Redis on Ubuntu and other distros.

  3. Install the PhpRedis library. Download and install the Redis Drupal module. You do not need to enable this module.

  4. Configure your Drupal site to use Redis for caching instead of the Drupal cache database tables. In settings.php or settings.local.php add:

/**
* Redis Configuration.
*/

$conf['chq_redis_cache_enabled'] = TRUE;
if (isset($conf['chq_redis_cache_enabled']) && $conf['chq_redis_cache_enabled']) {
$settings['redis.connection']['interface'] = 'PhpRedis';
$settings['cache']['default'] = 'cache.backend.redis';
// Note that unlike memcached, redis persists cache items to disk so we can
// actually store cache_class_cache_form in the default cache.
$conf['cache_class_cache'] = 'Redis_Cache';
}

Let It Fly

Between its powerful features and its simple setup, Redis can be an attractive alternative to memcached for Drupal caching, not only in production, but everywhere. If you are still skeptical, you could even run Redis in parallel with memcached for an existing site. You can switch between the two with just some small changes to your settings file until you are comfortable moving to Redis completely.