Now the next challenge arises, What is a faster and another cache system for Magento 2?
The answer to this question is Magento 2 REDIS.
In this article, you will learn about the configuration of the Redis Cache in Magento 2.
What is Redis?
Redis is an open-source robust caching system for use with Magento and other web applications. It helps to increase the speed of the backend and front end of your eCommerce shop. In short, it is nothing but an in-memory database which stores data back to disk periodically.
Magento 2 Redis cache backend helps in multi-server situations where you have to work with one or more web servers.
An advanced web app combines Web and mobile apps in order to help developers create online shops as progressive web apps.
A progressive web app operates like a webpage but can even work offline, all you need to do is carry a camera and then receive the push warnings.
Advanced internet applications have instruments and libraries like webpack, ReactJs, Redux and GraphQL to assist customize content and local preferences, rapid prototyping, useful debugging and improve efficiency.
Learn here Magento 2 Cache Overview
Need of Redis Cache
You should opt for Redis cache because of the following reasons:
- Redis has virtual memory: As once in a while, utilized parameters would be swapped out to the circle so that only the parameters needed to fit into the memory are used.
- Can be used in multi-server situations
- It is ideal as a cache system as the data is structured
- Its speed makes it incredible as a cache layer
- Database server which always there to support frequently asked questions in the cache for the users to help
-
Configuration of Redis Cache in Magento 2
Magneto 2 supports Redis as storage for the following data:
- Backend code
- Full-page code
- Sessions
Magento Redis Requirements
Redis Server and PHP Redis Extension both are the prerequisites to configure Magento Redis Cache on the hosting server.
Mageants recommends the latest versions of both.If are the owner of Magento stoner and you own the Magento hosting server, then you need to install Redis and PHP extension on your server.
Magento 2 Redis Configuration
Now, it is time to configure Magento 2 Redis for version 2.x for this; you need to add a Redis configuration script in theenv.php file,and that can be found inMagento2_ROOT/app/etcdirectory.
In theenv.phpfile, there are various settings and configurations, defined in a PHP array. Here we will add the following code at the end right before ); which show ending, and it 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', ), ), ), ),
Now, you need to save the env.php file and clear Magento 2 cache with command
php bin/Magento cache:flush
For setting Redis as a default cache storage in Magento 2, you just need to add respective configuration code to the Magento.Also Read: You Must Be Familiar With Magento 2.3.4 (Latest Update)
Setting up Redis as a Full Page Cache Storage in Magento 2
To set Redis as a Full Page Cache Storage in Magento 2, you need to add the below code.
The parameters are similar between default and page_cache only ‘database’ parameters must be different.
'cache' => array ( 'frontend' => array ( 'page_cache' => array ( 'id_prefix' => '8f5_', 'backend' => 'Cm_Cache_Backend_Redis', 'backend_options' => array ( 'server' => '127.0.0.1', or ‘localhost’, 'port' => '6379', 'persistent' => 0, 'database' => '0', 'force_standalone' => '0', 'connect_retries' => '10', 'read_timeout' => '30', 'automatic_cleaning_factor' => '0', 'compress_data' => '1', 'compress_tags' => '1', 'compress_threshold' => '20480', 'compression_lib' => 'gzip', ), ), ), ),
-
Setting up Redis as Session Storage in Magento 2
'session' => Array ( 'save' => 'redis', 'redis' => array ( 'host' => '127.0.0.1', or ‘localhost’, 'port' => '6379', 'timeout' => '5', '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' ) )
We have used multiple parameters here, to know more about these parameters you can visithttps://devdocs.magento.com/guides/v2.3/config-guide/redis/redis-session.html
Don’t forget: If Redis is used with various types, the database used for cache storage must be different between each type.
It’s done!! By following and implementing the above code, it will successfully install Redis.
Also Read: Why Should Store Owners Prefer Migration From Magento 1 To Magento 2?
-
How to Check Redis Cache is Working?
To verify whether you have configured Redis Cache correctly with Magento 2 and both are working fine together, use following commands:
-
1. Redis Information Command: redis-cli info
If you see the #Keyspace, the Redis server is working fine; if not, then check your configuration. -
2. Redis Ping Command: redis-cli ping
You will receive PONG in the console -
3. Redis Monitor Command: redis-cli monitor
Refresh your storefront page and you'll able to see the below output.For session storage, the output will be:
1476824834.187250 [0 127.0.0.1:52353] "select" "0" 1476824834.187587 [0 127.0.0.1:52353] "hmget" "sess_sgmeh2k3t7obl2tsot3h2ss0p1" "data" "writes" 1476824834.187939 [0 127.0.0.1:52353] "expire" "sess_sgmeh2k3t7obl2tsot3h2ss0p1" "1200" 1476824834.257226 [0 127.0.0.1:52353] "select" "0"1476824834.257239 [0 127.0.0.1:52353] "hmset" "sess_sgmeh2k3t7obl2tsot3h2ss0p1" "data" "_session_validator_data|a:4:{s:11:\"remote_addr\";s:12:\"10.235.34.14\";s:8:\"http_via\";s:0:\"\";s:20:\"http_x_forwarded_for\";s:0:\"\";s:15:\"http_user_agent\";s:115:\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36\";}_session_hosts|a:1:{s:12:\"10.235.32.10\";b:1;}admin|a:0:{}default|a:2:{s:9:\"_form_key\";s:16:\"e331ugBN7vRjGMgk\";s:12:\"visitor_data\";a:3:{s:13:\"last_visit_at\";s:19:\"2016-10-18 21:06:37\";s:10:\"session_id\";s:26:\"sgmeh2k3t7obl2tsot3h2ss0p1\";s:10:\"visitor_id\";s:1:\"9\";}}adminhtml|a:0:{}customer_base|a:1:{s:20:\"customer_segment_ids\";a:1:{i:1;a:0:{}}}checkout|a:0:{}" "lock" "0"... more ...
For Redis for page caching, the output will be:
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" ... more ... 1476826210.634998 [0 127.0.0.1:52439] "hmset" "zc:k:ea6_MVIEW_CONFIG" "d" "a:18:{s:19:\"design_config_dummy\";a:4:{s:7:\"view_id\";s:19:\"design_config_dummy\";s:12:\"action_class\";s:39:\"Magento\\Theme\\Model\\Indexer\\Mview\\Dummy\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:0:{}}s:14:\"customer_dummy\";a:4:{s:7:\"view_id\";s:14:\"customer_dummy\";s:12:\"action_class\";s:42:\"Magento\\Customer\\Model\\Indexer\\Mview\\Dummy\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:0:{}}s:13:\"cms_page_grid\";a:4:{s:7:\"view_id\";s:13:\"cms_page_grid\";s:12:\"action_class\";s:43:\"Magento\\Catalog\\Model\\Indexer\\Category\\Flat\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:1:{s:8:\"cms_page\";a:3:{s:4:\"name\";s:8:\"cms_page\";s:6:\"column\";s:7:\"page_id\";s:18:\"subscription_model\";N;}}}s:21:\"catalog_category_flat\";a:4:{s:7:\"view_id\";s:21:\"catalog_category_flat\";s:12:\"action_class\";s:43:\"Magento\\Catalog\\Model\\Indexer\\Category\\Flat\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:6:{s:23:\"catalog_category_entity\";a:3:{s:4:\"name\";s:23:\"catalog_category_entity\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";N;}s:31:\"catalog_category_entity_decimal\";a:3:{s:4:\"name\";s:31:\"catalog_category_entity_decimal\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:27:\"catalog_category_entity_int\";a:3:{s:4:\"name\";s:27:\"catalog_category_entity_int\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:28:\"catalog_category_entity_text\";a:3:{s:4:\"name\";s:28:\"catalog_category_entity_text\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:31:\"catalog_category_entity_varchar\";a:3:{s:4:\"name\";s:31:\"catalog_category_entity_varchar\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}s:32:\"catalog_category_entity_datetime\";a:3:{s:4:\"name\";s:32:\"catalog_category_entity_datetime\";s:6:\"column\";s:9:\"entity_id\";s:18:\"subscription_model\";s:71:\"Magento\\CatalogStaging\\Model\\Mview\\View\\Category\\Attribute\\Subscription\";}}}s:24:\"catalog_category_product\";a:4:{s:7:\"view_id\";s:24:\"catalog_category_product\";s:12:\"action_class\";s:46:\"Magento\\Catalog\\Model\\Indexer\\Category\\Product\";s:5:\"group\";s:7:\"indexer\";s:13:\"subscriptions\";a:2:{s:23:\"catalog_category_entity\";a:3:{s:4:\"name\";s:23:\"catalog_category_entity\";s:6:\"column\" ... more ...
If both commands succeed, Redis is set up correctly.
After this, type php -m | grep redis by which you'll see redis in reply, which confirms that the PHP Redis Extension is ready to proceed further.
If you want to clear Redis cache, type redis-cli flushall or redis-cli flushdb, and an OK reply will confirm that your cache has been removed successfully.
Enabling Magento Redis Cache on your online store’s backend will boost your website performance.
That’s why every Magento developer must be aware of how to configure Redis Cache in Magento 2. Let us know if you have any questions or ideas to discuss.
Happy Coding!! Stay tuned!
-
1. Redis Information Command: redis-cli info