Are you looking to configure Magento Redis cache for Magento 2?
The Magento is a powerful ecommerce platform and supports multiple caching including Full page cache, Varnish cache and APC. Magento owners use different versions and combinations of caching to accelerate the store speed as per the requirement and demand of the store.
If your store has lots of data, you should consider installing Magento 2 Redis cache.
In this tutorial, we will see how to config Redis cache in Magento 2.
What is Redis Cache?
Redis is often used to cache frequently accessed data in memory so that applications can be responsive to users. It’s mighty useful with its capacity to designate how long you want to keep the information and which information needs to be purged first.
Redis has virtual memory so that only the keys needed to fit into memory are being used. Moreover, Redis’ use cases work well when the data sets it holds in its memory can fit into that space.
Advantages of Using Magento 2 Redis
Advantages of using Magento 2 Redis cache:
- Use PHP session storage to replace memcached with Redis.
- No need to do a complete scan of every cache file as Redis backend works by indexing tags.
- The backend supports tag-based cache cleanup without for each loops.
- The metadata and the cache record are stored in the same file rather than separate files – it means fewer files to read, write, lock and unlink operations.
- Redis supports on-disk save and master/slave replication.
Magento 2 Redis Configuration
Let’s configure Redis cache in Magento 2 now. You have to add a Redis configuration script in the env.php file that you can find in the Magento2_ROOT/app/etc directory.
Magento\Framework\App\CacheInterface implements redis cache in Magento 2. In the env.php file, PHP array defines the different settings and configurations. To add the Redis cache, you have to add the following code at the end right before );.
'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'
]
]
]
],
Save the env.php file and clear the Magento 2 cache with php bin/magento cache:flush command.
Code explanation
default, page_cache: Specify the name of segment if you want to use a particular segment. Leave it at default for all other caches.
server: URL of Redis server or absolute path to a UNIX socket. If Redis is installed on the Magento server, the address would be 127.0.0.1.
port: Redis server listen port
database: Specify the database number of one of the cache. Other cache will use the 0 by default. It is required in case you use Redis for both the default and full page cache.
Important: Using Redis for many types of caching – then you should specify the database numbers.
password: If the Redis server has authentication enabled, then you must specify the password.
compress_data: Required for the full page caching. To compress the full page cache, set to 1. The algorithm chooses the compression in order of availability: snappy, l4z or lzf. Finally, if none of them are available, Redis use zgip.
Redis verification
Lastly, verify if the redis and magento are compatible and working together. Run the following commands.
Redis ping command:
redis-cli ping
You will receive PONG in the console.
Redis monitor command:
redis-cli monitor
If you use Redis for page caching, you’ll see output similar to the following:
1476826133.810090 [0 127.0.0.1:52366] "select" "1"
1476826133.816293 [0 127.0.0.1:52367] "select" "0"
1476826133.817461 [0 127.0.0.1:52367] "hget" "zc:k:ea6_GLOBAL__DICONFIG" "d"
1476826133.829666 [0 127.0.0.1:52367] "hget" "zc:k:ea6_DICONFIG049005964B465901F774DB9751971818" "d"
1476826133.837854 [0 127.0.0.1:52367] "hget" "zc:k:ea6_INTERCEPTION" "d"
1476826133.868374 [0 127.0.0.1:52368] "select" "1"
1476826133.869011 [0 127.0.0.1:52369] "select" "0"
1476826133.869601 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_DEFAULT__10__235__32__1080MAGENTO2" "d"
1476826133.872317 [0 127.0.0.1:52369] "hget" "zc:k:ea6_INITIAL_CONFIG" "d"
1476826133.879267 [0 127.0.0.1:52369] "hget" "zc:k:ea6_GLOBAL_PRIMARY_PLUGIN_LIST" "d"
1476826133.883312 [0 127.0.0.1:52369] "hget" "zc:k:ea6_GLOBAL__EVENT_CONFIG_CACHE" "d"
1476826133.898431 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DB_PDO_MYSQL_DDL_STAGING_UPDATE_1" "d"
1476826133.898794 [0 127.0.0.1:52369] "hget" "zc:k:ea6_RESOLVED_STORES_D1BEFA03C79CA0B84ECC488DEA96BC68" "d"
1476826133.905738 [0 127.0.0.1:52369] "hget" "zc:k:ea6_DEFAULT_CONFIG_CACHE_STORE_DEFAULT_10__235__32__1080MAGENTO2" "d"
If the commands are working, it means Redis is configured properly with the Magento store.
Wrap Up
With Breeze, you don’t have to worry about it. We do it for you. Take a Demo if you want to see yourself.
Redis improved the Magento store performance, and made it faster. Speed is critical for ecommerce stores, so you should not ignore any optimization that can help.
If you encounter any issue installing the redis cache in Magento 2, please leave it in the comment box.