Best practices for Redis service configuration

  • Use the extended Redis cache implementation, which includes the following optimizations to minimize the number of Redis queries that are performed on each request from Adobe Commerce:
    • Diminishes the size of network data transfers between Redis and Adobe Commerce
    • Lowers Redis consumption of CPU cycles by improving the adapter’s ability to automatically determine what needs to be loaded
    • Reduces race conditions on Redis write operations
  • Separate the Redis cache from the Redis session
  • Compress the Redis cache and use gzip to improve performance

Extended Redis cache implementation

Update your configuration to use the extended Redis cache implementation \Magento\Framework\Cache\Backend\Redis.

Configure cloud deployments

Configure enhanced Redis cache by setting the REDIS_BACKEND deployment variable in the .magento.env.yaml configuration file.

stage:
  deploy:
    REDIS_BACKEND: '\Magento\Framework\Cache\Backend\Redis'

For details, see the REDIS_BACKEND variable description in the Commerce on Cloud Infrastructure Guide.

NOTE

Check the ece-tools version installed in your local environment from the command line using the composer show magento/ece-tools command. If necessary, update to the latest version.

WARNING

Do not configure a Redis slave connection for cloud infrastructure projects with a scaled architecture. This causes Redis connection errors. See the Redis configuration guidance in the Commerce on Cloud Infrastructure guide.

Configure on-premises deployments

For Adobe Commerce on-premises deployments, configure the new Redis cache implementation using the bin/magento:setup commands. For instructions, see Use Redis for default cache.

Separate cache and session instances

Separating the Redis cache from Redis session allows you to manage the cache and sessions independently to prevent cache issues from affecting sessions.

  1. Update the .magento/services.yaml configuration file.

    mysql:
        type: mysql:10.4
        disk: 35000
    
    redis:
       type: redis:6.0
    
    redis-session:              # This is for the new Redis instance
       type: redis:6.0
    
    search:
       type: elasticsearch:7.9
       disk: 5000
    
    rabbitmq:
       type: rabbitmq:3.8
       disk: 2048
    
  2. Update the .magento.app.yaml configuration file.

    relationships:
        database: "mysql:mysql"
        redis: "redis:redis"
        redis-session: "redis-session:redis"   # Relationship of the new Redis instance
        search: "search:elasticsearch"
        rabbitmq: "rabbitmq:rabbitmq"
    
  3. Submit an Adobe Commerce Support ticket to change the Redis service configuration on Pro Production and Staging environments. Include the updated .magento/services.yaml and .magento.app.yaml configuration files.

  4. Verify that the new instance is running and make a note of the port number.

    echo $MAGENTO_CLOUD_RELATIONSHIPS | base64 -d | json_pp
    
  5. Add the port number to the .magento.env.yaml configuration file.

    NOTE

    disable_locking must be set to 1.

    SESSION_CONFIGURATION:
      _merge: true
      redis:
        port: 6374       # check the port in $MAGENTO_CLOUD_RELATIONSHIPS
        timeout: 5
        disable_locking: 1
        bot_first_lifetime: 60
        bot_lifetime: 7200
        max_lifetime: 2592000
        min_lifetime: 60
    
  6. Remove sessions from the default database (db 0) on the Redis cache instance.

    redis-cli -h 127.0.0.1 -p 6374 -n 0 FLUSHDB
    

During deployment, you should see the following lines in the build and deploy log:

W:   - Downloading colinmollenhour/credis (1.11.1)
W:   - Downloading colinmollenhour/php-redis-session-abstract (v1.4.5)
...
W:   - Installing colinmollenhour/credis (1.11.1): Extracting archive
W:   - Installing colinmollenhour/php-redis-session-abstract (v1.4.5): Extracting archive
...
[2022-08-17 01:13:40] INFO: Version of service 'redis' is 6.0
[2022-08-17 01:13:40] INFO: Version of service 'redis-session' is 6.0
[2022-08-17 01:13:40] INFO: redis-session will be used for session if it was not override by SESSION_CONFIGURATION

Cache compression

Use cache compression, but be aware that there is a trade-off with client-side performance. If you have spare CPUs, enable it. See Use Redis for session storage.

stage:
  deploy:
    REDIS_BACKEND: '\Magento\Framework\Cache\Backend\RemoteSynchronizedCache'
    CACHE_CONFIGURATION:
      _merge: true;
      frontend:
        default:
          backend_options:
            compress_data: 4              # 0-9
            compress_tags: 4              # 0-9
            compress_threshold: 20480     # don't compress files smaller than this value
            compression_lib: 'gzip'       # snappy and lzf for performance, gzip for high compression (~69%)

Additional information

On this page