Magento 2 Redis

Magento 2 Redis

Magento 2 Redis Tutorial, How to Install and Configure for Default Cache, Page Cache and Sessions Storage.

Redis is an optional backend cache provided that allow storage in memory of cached parts, compared to the default Magento cache that stores cache parts in files on disk.

Install #

Magento 2 Redis Extensions comes pre-installed as part of the Magento 2 package so there is no need to do a install but to take advantage of it you have to Configure Magento 2 to use Redis.

Configuration #

Redis configuration for a Magento 2 store is stored inside /app/etc/env.php .

Redis can be used in 2 ways in a Magento 2 store:

With Redis #

Running Magento 2 with Redis enabled will increase speed as Redis is a fast way to cache different areas of the Magento application.

Without Redis #

Magento 2 can run with Redis disabled. Below is a example of a /app/etc/env.php without Redis:

Cache #

Default Cache #

Below command can be executed in a shell console to enable Redis as default cache storage:

$ bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-db=0
  • replace 127.0.0.1 with the Redis server IP

If you don’t have Redis running Magento will show a error message, as on executing this command it also trys to connect to the Redis server instance.

Connection to Redis 127.0.0.1:6379 failed after 2 failures.Last Error : (111) Connection refused

If you have Redis server running then the message will be:

We saved default values for these options: db-engine, db-model, db-init-statements.

The Redis default cache command adds these lines to your /app/etc/env.php, under ‘cache’ => ‘frontend’:

...
    'default' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => '127.0.0.1',
                    'database' => '0',
                    'port' => '6379'
                ],
            ],
...

Redis Page Caching #

To enable Redis Page caching the following command can be executed:

$ bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db=1
  • replace 127.0.0.1 with the Redis server IP

The returned message is :

You saved the new configuration.

The Page Caching command adds these lines to your /app/etc/env.php, under ‘cache’ => ‘frontend’:

        'page_cache' => [
                    'backend' => 'Cm_Cache_Backend_Redis',
                    'backend_options' => [
                        'server' => '127.0.0.1',
                        'port' => '6379',
                        'database' => '1',
                        'compress_data' => '0'
                    ]
                ]
...

Session Storage #

Redis an be used to store the Sessions of your customers. This can be enabled in 2 ways:

Session Cache Command Line #

Command line can be used to enable Redis:

$ bin/magento setup:config:set --session-save=redis --session-save-redis-host=127.0.0.1 --session-save-redis-log-level=3 --session-save-redis-db=2

This will show if successful:

You saved the new configuration.

Session Cache Environment File #

To store Magento Sessions in Redis you can use command:

$ bin/magento setup:config:set --session-save=redis --session-save-redis-host=127.0.0.1 --session-save-redis-log-level=3 --session-save-redis-db=2

This will show if successful:

You saved the new configuration.

Session Cache Manually Set #

The file /app/etc/env.php can be manually edited and the following code added to Enable Redis Session Storage:

...
'session' =>
    array (
        'save' => 'redis',
        'redis' =>
        array (
            'host' => '127.0.0.1',
            'port' => '6379',
            'password' => '',
            'timeout' => '2.5',
            'persistent_identifier' => '',
            'database' => '2',
            'compression_threshold' => '2048',
            'compression_library' => 'gzip',
            'log_level' => '3',
            'max_concurrency' => '6',
            'break_after_frontend' => '5',
            'break_after_adminhtml' => '30',
            'first_lifetime' => '600',
            'bot_first_lifetime' => '60',
            'bot_lifetime' => '7200',
            'disable_locking' => '0',
            'min_lifetime' => '60',
            'max_lifetime' => '2592000'
        )
    ),
...

Where :

  • 127.0.0.1 => Replace with your Hosting’s Redis IP
  • 6379 => Replace with your Hosting’s Redis Port

Full Page Cache #

In the file located at /app/etc/env.php Full Page Cache can be enabled for your Magento 2 store using below env file as model.

...
    'cache' => [
        'frontend' => [
            'default' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => '127.0.0.1',
                    'database' => '0',
                    'port' => '6379'
                ]
            ],
            'page_cache' => [
                'backend' => 'Cm_Cache_Backend_Redis',
                'backend_options' => [
                    'server' => '127.0.0.1',
                    'port' => '6379',
                    'database' => '1',
                    'compress_data' => '0'
                ]
            ]
        ]
    ],
    'cache_types' => [
        'config' => 1,
        'layout' => 1,
        'block_html' => 0,
        'collections' => 1,
        'reflection' => 1,
        'db_ddl' => 1,
        'compiled_config' => 1,
        'eav' => 1,
        'customer_notification' => 1,
        'config_integration' => 1,
        'config_integration_api' => 1,
        'full_page' => 1,
        'config_webservice' => 1,
        'translate' => 1,
        'vertex' => 1
    ],
...

Where:

  • 127.0.0.1 => Replace with your Hosting’s Redis IP
  • 6379 => Replace with your Hosting’s Redis Port
  • full_page => 1 - Enabled Full Page Caching

Published: Mar 10, 2019. Updated: September 10, 2020.


Magento 2 Cache

Magento 2 Cache tutorial on different usage scenarios and why Cache should be enabled on your store.

Read
Magento 2 Google Analytics

Magento 2 Google Analytics

Magento 2 Google Analytics setup for a smooth integration between tracking in GA and your store.

Read

Magento 2 Extension

Magento 2 Extension tutorial, learn what is a Magento 2 Extension how to install or disable in your Magento store.

Read
Magento 2 Varnish

Magento 2 Varnish

Magento 2 Varnish. What is Varnish and how it can be used and configured in a Magento 2 store.

Read
Magento 2 Export

Magento 2 Export

Magento 2 Export tutorial and how to for Products Export, Categories Export, Customers Export or Tax Rates Export.

Read

Magento 2 Mode

The Magento 2 store can run in different modes, depending on the environment, that can increase speed load.

Read
Contact
Hire us


Need help with Magento 2 Redis ?


Tech

Technologies we work with

Magento is an open-source e-commerce platform written in PHP
PHP is a popular general-purpose scripting language that is especially suited to web development.
MySQL is an open-source relational database management system.
Redis is an in-memory data structure project implementing a distributed, in-memory key-value database with optional durability.
Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as APIs.
Google Tag Manager is a free tool that allows you manage and deploy marketing tags on your website / app
Google Analytics is a web analytics service offered by Google that tracks and reports website traffic
Elasticsearch is a search engine based on the Lucene library.