How to Install and Configure Redis Cache with Drupal and DDEV
Redis cache is a high-performance, in-memory data store that can drastically improve your website’s speed. This guide covers installing Redis on Linux, configuring it for Drupal with the complete settings.redis.php file, and integrating it with DDEV.
Step 1: Install Redis Server on Linux
Install Redis and the PhpRedis extension:
Install dependencies:
bashsudo apt install php-dev php-pear
Install PhpRedis:
bashsudo pecl install redis
Step 2: Configure Redis Settings
Edit /etc/redis/redis.conf (e.g., Ubuntu 18) for optimal caching:
- Disable disk saving: save ""
- Set memory limit: maxmemory 4gb (30% of total cache size)
- Use LRU policy: maxmemory-policy allkeys-lru
- Disable appendfsync: appendfsync no
- Enable I/O threads: io-threads 2 and io-threads-do-reads yes
Step 3: Configure Redis for Drupal
Create a settings.redis.php file with the following code to enable Redis caching:
<?php // #Use for redis cache use Drupal\Core\Installer\InstallerKernel; if (!InstallerKernel::installationAttempted() && extension_loaded('redis') && class_exists('Drupal\redis\ClientFactory')) { // Customize host and port. $settings['redis.connection']['host'] = '127.0.0.1'; $settings['redis.connection']['port'] = 6379; // Customize used interface. $settings['redis.connection']['interface'] = 'PhpRedis'; $settings['cache']['default'] = 'cache.backend.redis'; $settings['cache']['bins']['config'] = 'cache.backend.redis'; $settings['cache']['bins']['discovery'] = 'cache.backend.redis'; $settings['cache']['bins']['bootstrap'] = 'cache.backend.redis'; $settings['cache']['bins']['render'] = 'cache.backend.redis'; $settings['cache']['bins']['page'] = 'cache.backend.redis'; $settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.redis'; // Use compression for cache entries longer than the specified limit. $settings['redis_compress_length'] = 100; $settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml'; $settings['container_yamls'][] = 'modules/contrib/redis/redis.services.yml'; $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', ], ], ]; }
Include this file in settings.php:
if (file_exists(__DIR__ . '/settings.redis.php')) { include __DIR__ . '/settings.redis.php'; }
Step 4: Redis with DDEV
For DDEV, use the Redis add-on:
Install:
bashddev add-on get ddev/ddev-redis ddev restart
Update settings.redis.php for DDEV:
php$settings['redis.connection']['host'] = 'redis';
Learn more at DDEV Redis GitHub.
Why Redis Cache?
Redis reduces database load and speeds up page rendering, making it perfect for Drupal sites, e-commerce, and more.
Call to Action: Set up Redis cache with this guide to optimize your website’s performance! Questions? Drop them below.