Platform.sh User Documentation

Using Redis with Drupal

Upsun Beta

Access our newest offering - Upsun!

Get your free trial by clicking the link below.

Get your Upsun free trial

Redis is a fast open-source in-memory database and cache, useful for application-level caching. For more information on this service, see the dedicated Redis page or the official Redis documentation.

Follow the instructions on this page to do one of the following:

  • Add and configure Redis for Drupal if you have deployed Drupal manually.
  • Fine-tune your existing configuration if you have deployed Drupal using a Platform.sh template.

Before you begin Anchor to this heading

You need:

You also need a settings.platformsh.php file from which you can manage the configuration of the Redis service. If you installed Drupal with a template, this file is already present in your project.

Add a Redis service Anchor to this heading

1. Configure the service Anchor to this heading

To define the service, use the redis-persistent type:

.platform/services.yaml
# The name of the service container. Must be unique within a project.
<SERVICE_NAME>:
    type: redis-persistent:<VERSION>

Note that persistent Redis requires disk to store data. For more information, refer to the dedicated Redis page.

If want to use ephemeral Redis instead, use the redis type:

.platform/services.yaml
# The name of the service container. Must be unique within a project.
<SERVICE_NAME>:
    type: redis:<VERSION>

Note that changing the name of the service replaces it with a brand new service and all existing data is lost. Back up your data before changing the service.

2. Add the relationship Anchor to this heading

To define the relationship, use the redis endpoint :

.platform.app.yaml
# Relationships enable access from this app to a given service.
relationships:
    <RELATIONSHIP_NAME>: "<SERVICE_NAME>:redis"

You can define <SERVICE_NAME> and <RELATIONSHIP_NAME> as you like, but it’s best if they’re distinct. With this definition, the application container now has access to the service via the relationship <RELATIONSHIP_NAME>.

For PHP, enable the extension for the service:

.platform.app.yaml
# PHP extensions.
runtime:
    extensions:
        - redis

Example Configuration Anchor to this heading

Service definition Anchor to this heading

.platform/services.yaml
# The name of the service container. Must be unique within a project.
cacheredis:
    type: redis-persistent:7.0

App configuration Anchor to this heading

.platform.app.yaml
# Relationships enable access from this app to a given service.
relationships:
    rediscache: "cacheredis:redis"

3. Add the Drupal module Anchor to this heading

To add the Redis module to your project, run the following command:

composer require drupal/redis

Then commit the resulting changes to your composer.json and composer.lock files.

Configure your Redis service Anchor to this heading

To configure your Redis service, follow these steps:

  1. Add the following code at the top of your settings.platformsh.php file:

    settings.platformsh.php
     <?php
    
     $platformsh = new \Platformsh\ConfigReader\Config();
     if (!$platformsh->inRuntime()) {
        return;
     }
  2. Add the following code at the end of the file:

    settings.platformsh.php
    <?php
    
    // Enable Redis caching.
     if ($platformsh->hasRelationship('rediscache') && !InstallerKernel::installationAttempted() && extension_loaded('redis')) {
       $redis = $platformsh->credentials('rediscache');
    
       // Set Redis as the default backend for any cache bin not otherwise specified.
       $settings['cache']['default'] = 'cache.backend.redis';
       $settings['redis.connection']['host'] = $redis['host'];
       $settings['redis.connection']['port'] = $redis['port'];
    
       // You can leverage Redis by using it for the lock and flood control systems
       // and the cache tag checksum.
       // To do so, apply the following changes to the container configuration.
       // Alternatively, copy the contents of the modules/contrib/redis/example.services.yml file
       // to your project-specific services.yml file.
       // Modify the contents to fit your needs and remove the following line.
       $settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml';
    
       // Allow the services to work before the Redis module itself is enabled.
       $settings['container_yamls'][] = 'modules/contrib/redis/redis.services.yml';
    
       // To use Redis for container cache, add the classloader path manually.
       $class_loader->addPsr4('Drupal\\redis\\', 'modules/contrib/redis/src');
    
       // Use Redis for container cache.
       // The container cache is used to load the container definition itself.
       // This means that any configuration stored in the container isn't available
       // until the container definition is fully loaded.
       // To ensure that the container cache uses Redis rather than the
       // default SQL cache, add the following lines.
       $settings['bootstrap_container_definition'] = [
         'parameters' => [],
         'services' => [
           'redis.factory' => [
             'class' => 'Drupal\redis\ClientFactory',
           ],
           'cache.backend.redis' => [
             'class' => 'Drupal\redis\Cache\CacheBackendFactory',
             'arguments' => ['@redis.factory', '@cache_tags_provider.container', '@serialization.phpserialize'],
           ],
           'cache.container' => [
             'class' => '\Drupal\redis\Cache\PhpRedis',
             'factory' => ['@cache.backend.redis', 'get'],
             'arguments' => ['container'],
           ],
           'cache_tags_provider.container' => [
             'class' => 'Drupal\redis\Cache\RedisCacheTagsChecksum',
             'arguments' => ['@redis.factory'],
           ],
           'serialization.phpserialize' => [
             'class' => 'Drupal\Component\Serialization\PhpSerialize',
           ],
         ],
       ];
     }

    You can customize your configuration further using the inline comments from this example configuration. For more information on possible configuration options, see the README.txt file delivered with the Redis module or the official Redis documentation.

Verify Redis is running Anchor to this heading

To verify that Redis is running, run the following command:

platform ssh 'echo $PLATFORM_RELATIONSHIPS | base64 --decode | json_pp'

In the output, retrieve the value of the host property for your Redis relationship.

Then, run the following command:

platform ssh -- redis-cli -h HOST info

The output produces information and statistics about Redis, showing that the service is up and running.

Is this page helpful?