Use Redis to speed up your Drupal site

Install PhpRedis extension

pecl install redis

Require Drupal Redis module

composer require drupal/redis

Configure Drupal settings

To get Redis works with your Drupal site, you need to modify your site’s settings.php according to below.

Use redis for container cache

The container cache is used to load the container definition itself, and thus any configuration stored in the container itself is not available yet. These lines force the container cache to use Redis rather than the default SQL cache.

settings.php
// Use redis for container cache.
// The container cache is used to load the container definition itself, and
// thus any configuration stored in the container itself is not available
// yet. These lines force the container cache to use Redis rather than the
// default SQL cache.
$settings['bootstrap_container_definition'] = [
  'parameters' => [],
  'services' => [
    'redis.factory' => [
      'class' => \Drupal\redis\ClientFactory::class,
    ],
    'cache.backend.redis' => [
      'class' => \Drupal\redis\Cache\CacheBackendFactory::class,
      'arguments' => ['@redis.factory', '@cache_tags_provider.container', '@serialization.phpserialize'],
    ],
    'cache.container' => [
      'class' => \Drupal\redis\Cache\PhpRedis::class,
      'factory' => ['@cache.backend.redis', 'get'],
      'arguments' => ['container'],
    ],
    'cache_tags_provider.container' => [
      'class' => \Drupal\redis\Cache\RedisCacheTagsChecksum::class,
      'arguments' => ['@redis.factory'],
    ],
    'serialization.phpserialize' => [
      'class' => \Drupal\Component\Serialization\PhpSerialize::class,
    ],
  ],
];

Add redis services definition

settings.php
// Allow the services to work before the Redis module itself is enabled.
$settings['container_yamls'][] = 'modules/contrib/redis/redis.services.yml';

Either include the default example.services.yml from the module, which will replace all supported backend services (that currently includes the cache tags checksum service and the lock backends, check the file for the current list) or copy the service definitions into a site specific services.yml.

settings.php
// Apply changes to the container configuration to better leverage Redis.
// This includes using Redis for the lock and flood control systems, as well
// as the cache tag checksum. Alternatively, copy the contents of that file
// to your project-specific services.yml file, modify as appropriate, and
// remove this line.
$settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml';

Configure redis connection

settings.php
$settings['redis.connection']['interface'] = 'PhpRedis';
$settings['redis.connection']['host']      = '1.2.3.4';  // Your Redis instance hostname.
$settings['redis.connection']['port']      = '6379';  // Redis port

Port is optional, default is 6379 (default Redis port).

Using a specific database

Per default, Redis ships the database “0”. All default connections will be use this one if nothing is specified.

Depending on your OS or OS distribution, you might have numerous database. To use one in particular, just add to your settings.php file:

settings.php
$settings['redis.connection']['base']      = 12;

Connection to a password protected instance

If you are using a password protected instance, specify the password this way:

settings.php
$settings['redis.connection']['password']  = "mypassword";

Using a wrong auth will make Redis calls silent and creates some PHP warnings, thus Drupal will behave as if it was running with a null cache backend (no cache at all).

Tell Drupal to use the cache backend

settings.php
# Use for all bins otherwise specified.
$settings['cache']['default'] = 'cache.backend.redis';

# Use this to only use it for specific cache bins.
$settings['cache']['bins']['render'] = 'cache.backend.redis';

Tell Drupal to use the queue backend

The redis module provides reliable and non-reliable queue implementations. Depending on which is to be use you need to choose queue.redis or queue.redis_reliable as a service name.

settings.php
# Use for all queues unless otherwise specified for a specific queue.
$settings['queue_default'] = 'queue.redis';

# Or if you want to use reliable queue implementation.
$settings['queue_default'] = 'queue.redis_reliable';


# Use this to only use Redis for a specific queue (aggregator_feeds in this case).
$settings['queue_service_aggregator_feeds'] = 'queue.redis';

# Or if you want to use reliable queue implementation.
$settings['queue_service_aggregator_feeds'] = 'queue.redis_reliable';

Clear SQL cache tables

Once you’ve confirmed that your site is using Redis for caching, you can and should purge any remaining cache data in the MySQL database as it is now just taking up space. TRUNCATE any table that begins with cache_.


Photo by Marc-Olivier Jodoin on Unsplash