Reading Time: 4 minutes

Redis is a caching method that can increase the speed of your store’s backend and frontend.

This article will explain how to set up Redis in your Magento 2 store and how to work with redis-cli.

How to Configure Redis Cache for Magento 2

There are two ways to configure Redis Cache for Magento 2. You can run a command that automatically updates env.php with the correct details or you can manually edit the env.php file .

Configure Redis Cache for Magento 2 via the command line

You can use the following command to enable Redis backend caching:

cd /data/web/magento2
bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server= --cache-backend-redis-db=0

Configure Redis Cache for Magento 2 by editing the env.php file

To enable caching in Redis, extend your /data/web/magento2/app/etc/env.php with the following snippet. Add it to your cache keys. (Without the cache key in the snippet)

'cache' => array(

   'frontend' => array(

       'default' => array(

           'backend'         => 'Cm_Cache_Backend_Redis',

           'backend_options' => array(

               'server' => '',

               'port'   => '6379',





Now clear the cache:

rm -rf /data/web/magento2/var/cache/*

redis-cli flushall

Configure Redis Full Page Caching for Magento 2

To enable Redis page caching, extend your `/ data / web / magento2 / app / etc / env.php` with the following snippet.

You should paste it between the `cache` keys, so leave out the cache tag in this snippet.

'cache' => array (

       'frontend' => array (

           'default' => array (

               'backend'         => 'Cm_Cache_Backend_Redis',

               'backend_options' => array (

                   'server' => '',

                   'port'   => '6379',



           // Start of snippet

           'page_cache' => array (

               'backend'         => 'Cm_Cache_Backend_Redis',

               'backend_options' => array (

                   'server'        => '',

                   'port'          => '6379',

                   'database'      => '1',

                   'compress_data' => '0',



           // End of snippet



A complete example can be viewed here

Now clear the cache:

rm -rf /data/web/magento2/var/cache/*

redis-cli flushall

Empty the Cache

To clear the Magento cache, clear the Redis database corresponding to the configured Redis database:

redis-cli -n $db flushdb

or alternatively

n98-magerun2 or Magento cli tool:


## Flush using n98-magerun2

n98-magerun2 cache:flush

## Flush using magento cli

cd /data/web/magento2 && php bin/magento cache:flush


To clear all sessions, caches etc. (Empty the entire Redis instance), use the following command:


redis-cli flushall

Modification of the compression library

You can use the “Snappy” compression library. More information on Snappy can be found in the change log: Release-4224 .


To use the Snappy compression library for Redis cache you need to add ‘compression_library’ => ‘snappy’ , in your env.php under:


‘page_cache’ => array (

‘backend’         => ‘Cm_Cache_Backend_Redis’,

‘backend_options’ => array (

Configure Magento 2 to use Redis as a Session Store

You can also use Redis to store sessions!


This way the sessions are stored in memory, making the store faster and using less IO than when using MySQL or files as the session archive.


Configure Magento 2 to archive sessions in Redis

Since Magento 2 fully supports Redis, there is no need to install additional extensions to configure Redis.

All you need to do is extend your / etc / env.php app and clear the cache.


To enable session archiving in Redis, extend your /data/web/magento2/app/etc/env.php with the following snippet:

‘session’ => array(

   ‘save’ => ‘redis’,

   ‘redis’ => array(

       ‘host’                  => ‘redismaster’,

       ‘port’                  => ‘6379’,

       ‘password’              => ”,

       ‘timeout’               => ‘2.5’,

       ‘persistent_identifier’ => ”,

       ‘database’              => ‘2’,

       ‘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’       => ‘0’,

       ‘min_lifetime’          => ’60’,

       ‘max_lifetime’          => ‘2592000’,



Now refresh the cache

rm -rf / data / web / magento2 / var / cache / *

redis-cli flushall

Enable second Redis instance for sessions

We have made it possible to enable a second, more tailored Redis instance for saving session data (more information can be found in our changelog)

To enable the second Redis instance for sessions, run the command: systemctl settings redis_persistent_instance –value True

After enabling the second Redis instance you need to update the /data/web/public/app/etc/local.xml file and change the port value to 6378 instead of the default 6379. Also you need to add the following line to your crontab:

* * * * * redis-cli -p 6378 bgsave

Check if your sessions are archived in Redis

To check if your setup is working properly, first clear the session store:

rm / data / web / public / var / sessions / *

Now open the site in your browser and press F5 a few times or log into the admin panel.

Hopefully, no additional session files should be written to / data / web / var / sessions , but instead to the Redis database:

redis-cli -n 2 keys \*


A quick note, when you run into the configured maximum memory limit, make sure the necessary Redis keys are set to volatile (ensure an expiration). Otherwise, the entire allocated configured memory will fill up and Redis will “hang”.

When the Redis instance memory is full and a new write arrives, Redis removes the keys to make room for write according to the instance’s maxmemory policy. This is called the eviction policy.

In some cases, we see that when Redis reaches the configured limit and tries to expire the keys to make room, the eviction policy gets stuck in a loop. This means that the keys will not expire and Redis reaches the limit.

A workaround is to clear the Redis cache, you can do this using the flushall command:

redis-cli flushall

This will delete all available Redis databases. Keep in mind that this is only a temporary solution. The underlying cause is in the application code and needs to be fixed permanently.

A more extensive guide on configuring Redis caches can be found in the Magento help pages .


As you know, your online store sessions can also be stored on Redis. If you use Redis caching and store sessions in Redis as well, you will need to share the available Redis memory. This shouldn’t be a problem, however we have seen scenarios where a store stores its sessions in Redis and has had some aggressive robots / crawlers visiting the store. This has resulted in Redis storing far more sessions than usual, which causes Redis to fill up memory in no time and Redis to crash.

If you want to have a more detailed view of bot traffic you can use the command pnl –yesterday –php –bots –fields ua | order | uniq -c | sort -n to get an overview of the top 10 bots that visited your online store yesterday.


Free 30-days trial Hosting Fast, Secure and Optimized

Switch to Bhoost with 30 days free and migration included

Free 30-days trial