Redis part 4 - How is Redis integrated into Shopware?

22.10.2019
extendedLogo

Redis is a complex service. Therefore, several preparatory steps should be taken for the integration into Shopware. In this blog post we explain step by step how to integrate Redis into Shopware.

Redis and Shopware

In our blog series on Redis, we have already presented the advantages of Redis. In our Redis application cache benchmark and sessions benchmark, we measured the number of write, read and delete operations per second for different backends. With this, we wanted to determine which type of storage provides better performance for online shops: storage in the main memory with Redis or in the file system with NVMe SSDs or (for the application cache) with memcached (main memory) or for the sessions with MySQL.

Our results showed that, compared to the use of NVMe SSDs, memcached or MySQL for medium to larger shops with increased conversion, Redis provides better performance due to the higher throughput of operations per second. This is a clear advantage of Redis, especially for Shopware users. By default, Shopware stores the sessions in the MySQL database instead of in the file system. This allows for a significantly lower number of operations per second and is therefore much more in-perfomanent.

Shopware already supports Redis natively. For this reason, sessions, the Shopware cache and the Doctrine cache (model) can be stored in Redis without the integration of plugins.

In the Enterprise Edition of Shopware, Redis is also supported as an HTTP cache.

Preparing the Redis installation

In order to integrate Redis into Shopware, various preparatory steps must be taken:

Install Redis: At maxcluster, Redis is already pre-configured and can be activated in our interface with a mouse click. Alternatively, Redis can usually be installed via the package management of the operating system.

Adjust the size of the Redis instances: For existing shops, the size of the var/sessions and var/cache folders can be used for estimation. The cache in particular must never become full, so we recommend setting the Redis instance so that it is no more than 40 % full. For newly developed shops, a size of 1 GB is a good starting point. The size can be set via the Redis setting maxmemory. With maxcluster, the size can be configured via our Managed Center.

Fill in displacement strategy: For cache and sessions we recommend the displacement strategy volatile-lru, see details in post: Redis part 1 - Performance optimization for Shopware and Magento. To use this strategy, the maxmemory-policy setting must be set to volatile-lru.

Activate persistence: Persistence must still be activated for sessions, but it is a hindrance for cache instances. We described details on persistence in part 1 of our blog series.

Set up monitoring: We recommend setting up a monitoring system for the Redis instance allocation. To do this, you can read out the current allocation via the Redis command "info memory". With maxcluster, such monitoring is automatically active for all instances marked as cache.

Simple integration of Redis at maxcluster

At maxcluster, the Redis instances can be conveniently created via our interface. To use Shopware completely with Redis, 3 instances must be created (Sessions, Cache and Model). The "Cache" preset can also be used for the model cache. Since Redis works with only a single thread, it is not recommended to use the same instance several times. Another reason against using the same instance several times is that the settings cannot be adjusted individually, but apply to a complete instance.

Create Redis Model at maxcluster

Create Redis Model at maxcluster

Activate Redis in the maxcluster interface

Activate Redis in the maxcluster interface

Redis configuration for Shopware 5

In order to configure Redis for Shopware 5, only the config.php must be adapted. With the classic installation, this is located directly in the Shopware root directory.

If Shopware was installed via Composer, the config.php is located under app/config/config.php instead.

NOTE

If Shopware 5 is used in conjunction with the Enterprise Tools, Shopware does not store every element within a Redis key, but creates only a few keys where an element is stored as a subpage.

This means that if the Redis instance is too small, it may be constantly fully utilized and performance may suffer as a result. This is due to the fact that with the Redis Eviction Policies whole Keys are removed and therefore not only single pages, but whole blocks.

To counter this, we recommend monitoring and adjusting the Redis cache size accordingly. At maxcluster, use the Redis template "Cache" in the Managed Center, as this is monitored by our monitoring.

Sessions

To save sessions in Redis, the following code block can be inserted into config.php:

'session' => array(
  'save_handler' => 'redis',
  'save_path' => "tcp://127.0.0.1:6379",
),

'backendsession' => array(
  'save_handler' => 'redis',
  'save_path' => "tcp://127.0.0.1:6379",
),

Port and IP address may have to be adjusted individually, in our example it is the standard port of Redis. After the change, the Shopware cache must be cleared.

Subsequently, it can be checked via SSH whether the integration was successful.

This is done via the command:

redis-cli -p 6379 MONITOR

