Store Owners, Merchants as well as Developers are now adopting Magento 2 because of its new design, the increase in security & performance, memory optimization, and support with latest technology trends. But still everyone is looking and trying to improve the Magento site’s performance and responsiveness using different optimization techniques.
Most of the developers always go through different approaches like enabling default cache and compilation of Magento 2, but, unfortunately, utilizing the file-system based cache for a Magento site simply makes your site run a bit faster instead of giving it a boost.
So the question raised is that, what would you use? A file-system based cache or any other option by which you’ll be able to work and load your site faster? What if you develop your Magento store with a faster backend cache?
Sounds interesting, right?
Same as our previous article How To Setup Redis Cache On Magento Backend, let’s talk about Redis Cache and its configuration on Magento 2.
Why is Redis better?
Redis is a very fast backend cache with full cache tag support, no need for low-level file system cache and on high traffic Magento stores, the performance is great and stable. It is also highly recommended in multi-server environments.
Scale Your Magento 2 Store With Ease
One-Click Magento installation with your own managed hosting solution.
The following are further advantages of Redis with Magento 2:
- Decreases the memory usage with the backend support of tag-based cache cleanup without any foreach loops.
- The Redis backend works by indexing tags in files so that tag operations do not require a full scan of every cache file.
- The metadata and the cache record are stored in the same file rather than separate files resulting in fewer file stat, read, write, lock, and unlink operations.
- Redis supports on-disk save and master/slave replication.
The following are the prerequisites to configure Redis Cache on Magento 2:
- Redis Server
- PHP Redis Extension
The latest versions are always preferable.
Cloudways servers are based on Redis Cloud Hosting which provides unbeatable performance. If you are a Cloudways user, you only have to activate Redis or if you have your own web server, you need to install Redis and PHP Redis Extension on your server.
Activate Redis on Cloudways
Login to your Cloudways account. Click on your Server. Under “Server Management”, click “Settings & Packages” and click “PACKAGES” tab. You’ll see the Redis option, click “INSTALL” button. After that, you’ll see a waiting screen for the server where Redis is being activated.
After Redis activation, Go to “Manage Services” and verify that Redis status shows green signal, its mean Redis has been successfully activated on your server.
For more information, see this article Activate Redis On Your Cloud Server Now.
Configure Redis Cache
In this step, we are going to configure Redis on Magento 2. You need to add Redis configuration in Magento2_ROOT_Directory > app > etc > env.php file. In this env.php file, you would find a large PHP array with different settings and configuration. We would add the following configuration at the end of this file right before “);” which is the ending parenthesis of PHP array:
'cache' => array ( 'frontend' => array ( 'default' => array ( 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => array ( 'server' => '127.0.0.1', 'port' => '6379', 'persistent' => '', 'database' => '0', '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', ), ), 'page_cache' => array ( 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => array ( 'server' => '127.0.0.1', 'port' => '6379', 'persistent' => '', 'database' => '1', 'force_standalone' => '0', 'connect_retries' => '1', 'read_timeout' => '10', 'automatic_cleaning_factor' => '0', 'compress_data' => '0', 'compress_tags' => '1', 'compress_threshold' => '20480', 'compression_lib' => 'gzip', ), ), ), ),
Save your env.php file and clear Magento 2 cache with php bin/magento cache:flush command.
Monitor & Verify Redis Cache
To verify whether you have correctly configured Redis cache on Magento 2, you can use the following commands in the SSH terminal:
- Type redis-cli monitor and refresh your site, you will see an output on your screen.
- Type redis-cli ping and you’ll get a reply of ‘PONG’ which means everything is looking good.
- Type php -m | grep redis by which you’ll see ‘redis’ in reply, which confirms that the PHP Redis Extension is ready to go.
Also, if you need to clear Redis cache, simple type redis-cli FLUSHALL and an OK reply will confirm that your cache has been cleared successfully.
Boost Your Magento Store Performance by 5x Times & Maximize Your Sales
Our fastest Magento hosting can help you in growing your business revenue by 500%
Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at email@example.com