Below, I shed light on a proper Redis configuration for Magento 2. If you are not familiar with the solution, it is an advanced key-value cache with top notch performance. In addition, Redis is often considered to be a data structure server, which provides the following opportunities:
- value incrementation in a hash;
- appending to a string;
- pushing an element to a list;
- set intersection, union and difference computing;
- getting a sorted set of members with the highest ranking.
Since Redis is an open source cache, you can freely use it in your projects.
The core reason of high performance is the usage of an in-memory dataset, for which Redis incorporates several use cases. For instance, you can easily persist it by dumping the dataset to disk. Another use case is about appending each command to a log. Moreover, if your project requires a feature-rich, in-memory cache only, you can easily disable persistence.
It is also necessary to say a few words about trivial-to-setup master-slave asynchronous replication. In case of Redis, you get lightning fast non-blocking first synchronization, as well as auto-reconnection with resynchronization partial on net split. Other incredible Redis features are:
- Lua scripting;
- Limited time-to-live keys;
- Keys LRU eviction;
- Automatic failover;
- Support for most programming languages.
Redis and Magento 2
You can easily replace the default Magento 2
The most annoying one is associated with the core_cache_tag table. In case of multiple web stores and large catalogs, it can grow up to 15 million records in less than 24 hours. That leads to issues with MySQL server; for instance, its performance degradation.
The second issue is associated with difficulties related to the use of the TwoLevels backend. It is more difficult to maintain this type of backend, since two services are necessary for making it active. Besides, there are memcached limitations, such as fixed bucket sizes.
Scaling is the third problem: unfortunately, the Zend TwoLevels backend relies on the database as a part of the cache backend. This adds an additional load to the master database server. Besides, we still don’t know any reliable method for memcached replication. Therefore, the solution does not scale well.
Moreover, Redis not only helps to avoid these issues, it also offers a lot of advantages over Zend Cache. Thus, it can be used for PHP session storage, completely replacing memcached. Since the Redis backend relies on indexing file tags, tag operations do not need a full scan of all cache files.
Besides, Redis uses the same file for storing the metadata and the cache record. As a result, there are much less inodes and other operations necessary in case of this cache. Additionally, Redis does not require foreach loops for tag-based cache cleanups, and supports on-disk save, as well as master/slave replication..
How to Install Redis
First of all you need to download Redis. Use the official website to get the cache:
Magento 2 Redis Configuration
To configure redis for Magento 2, just add the following code sample to your <your Magento install dir>app/etc/env.php:
'cache' => [
'frontend' => [
'page_cache' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'persistent' => '',
'database' => 0,
'password' => '',
'force_standalone' => 0,
'connect_retries' => 1,
- page_cache is used to specify a particular segment as well as a default shortcut for other caches. For other caches, a ‘default’ segment is still needed, otherwise it will write into var/cache folder.
- server requires an absolute URL to a Redis server. If Redis is installed on the Magento server, use 127.0.0.1 . Depending on a use case, it can also be an absolute path to a UNIX socket.
- port requires a Redis server listen port.
- persistent specifies a unique string for enabling persistent connections. It can be sess-db0, for instance.
- database requires a unique Redis database number. Protect it against data loss.
- password – a password of your Redis server, if it requires authentication of course.
- force_standalone requires the following meanings: for phpredis you should use 0; for standalone PHP – 1;
- connect_retries reduces errors caused by random connection failures. To disable retry after the first failure, specify 1 .
In case of troubles with Magento 2 Redis configuration, leave your comments under the post – we are always here to help you!'