If the Redis service is located on the same server as the application, it is advisable to connect via a socket instead of TCP, as TCP has a higher overhead.

'session' => array(
  'save_handler' => 'redis',
  'save_path' => "/var/run/redis/6379.sock",
),

'backendsession' => array(
  'save_handler' => 'redis',
  'save_path' => "/var/run/redis/6379.sock",
),

Redis Cache in Shopware 5

The integration of the cache is done equivalently to the session integration with the following code block:

'cache' => [
  'backend' => 'redis', // e.G auto, apcu, xcache
  'backendOptions' => [
    'servers' => array(
      array(
        'host' => '127.0.0.1',
        'port' => 6380,
        'dbindex' => 1,
        'redisAuth' => ''
      ),
    ),
  ],
],

Or through the code block:

'cache' => [
  'backend' => 'redis', // e.G auto, apcu, xcache
  'backendOptions' => [
    'servers' => array(
      array(
        'host' => '/var/run/redis/6380.sock',
        'port' => 0,
        'dbindex' => 1,
        'redisAuth' => ''
      ),
    ),
  ],
],

Model

Also for the model, only the config.php has to be adapted by inserting the following code block:

'model' => [
  'redisHost' => '127.0.0.1',
  'redisPort' => 6381,
  'redisDbIndex' => 2,
  'cacheProvider' => 'redis'
],

Or through the code block:

'model' => [
  'redisHost' => '/var/run/redis/6381.sock',
  'redisPort' => 0,
  'redisDbIndex' => 2,
  'cacheProvider' => 'redis'
],

Redis configuration in Shopware 6

Compared to Shopware 5, Shopware 6 has made some far-reaching changes to the core, which also affects the configuration of Redis. Among other things, the config.php known from Shopware 5 is gone. Instead, the configuration is now done via environment variables and the possibilities of the Symfony framework. We have described more details about Shopware 6 in the blog post Milestone: What does Shopware 6 offer?.

Preparations

To use Redis as a cache on Shopware 6, we recommend configuring Redis with environment variables, unlike what is described in the official documentation (https://developer.shopware.com/docs/guides/hosting/performance/caches#example-replace-some-cache-with-redis). To do this, create the following variables accordingly in the Shopware root directory within the ".env" file:

REDIS_CACHE_HOST=127.0.0.1
REDIS_CACHE_PORT=6379

So that the environment variables can be used later within the framework, you must define them in the services.yaml file. To do this, create the file config/services.yaml in the Shopware root directory - if it does not already exist - and define the following parameters:

parameters:
    app.redis.cache.host: "%env(REDIS_CACHE_HOST)%"
    app.redis.cache.port: "%env(REDIS_CACHE_PORT)%"

HTTP Cache

If only the HTTP cache pool is to be used in Redis, you can store the following configuration in the file config/packages/framework.yaml. If the file does not exist yet, it can be created:

framework:
    cache:
        default_redis_provider: "redis://%app.redis.cache.host%:%app.redis.cache.port%"
        pools:
            cache.http:
                default_lifetime: 3600
                adapter: cache.adapter.redis
                tags: cache.tags

App Cache

If all cache adapters and not only the HTTP pool should use Redis, this can be configured as follows in the config/packages/framework.yaml file:

framework:
    cache:
        app: cache.adapter.redis
        default_redis_provider: "redis://%app.redis.cache.host%:%app.redis.cache.port%"

Session

Shopware itself does not offer its own way to store sessions in Redis and refers to the underlying Symfony framework. To do this, we create variables in the .env in the root directory, similar to before:

REDIS_SESSION_HOST=127.0.0.1
REDIS_SESSION_PORT=6380

To make sure these values are now used, we edit the config/services.yaml file from the root directory as follows:

services:
    Redis:
        class: Redis
        calls:
            - method: connect
              arguments:
                  - '%env(REDIS_SESSION_HOST)%'
                  - '%env(int:REDIS_SESSION_PORT)%'
    Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
        arguments:
            - '@Redis'

Afterwards we define the handler in the framework in the config/packages/framework.yaml file

framework:
    session:
        handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler

Rework

For the changes to take effect, the Shopware cache must be cleared. To do this, run the following command on the command line in the Shopware root directory:

bin/console cache:clear

We are happy to support you

If you would like support with the integration of Redis, we offer to carry out the integration for our customers as described here. Please contact our support team by phone at 05251/41 41 30 or by email 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