Redis is a popular key-value storage database, cache, and message broker. It helps keep stable performance for high-traffic Magento stores.
To configure Redis in Magento 2, you can try any one of the below two methods:
Method 1 – Setup Redis from Configuration:
To enable the Redis in Magento 2 run the setup:config:set command. You can specify the parameters of Redis default caching.
php 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-redis-<parameter_name>=<parameter_value>
is the parameter/value pairs that set up page caching. The list includes:
- cache-backend-redis-server: In this parameter, we set the server name which indicates a fully qualified hostname.
- cache-backend-redis-port: In this parameter, we set the port name. It is a Redis server listening to port 6379 (server 127.0.0.1, port 6379)
- cache-backend-redis-db: In this parameter, we set the database id for which Magento 2 is using as default. It is required if you use Redis for both full-page and default 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.
- cache-backend-redis-password: In this parameter, we set the password which enables its security feature: the auth command.
Method 2 – Adding directly in code:
- Install Redis on your server. You can follow the instructions in the Redis documentation to install it. You can find installation instructions for various operating systems at the following link: https://redis.io/download
- Edit the `env.php` file in the `app/etc` directory of your Magento 2 installation.
- In the `env.php` file, find the `cache` section and update it with the following information:
'cache' => [
'frontend' => [
'default' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1', // or your Redis server IP address
'database' => '0', // Redis database number
'port' => '6379' // Redis server port
],
],
'page_cache' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1', // or your Redis server IP address
'port' => '6379', // Redis server port
'database' => '1', // Redis database number
'compress_data' => '0'
]
]
]
],
Save the env.php file and exit.
Flush the cache by running the following command:
php bin/magento cache:flush
This will configure Redis as the cache backend for Magento 2. You may also want to consider using Redis for session storage and full-page caching further to improve the performance of your Magento 2 store. This is all about the usage of Redis for Magento 2.
I hope this helps! If you have any further questions, please don’t hesitate to reach out.