Redis part 5 - How to integrate Redis into Magento?

31.10.2019
extendedLogo

In the fifth part of our blog series, we explain step by step how to integrate Redis into Magento. Which preparations have to be made, which code blocks have to be inserted and how Redis is activated in maxcluster.

Redis blog series

In part 1 of our blog series on the in-memory database Redis, we reported on how the performance of online shops is improved by this service. In addition to our many years of positive experience with the use of Redis, we wanted to provide our customers with facts about the expected performance improvements and have created an elaborate benchmark. Part 2 of our blog series deals with the application cache benchmark, part 3 with the sessions benchmark. Part 4 of this Redis blog series deals with the integration of Redis in Shopware. In this last and final Redis post, we cover the integration of Redis in Magento online shops.

Redis and Magento

For shops that already use Magento 2, Redis is natively supported. This means that no further plugins are necessary for integration. The sessions and the Magento cache can be stored in the Redis database and thus the performance of the online shop can be improved. The Magento PageCache can also be stored in Redis, but here we recommend using Varnish instead.

With Magento 1 shops, on the other hand, an additional plugin is required so that Redis can play out its advantages. We recommend the plugin Cm_Cache_Backend for the cache and the plugin Cm_RedisSession for sessions.

Preparations for integration

As a first step, we recommend determining the size of the Redis instances. When estimating the size, use the folder size of var/sessions and var/cache as a guide. Please note in your estimation that the cache in particular should never become full. We therefore recommend that you set the Redis instance so that it is no more than 40 % full. As a further step, it is advisable to set up monitoring for the occupancy so that you can recognise bottlenecks at short notice and react to them. If you are running a newly developed shop, a size of 1 GB for the Redis instances is a good starting point. By the way, maxcluster also monitors the cache occupancy, intervenes in an emergency and informs you.

Creating a Redis server in the maxcluster interface

Creating a Redis server in the maxcluster interface

At maxcluster, all Redis instances can be conveniently created via our interface. Please note that you must create an instance for each of the sessions and the cache in order to fully utilise the advantages of Redis for your shop. In our interface, templates can be selected for the cache and session instances, for which only the size needs to be set according to individual requirements.

If you are running the Redis instance in a different hosting setup, we recommend (as described in part 1 of our blog series) setting the "Max Memory Policy" (displacement strategy) to volatile-lru in both cases. Persistence should be deactivated for the cache instance and activated for the session instance.

Setting Redis in the maxcluster interface

Setting Redis in the maxcluster interface

Redis and Magento 2

Since Magento 2 already supports Redis natively, all that is required to activate it is to execute a command via SSH in the root directory of the Magento instance:

Sessions are stored in Redis with the following code block:

bin/magento setup:config:set -q --session-save=redis   --session-save-redis-port=0  --session-save-redis-db=2    --session-save-redis-host=/var/run/redis/6379.sock

You can store the cache in Redis as follows:

bin/magento setup:config:set -q --cache-backend=redis --cache-backend-redis-port=0 --cache-backend-redis-db=1 --cache-backend-redis-server=/var/run/redis/6380.sock

If you use an external Redis server, you can also specify this:

bin/magento setup:config:set -q --session-save=redis   --session-save-redis-port=6379  --session-save-redis-db=2    --session-save-redis-host=1.2.3.4
bin/magento setup:config:set -q --cache-backend=redis --cache-backend-redis-port=6380 --cache-backend-redis-db=1 --cache-backend-redis-server=1.2.3.4

These commands ultimately lead to a change being made in the app/etc/env.php file. It is also possible to enter the configuration directly there:

For the cache:

'cache' => [
    'frontend' => [
        'default' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => '/var/run/redis/6380.sock',
                'database' => '1',
                'port' => '0'
            ]
        ]
    ]
]

For the sessions:

'session' =>
array (
  'save' => 'redis',
  'redis' =>
  array (
    'host' => '/var/run/redis/6379.sock',
    'port' => '0',
    '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' => '600',
    'max_lifetime' => '2592000'
  )
),

Redis and Magento 1

As already mentioned at the beginning, Magento 1 does not offer native Redis support. In order to be able to use the performance advantages, two plugins must therefore be installed in advance. To simplify the installation, you can use the plugin manager Modman, which is always pre-installed in the latest version on the clusters hosted at maxcluster. If your shop is in a different environment, Modman may still need to be installed.

In order to be able to use Modman for the respective Magento instance, you must execute the following command once in the Magento root directory:

modman init

Then the two modules - or just one - can be installed.

This is done with the command for the cache module:

modman clone --copy git://github.com/colinmollenhour/Cm_Cache_Backend_Redis.git

and with the following command for the session module:

modman clone --copy https://github.com/colinmollenhour/Cm_RedisSession

Since the possibility of using system links has been reduced in Magento from version 1.9.3.3 or with the Magento patch SUPEE-9767 for security reasons, we use the parameter "--copy" here in the installation commands.

