Redis, an in-memory data structure store, is the gold standard for Magento 2 Caching.
It’s lightning-fast, reliable, and significantly reduces the load on your
database by handling caching operations.
You’ve already installed Redis, which is the first crucial step. Now, let’s bridge the gap and show Magento how to use it.
This guide will walk you through the exact steps to configure the Redis backend cache in Magento 2.
Redis must be installed on your server before continuing.
Why Configure Redis Backend Cache?
Before we dive in, let’s quickly recap why this configuration is so vital:
- Blazing Speed: Redis operates entirely in memory, making data retrieval incredibly
fast compared to disk-based or database caching. - Reduced Database Load: By offloading cache storage to Redis, your database is freed
up to handle critical transactional queries, improving overall site stability and performance. - Improved Scalability: A lighter database load means your store can handle more
concurrent users without a degradation in performance. - Centralized Cache Management: Redis provides a single,
efficient location for all your caching needs, from configuration and layouts
to full-page content.
Backup Your Configuration env.php file.
Safety first! Before making any changes, it’s essential to create a backup of your
Magento environment configuration file.
From your Magento root directory, run the following command:
cp app/etc/env.php app/etc/env.php.bak
If anything goes wrong, you can easily restore the original file.
Configure the Backend Cache
The core of the configuration happens in the app/etc/env.php file.
You will need to edit this file to tell Magento to use Redis as the backend cache storage.
Using Redis for the backend cache improves overall system performance
by reducing filesystem load and speeding up cache read/write operations.
For best practices, it is recommended to use a dedicated Redis
database (e.g., db 0) for backend caching to avoid conflicts with other cache pools.
The following example enables Redis backend cache, sets the host to 127.0.0.1, and assigns the database number to 1. All other parameters are set to the default value.
bin/magento setup:config:set --cache-backend=redis --cache-backend-redis-server=127.0.0.--cache-backend-redis-db=1 --cache-backend-redis-port=6379
After running these commands Magento 2 adds configuration to app/etc/env.php file.
'cache' => [
'frontend' => [
'default' => [
'backend' => '\\Cm\\Cache\\Backend\\Redis', // The Redis backend adapter
'backend_options' => [
'server' => '127.0.0.1', // Your Redis server IP
'port' => '6379', // Your Redis server port
'database' => '1', // Redis database for backend cache
// 'password' => 'your-redis-password', // Uncomment and set if you have a password
'force_standalone' => 0,
'connect_retries' => 1,
'read_timeout' => 10,
'automatic_cleaning_factor' => 0,
'compress_data' => 1,
'compress_tags' => 1,
'compress_threshold' => 20480,
'compression_lib' => 'gzip',
],
],
],
],
Key Parameters Explained:
'backend': This tells Magento to use the Cm_RedisCache module’s backend adapter.'server': The IP address or hostname where your Redis server
is running.127.0.0.1is used if Redis is on the same server as Magento.'port': The port your Redis server is listening on (default is6379).'database': The Redis database number.
Crucially, we use0fordefaultand1forpage_cacheto keep them separate.'password': If your Redis instance is secured with a password,
uncomment this line and add your password.
Save the env.php file after making these changes.
Flush Magento Cache and Verify
Now that the configuration is in place, you need to tell Magento to
read the new settings and clear any existing cache.
From your Magento root directory, run:
bin/magento cache:flush
This command clears all cache types and forces Magento to
rebuild them using the new Redis backend cache.
How to Verify Redis Backend Cache is Working
redis-cli
Once inside, you can monitor the live traffic. Type MONITOR and press Enter.
redis-cli> MONITOR
Now, load a few pages of your Magento store in your web browser.
You should see a stream of commands appearing in your terminal window,
looking something like this:
1612345678.123456 [0 127.0.0.1:54321] "GET" "zc:k:default_..." 1612345678.234567 [1 127.0.0.1:54322] "HGET" "zc:k:FPC..." ...
The [0] and [1] indicate the database being used, confirming that both your default
cache and FPC are being stored in Redis. Press Ctrl+C to stop monitoring.
Reference Redis Magento 2 doc
Be the first to comment.