How to Setup Redis Cache for Magento 2

How to Setup Redis Cache for Magento 2

Redis is a popular key-value storage database, cache, and message broker. It helps keep stable performance for high traffic Magento stores.

Redis is used for PHP session storage. The backend supports tag-based cache cleanup without foreach loops.

Redis cache indexes tags in files. So the tag operations don’t have to do a full scan of each cache file.

Redis also supports on-disk save and master/slave replication. You get compression libraries like gzip and snappy.

Redis is useful for multi-server hosting. It allows a fast backend cache that does not require a low-level file system cache.

Best Magento Hosting now

Before Enabling Magento Redis Cache

  • Backup the server:

Ensure that you backup your Magento store and server settings. It allows for quick data restoration if there are any mishaps in the process.

  • Use a staging site for testing:

You should test the site updates on the staging site first. You can shift the updates to the live site if the updates are working.

It ensures you don’t damage the live site while changing the cache.

Magento Redis Cache Requirements

There are some prerequisites to set up Magento 2 Redis Cache on the hosting server:

  • Redis Server
  • PHP Redis Extension

Configure Redis Default Caching

Run the setup:config: set command. You can specify parameters of Redis default caching.

bin/magento setup:config: set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend- redis-db=0

The --cache-backend=redis command enables the Redis default caching. You can skip this parameter if it is already enabled.

--cache-backend-redis-<parameter_name>=<parameter_value> is the key/value pairs. These key/value pairs configure the default caching. The list includes:

Command Line Parameter Parameter Description Default Value
cache-backend-redis-server server Indicates fully qualified hostname.

Also, the IP address, or an absolute path to a UNIX socket.
127.0.0.1

The default value of server 127.0.0.1 specifies that Redis is installed on the Magento server.
cache-backend-redis-port port It is Redis server listen port 6379
(server 127.0.0.1, port 6379)
cache-backend-redis-db database It is required if you use Redis for both full-page and default caching.

Here you specify the database number for one of the caches. The other caching option uses 0 by default.

Note: The database numbers have to be different if you use different types of caching.

You can assign the default caching db number to 0 and the page caching db number to 1.
Assign the session cache storage db number to 2.
0
(database 0. port 6379)
cache-backend-redis-password password Setting up a Redis password enables its security feature: the auth command.

You have to configure the password in the Redis caching configuration file /etc/redis/redis.conf.

Example Command

The example command enables Redis default caching. You can set the host to 127.0.0.1 and assign the database number to 0.

bin/magento setup:config: set --cache-backend=redis --cache-backend-redis-server=127.0.0.1 --cache-backend-redis-db =0

Configure Redis Page Caching

Run the setup:config:set command with other parameters.

bin/magento setup:config:set --page-cache= redis --page -cache-redis-<parameter_name>=<parameter_value>...

The --page-cache=redis command will enable Redis page caching.

--page -cache-redis-<parameter_name>=<parameter_value> is the parameter/value pairs that set up page caching. The list includes:

  • Cache-backend-redis-server
  • Cache-backend-redis-port
  • Cache-backend -redis-db
  • Cache-backend-redis-password

Example Command

The example command is used to enable Redis page caching. It sets the host to 127.0.0.1 and assigns the database number to 1.

bin/magento setup:config:set --page-cache=redis --page-cache-redis-server=127.0.0.1 --page-cache-redis-db =1

Results

The two example commands of Redis CLI result in Magento adding lines that are similar to <Magento install dir>app/etc/env.php

'cache' => [
'frontend' => [
        'default' => [
            'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
            'backend_options' => [
              'server' => '127.0.0.1',
                'database' => '0',
                'port' => '6379'
            ],
       ],
        'page_cache' => [
            'backend' => 'Magento\\Framework\\Cache\\Backend\\Redis',
            'backend_options' => [
                'server' => '127.0.0.1',
                'port' => '6379',
                'database' => '1',
                'compress_data' => '0'
            ]
        ]
    ]
],

Configure Redis for Session Storage

You can configure Redis session storage with Magento’s command line options. Run the setup:config:set command and specify the parameters for Redis.

bin/magento setup:config:set --session-save=redis --session-save-redis-<parameter_name>=<parameter_value>...

The --session-save=redis is used to enable Redis session storage.

--session-save-redis-<parameter_name>=<parameter_value> is a list of parameter/value pairs. Some of the parameters to configure session storage include:

Command Line Parameter Parameter Description or Meaning Default Value
session-save-redis-host host Hostname, IP address, or the absolute path if using UNIX sockets. localhost
session-save-redis-port port Specifies Redis server listen port 6379
session-save-redis-password password Specifies a password. Add it if your Redis server requires an auth password. empty
session-save-redis-timeout timeout Specify the connection timeout, in seconds. 2.5
session-save-redis-persistent-id persistent_identifier Configure a unique string to enable persistent connections.

For example: sess-db0
session-save-redis-db database Configure unique Redis database number.
A unique number for each db prevents data loss.

Note: The database numbers should be different for Redis caching types.

Assign the default caching db number to 0. The page caches db to 1, and the session storage db number to 2
0
session-save-redis-compression-threshold compression_ threshold Set to 0 to disable the compression threshold.

It is recommended when suhosin.session.encrypt = On
2048
session-save-redis-compression-lib compression_library Set in the following options: gzip, lzf, lz4 or snappy. gzip
session-save-redis-log-level Configure any of the following:

0 for emergency and most severe errors.
1 for alerting and immediate action
2: critical when an application component is not available.
3 for error: It includes runtime errors. Not critical requires monitoring. 4 for warning: To get additional information.

5 for notice: Normal but essential condition) 6 for info: To get essential messages.
7 for debug: Information for development or testing.
1

Conclusion

Using Redis for Magento will allow your store to run faster. It offers horizontal and vertical scaling. Redis cache is sharable on multiple servers and supports cache tag.

Redis also allows many databases for the Magento cache. It includes a full-page cache and sessions. You don’t have to start processes listening on different ports.

Redis Cache is a popular choice to keep stable and high performance for your store.

Learn more on Magento backend on the MGT-Commerce tutorials.

Nikita S.
Nikita S.
Lead Technical Writer

As a lead technical writer, Nikita S. is experienced in crafting well-researched articles that simplify complex information and promote technical communication. She has expertise in cloud computing and holds a specialization in SEO and digital marketing.


Get the fastest Magento Hosting! Get Started