Although the module CmRedisSession is now supplied by Magento, we still recommend installing it via Modman to get the latest version. In any case, please make sure that both modules have also been activated.

Finally, the configuration must be entered in the file "app/etc/local.xml".

For the cache module, the following code block is inserted within the existing " block":

<cache>
  <backend>Cm_Cache_Backend_Redis</backend>
  <backend_options>
    <server>/var/run/redis/6380.sock</server> <!-- or absolute path to unix socket -->
    <port>0</port>\n
    <persistent></persistent> <!-- Specify unique string to enable persistent connections. E.g.: sess-db0; bugs with phpredis and php-fpm are known: https://github.com/nicolasff/phpredis/issues/70 -->
    <database>0</database> <!-- Redis database number; protection against accidental data loss is improved by not sharing databases -->
    <password></password> <!-- Specify if your Redis server requires authentication -->
    <force_standalone>0</force_standalone>  <!-- 0 for phpredis, 1 for standalone PHP -->
    <connect_retries>1</connect_retries>    <!-- Reduces errors due to random connection failures; a value of 1 will not retry after the first failure -->
    <read_timeout>10</read_timeout>         <!-- Set read timeout duration; phpredis does not currently support setting read timeouts -->
    <automatic_cleaning_factor>0</automatic_cleaning_factor> <!-- Disabled by default -->
    <compress_data>1</compress_data>  <!-- 0-9 for compression level, recommended: 0 or 1 -->
    <compress_tags>1</compress_tags>  <!-- 0-9 for compression level, recommended: 0 or 1 -->
    <compress_threshold>20480</compress_threshold>  <!-- Strings below this size will not be compressed -->
    <compression_lib>gzip</compression_lib> <!-- Supports gzip, lzf, lz4 (as l4z) and snappy -->
    <use_lua>0</use_lua> <!-- Set to 1 if Lua scripts should be used for some operations -->
  </backend_options>\n
</cache>

For sessions, the existing line

removed and replaced by a new block:

<session_save>db</session_save>
        <redis_session>                       <!-- All options seen here are the defaults -->
            <host>/var/run/redis/6379.sock</host>            <!-- Specify an absolute path if using a unix socket -->
            <port>0</port>
            <password></password>             <!-- Specify if your Redis server requires authentication -->
            <timeout>2.5</timeout>            <!-- This is the Redis connection timeout, not the locking timeout -->
            <persistent></persistent>         <!-- Specify unique string to enable persistent connections. E.g.: sess-db0; bugs with phpredis and php-fpm are known: https://github.com/nicolasff/phpredis/issues/70 -->
            <db>1</db>                        <!-- Redis database number; protection from accidental loss is improved by using a unique DB number for sessions -->
            <compression_threshold>2048</compression_threshold>  <!-- Set to 0 to disable compression (recommended when suhosin.session.encrypt=on); known bug with strings over 64k: https://github.com/colinmollenhour/Cm_Cache_Backend_Redis/issues/18 -->
            <compression_lib>gzip</compression_lib>              <!-- gzip, lzf, lz4 or snappy -->
            <log_level>1</log_level>               <!-- 0 (emergency: system is unusable), 4 (warning; additional information, recommended), 5 (notice: normal but significant condition), 6 (info: informational messages), 7 (debug: the most information for development/testing) -->
            <max_concurrency>6</max_concurrency>                 <!-- maximum number of processes that can wait for a lock on one session; for large production clusters, set this to at least 10% of the number of PHP processes -->
            <break_after_frontend>5</break_after_frontend>       <!-- seconds to wait for a session lock in the frontend; not as critical as admin -->
            <fail_after>10</fail_after>                          <!-- seconds after which we bail from attempting to obtain lock (in addition to break after time) -->
            <break_after_adminhtml>30</break_after_adminhtml>
            <first_lifetime>600</first_lifetime>                 <!-- Lifetime of session for non-bots on the first write. 0 to disable -->
            <bot_first_lifetime>60</bot_first_lifetime>          <!-- Lifetime of session for bots on the first write. 0 to disable -->
            <bot_lifetime>7200</bot_lifetime>                    <!-- Lifetime of session for bots on subsequent writes. 0 to disable -->
            <disable_locking>0</disable_locking>                 <!-- Disable session locking entirely. -->
            <min_lifetime>600</min_lifetime>                      <!-- Set the minimum session lifetime -->
            <max_lifetime>2592000</max_lifetime>                 <!-- Set the maximum session lifetime -->
            </redis_session> 

Please do not forget to clear the Magento cache as usual after changing the local.xml file.

Support

Should you require support with the described integration of Redis, we offer our customers to carry out the integration for them as described here. Please feel free to contact our support team via telephone at 05251/414130 or via e-mail at support@maxcluster.de.


Last updated on 09.06.2021 | JH

You have questions, requests, criticism, suggestions or just want to tell us your opinion about our blog? Here you have the opportunity to contact us directly.

Send e-mail