Redis for Page Cache and Session Storage
Zend_Cache_Backend_File, which is the back-end cache solution that Magento 2 by default uses, can be replaced by Redis.
So, Let’s Ask Why Use Redis?
Using Redis can have a variety of benefits:
- Redis can take the role of memcached since it can be utilized for PHP session storage as well.
- Memcached does not support on-disk save or master/slave replication, which is a highly requested functionality. High availability is made possible by replication, which prevents a single point of failure.
- As Redis operates by indexing tags in files, tag operations do not need a complete scan of every cache file.
- Without for-each loops, the back-end offers tag-based cache clean-up.
The major disadvantage is as follows:
- Since Redis is an in-memory store, all of your data needs to fit there, so it is just limited by RAM capacity and speed.
What about Configure Magento to use Redis for session storage:
Following you will find a sample configuration to add to <your Magento install dir>app/etc/env.php:
‘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’ => ‘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’
)
),
Here you can check all the details regarding the parameters and instructions on how to perform a quick check to ensure that your Redis installation works well together with your Magento.
To configure Redis for the page and default cache, there are two methods. You are able to manually edit <Magento install dir>app/etc/env.php file or you can utilize the command line, which is advised because it additionally offers validation.
To access the default cache, run this command:
php bin/magento setup:config:set –cache-backend=redis –cache-backend-redis-<parameter_name>=<parameter_value>…
Determine exactly the parameters that are specific to Redis default caching.
Run this command for the page cache:
Php bin/magento setup:config:set –page-cache=redis –page-cache-redis-server=redis.example.com –page-cache-redis-db=1
Redis page caching is enabled by this command, the host is set to redis.example.com, and the database number is assigned to 1. On the Magento DevDocs, you can review all the information.