How to Configure Redis on Magento 2 Using Docker Compose

Likes (1)   Dislikes (0)  

This is an extracted or additional part of the Magento 2 Docker Setup for Development article. Thank you my fellow developers for giving me a good response and reading my article for your use case. I am so much inspired and willing to write this additional article that features Redis Cache Integration in your Magento 2 Docker setup.

I will be using redis:6.2-alpine as cache service container on my Magento v2.4.5 docker setup. Below is the docker compose file that contains the cache service which is a redis image. The other services are omitted as those are available in the article Magento 2 Docker Setup for Development.

Cache Service Container using Redis
version: "3"
services:
  web:
    ...
  mysql:
    ...
  elasticsearch:
    ...
  cache:
    image: redis:6.2-alpine
    container_name: m245redis
    command: redis-server --save 60 1 --loglevel warning
    ports:
      - 6379:6379
    volumes:
      - "./data/magento245-cache:/data"
...

Explanation of Cache/Redis service block

Some background on Redis Server

Redis as you know is an in-memory database. Hence once the service is stopped all data will be lost. This is the generic case with Redis not specific with Docker. If we compare any RDBMS engines with Redis, this is the main difference Redis stores data in-memory (RAM) whereas other database engines store the data in filesystem. This makes Redis so fast for read and write operations. And that is the main reason why this is used for temporary or variable data storage such as caching.

Docker Specific Redis Configuration

But in case of Magento caching we don’t want to lose the cached data very often. We want to lose cached data at desired time. But Redis as we learned is an in-memory database. That will not work out of the box. We have to do some persistent storage configuration in Redis itself. The command option in the docker-compose configuration does exactly that. It instructs Redis to store the in-memory data in to the persistent volume. The data directory in the unix root is the default storage file system path.

command: redis-server --save 60 1 --loglevel warning

The command section exactly says “persist in-memory data to persistent storage location in every 60 seconds gap if at least 1 write operation has been made during that period. Also sets the log level as per desired”.

volumes:
      - "./data/magento245-cache:/data"

Then using Docker volume mapping we mapped the data directory to our host machine path at some desired location. So whenever we will re-run our docker compose setup will not lose those cached data.

Read more about Redis Persistence system at http://redis.io/topics/persistence.

Let’s Configure Magento to Use Redis for Caching

Manually configuring the cache section in the env.php file. I have configured both default & page caching. Consider reading Magento documentation for Redis Cache Configuration for a configuration that best suits your requirement.

app/etc/env.php cache configuration
'cache' => [
    'frontend' => [
        'default' => [
            'id_prefix' => 'f53_',
            'backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
            'backend_options' => [
                'server' => 'cache',
                'database' => '0',
                'port' => '6379',
                'password' => '',
                'compress_data' => '1',
                'compression_lib' => '',
                'preload_keys' => [
                    'f53_EAV_ENTITY_TYPES',
                    'f53_GLOBAL_PLUGIN_LIST',
                    'f53_DB_IS_UP_TO_DATE',
                    'f53_SYSTEM_DEFAULT',
                ],
            ],
        ],
        'page_cache' => [
            'id_prefix' => 'f53_',
            'backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
            'backend_options' => [
                'server' => 'cache',
                'port' => '6379',
                'database' => '1',
                'compress_data' => '0'
            ]
        ]
    ],
    'allow_parallel_generation' => false
],

You may need to change your id prefix if you are configuring preload keys. Either rebuild your containers or just docker compose up to catch the new service container and prepare it.

Leave a Comment

Share via
Copy link
Powered by Social Snap