10/24/2019 - #development #magento
by: Kévin Weyhaupt

 

What’s that?


Redis is a cache system using the RAM of the server to rapidly access the data with a key / value combination.

I am going to introduce you how to implement Redis on Magento and boost your server side performances. I hope this article will be as fast and efficient as Redis is!

Quickly! I want to set it up, how do I do? 

Installation

To install Redis on your server and make it usable with PHP you only need one command (this is only true for Debian/Ubuntu OS):

apt install redis-server php-redis

And… That’s it!

Check

Don’t hesitate to check the installation. Once again, it’s really easy and you just need to execute the following command:

redis-cli ping

If the answer of Redis is PONG then we are good to go. If that’s not the case, you will need to check the logs of Redis located here: /var/log/redis/redis-server.log

Redis is a service so you can check its status like any other:

service redis-server status

Last check to be sure that PHP is really taking the Redis module into account:

php -m | grep redis

If there is no result, you just need to enable the module:

phpenmod redis

What about Magento in all of this?

Requirements

Prior to configure Redis to be used with Magento you need to know the port used by Redis on your system. To find out this, you have two possibilities:

service redis-server status

or

redis-cli info | grep port

Magento 1

To make Redis work with Magento 1 it’s required to have the module Cm_RedisSession enable. (check app/etc/modules)
You need to specify the configuration in the etc/local.xml file. Check the tag <session_save> contains db and not file.
Add this piece of XML code inside the tags <config><global>...</global></config> and verify that the <port> matches the one in the requirements (for more details https://github.com/colinmollenhour/Cm_RedisSession) :

<session_save>db</session_save>
<redis_session>
<host>127.0.0.1</host>
<port>6379</port>
<password></password>
<timeout>5</timeout>
<persistent></persistent>
<db>1</db>
<compression_threshold>2048</compression_threshold>
<compression_lib>gzip</compression_lib>
<log_level>1</log_level>
<max_concurrency>6</max_concurrency>
<break_after_frontend>5</break_after_frontend>
<break_after_adminhtml>30</break_after_adminhtml>
<first_lifetime>600</first_lifetime>
<bot_first_lifetime>60</bot_first_lifetime>
<bot_lifetime>7200</bot_lifetime>
<disable_locking>0</disable_locking>
<min_lifetime>60</min_lifetime>
<max_lifetime>2592000</max_lifetime>
</redis_session>

<cache>
<backend>Cm_Cache_Backend_Redis</backend>
<backend_options>
<server>127.0.0.1</server>
<port>6379</port>
<persistent></persistent>
<database>2</database>
<password></password>
<force_standalone>0</force_standalone>
<connect_retries>1</connect_retries>
<read_timeout>10</read_timeout>
<automatic_cleaning_factor>0</automatic_cleaning_factor>
<compress_data>1</compress_data>
<compress_tags>1</compress_tags>
<compress_threshold>20480</compress_threshold>
<compression_lib>gzip</compression_lib>
<use_lua>0</use_lua>
</backend_options>
</cache>

Listen up! Don’t forget to double (or triple) check these configurations, especially on live sites because it’s better to be safe than sorry. If the configuration for Magento 2 doesn’t interest you can pass the next chapter and scroll a bit further down. If you are not interested about the configuration for Magento 2 you can pass the next chapter and scroll a bit further down.


Magento 2

A bit of configuration and you will be good to go with Magento 2. You need to edit the app/etc/env.php file and add two configurations to it. One is for the storage of the sessions, the other one is for the storage of the pages and the default caches. (Once again, don’t forget about the port!)

Let’s begin with the storage of sessions:

'session' => [
'save' => 'redis',
'redis' =>
[
'host' => '127.0.0.1',
'port' => '6379',
'password' => '',
'timeout' => '2.5',
'persistent_identifier' => '',
'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'
]
],

For more details, I suggest you reading the documentation made by Magento itself: https://devdocs.magento.com/guides/v2.3/config-guide/redis/redis-session.html

Now the storage of pages and the default caches:

'cache' => [
'frontend' => [
'default' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1',
'database' => '0',
'port' => '6379'
],
],
'page_cache' => [
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' => [
'server' => '127.0.0.1',
'port' => '6379',
'database' => '1',
'compress_data' => '0'
]
]
]
],

Once again I recommend learning more about this by reading Magento’s documentation: https://devdocs.magento.com/guides/v2.3/config-guide/redis/redis-pg-cache.html

Check (Magento 1 et 2)

Of course we check!

To make sure that Redis is working as expected on your Magento instance you can execute the following command:

redis-cli monitor

After executing the command you need to browse your site. The firsts loads might be long but then it will become faster and faster. If Redis is working as it should you should see in your console what Redis is doing (SELECT, HGET, ADD) during your browsing of the site (load of collections, read of session ...).

Cache Flush

The cache of Redis being in the RAM of your server you can reboot your server to empty it. That’s not really convenient, don’t you think?

Actually, you only need to use the redis-cli command and then do a flushall :

kevin@kevin:~$ redis-cli
127.0.0.1:6379> flushall
OK
127.0.0.1:6379>

Bonus

Before leaving you I have a small gift for the people that are still interested. If you are willing to understand in practice how Redis does work and its multiple possibilities you can try this interactive tutorial: http://try.redis.io/

I hope that I helped you to boost your server side performances. I want to add one last thing before I let you go. It’s not the cache that makes your applications faster but the application of good practice and code quality.

Write your comment

Ready
for take-off?

Blackbird is a web agency specialized in the development of eCommerce websites. We are Magento Experts and we offer our services and advice for e-marketing, strategy, branding, deployment and methodology.

Contact
+339 50 66 21 38


Legal terms
Agency 30, Avenue du Rhin
67100 Strasbourg
France
SEE MAP
FR - EN