Implementing Redis on Magento 2

Magento 2 comes with native Redis support, so it is not necessary to install any supporting dependencies like on Magento 1. You just need to add the respective configuration to the Magento ./app/etc/env.php

Cache and full page cache storage

There are three elements to add for Redis; cache, full page cache and sessions.

Magento 2 versions lower than 2.3.4

Add the following after the opening array statement,

    'cache' => [
        'frontend' => [
            'default' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' =>
                    [
                        'server' => 'redis11.i',
                        'database' => '0',
                        'port' => '6379',
                        'compress_data' => '1',
                        'compression_lib' => 'gzip'
                    ],
            ],
            'page_cache' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' =>
                    [
                        'server' => 'redis31.i',
                        'port' => '6380',
                        'database' => '0',
                        'compress_data' => '1',
                        'compression_lib' => 'gzip'
                    ]
            ]
        ]
    ],

Magento 2.3.5+ and greater:

A valid id_prefix is required on these Magento versions, this is normally set on install but may not be present on legacy installs.

Magento does attempt to automatically calculate a value if this is not set, however, it uses a path that differs between the CLI and Web.

There are 2 methods of doing this:

Automatically defined ID_PREFIX

Place the following below the <?php opening tag in app/etc/env.php:

$SON_PREFIX=substr(\hash('sha256', dirname(str_replace("/microcloud","",__DIR__),7)), 0, 3).'_';

You can then implement the cache/page_cache directives that use this variable:

    'cache' => [
        'frontend' => [
            'default' => [
                'id_prefix' => "$SON_PREFIX",
                'backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
                'backend_options' =>
                    [
                        'server' => 'redis11.i',
                        'database' => '0',
                        'port' => '6379',
                        'compress_data' => '1',
                        'compression_lib' => 'gzip'
                    ],
            ],
            'page_cache' => [
                'id_prefix' => "$SON_PREFIX",
                'backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
                'backend_options' =>
                    [
                        'server' => 'redis31.i',
                        'port' => '6380',
                        'database' => '0',
                        'compress_data' => '1',
                        'compression_lib' => 'gzip'
                    ]
            ]
        ]
    ],
Manually defined ID_PREFIX

! Please be ensure that you change the <ID_PREFIX> variable to a 3 character string i.e. 061_**

    'cache' => [
        'frontend' => [
            'default' => [
                'id_prefix' => '<ID_PREFIX>_',
                'backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
                'backend_options' =>
                    [
                        'server' => 'redis11.i',
                        'database' => '0',
                        'port' => '6379',
                        'compress_data' => '1',
                        'compression_lib' => 'gzip'
                    ],
            ],
            'page_cache' => [
                'id_prefix' => '<ID_PREFIX>_',
                'backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
                'backend_options' =>
                    [
                        'server' => 'redis31.i',
                        'port' => '6380',
                        'database' => '0',
                        'compress_data' => '1',
                        'compression_lib' => 'gzip'
                    ]
            ]
        ]
    ],

Session storage

Replace the following,

    'session' => [
        'save' => 'files',
    ],

With,

    'session' => [
        'save' => 'redis',
        'redis' => [
            'host' => 'redis1.i',
            'port' => '6379',
            'password' => '',
            'timeout' => '2.5',
            'persistent_identifier' => '',
            'database' => '0',
            'compression_threshold' => '2048',
            'compression_library' => 'gzip',
            'log_level' => '1',
            'max_concurrency' => '6',
            'break_after_frontend' => '5',
            'break_after_adminhtml' => '30',
            'first_lifetime' => '600',
            'bot_first_lifetime' => '60',
            'bot_lifetime' => '7200',
            'disable_locking' => '1',
            'min_lifetime' => '60',
            'max_lifetime' => '2592000'
        ]
    ],