How to Implement Redis in Drupal 8?

| | 2 min read

For one of my project, I have implemented the Redis module in Drupal 8. I have used the following configurations as follows:

  • First I download Redis module for a Drupal 8 environment and stored it in 'modules/contrib' directory and enabled the same.
  • For the working of Redis module we need its libraries, so first you have to download the phpredis php extension library from https://github.com/nicolasff/phpredis. Extract the entire contents of the archive into the 'libraries/' folder.

For the working of Redis in local server we have add php extension to our local server. For this follow the steps for seting up Redis Server

  • After that update the settings.php file with the following code:

    $settings['redis.connection']['interface'] = 'PhpRedis';
        // Host ip address.
        $settings['redis.connection']['host']      = '127.0.0.1';
        $settings['cache']['default'] = 'cache.backend.redis';
        // Redis port.
        $settings['redis.connection']['port']      = '6379';
        $settings['redis.connection']['base']      = 12;
        // Password of redis updated in php.ini file.
        $settings['redis.connection']['password'] = "password";
        $settings['cache']['bins']['bootstrap'] = 'cache.backend.chainedfast';
        $settings['cache']['bins']['discovery'] = 'cache.backend.chainedfast';
        $settings['cache']['bins']['config'] = 'cache.backend.chainedfast';
    
  • Create services.yml and add the below code in it.

    services:
      # Cache tag checksum backend. Used by redis and most other cache backend
      # to deal with cache tag invalidations.
      cache_tags.invalidator.checksum:
       class: Drupal\redis\Cache\RedisCacheTagsChecksum
       arguments: ['@redis.factory']
       tags:
         - { name: cache_tags_invalidator }
    
      # Replaces the default lock backend with a redis implementation.
      lock:
        class: Drupal\Core\Lock\LockBackendInterface
        factory: ['@redis.lock.factory', get]
    
      # Replaces the default persistent lock backend with a redis implementation.
      lock.persistent:
        class: Drupal\Core\Lock\LockBackendInterface
        factory: ['@redis.lock.factory', get]
        arguments: [true]
    
      # Replaces the default flood backend with a redis implementation.
      flood:
        class: Drupal\Core\Flood\FloodInterface
        factory: ['@redis.flood.factory', get]
    

Hope this is useful.