How to configure Magento 2 to use Redis

How to configure Magento 2 to use Redis

Redis - High-Performance Database

Redis is an open-source, high-performance database, in-memory data structure store. Redis is a key-value store; the meaning is primarily designed to store data using a unique key.

Redis is tremendously popular among developers worldwide, and chances are you use many applications that utilize Redis under the covers, in one way or another.

Best Magento Hosting now

Why is redis so fast?

Redis uses the memory to store data. The entire data set, like Memcached, is stored in memory, so it is extremely fast. Redis is a fantastic choice if you want a highly scalable data store shared by multiple processes or multiple servers. The most significant advantages over Memcached is that is cache tag support included and can store data persistently.

Advantages of using Redis with Magento 2

  • Extreme fast in-memory storage
  • Vertical and Horizontal Scaleable
  • Shareable among Multi Servers
  • Cache Tag Support
  • Persistence

Magento 2 and Redis

Magento 2 is shipped with Redis support out of the box, and we can use it for cache, sessions, and full-page cache.

Magento 2 cache entries are organized in groups like Configuration, Layous, Blocks HTML Output, etc. If you have a shop(s) with a vast amount of products, your cache will be huge. Because of that, it's highly recommended to use Redis as a cache backend. Out of the box, Magento 2 uses the file system as cache storage, which isn't recommended because it's the slowest cache storage you can use. For each cache group deletion, Magento needs to open all files to check if it's in the cache group or not. If you do this with thousands of files, which you will have with a big shop, it will slow down your shop.

How to configure Magento 2 to use Redis as cache storage?

To configure Redis for Magento 2 cache, replace the cache settings in app/etc/env.php with the following one:

'cache' => [
    'frontend' => [
        'default' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => '127.0.0.1',
                'port' => '6379',
                'database' => '0',
                'compress_data' => '1',
                'force_standalone' => '0',
                'connect_retries' => '10',
                'read_timeout' => '30',
                'automatic_cleaning_factor' => '0',
                'compress_tags' => '1',
                'compress_threshold' => '20480',
                'compression_lib' => 'gzip'
            ]
        ]
    ]
],
If Redis is working, you should not see any files and directories in the folder var/cache/ anymore.
Further, you can connect with redis-cli info to check if there entries in the databases.

How to configure Magento 2 to use Redis as session storage?

To configure Magento 2 to use Redis for sessions, replace the session settings in app/etc/env.php with the following one:

'session' => [
    'save' => 'redis',
    'redis' => [
        'host' => '127.0.0.1',
        'port' => '6379',
        'password' => '',
        'timeout' => '15',
        'persistent_identifier' => '',
        'database' => '2',
        'compression_threshold' => '2048',
        'compression_library' => 'gzip',
        'log_level' => '1',
        'max_concurrency' => '15',
        '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'
    ]
],
Make sure to use any database number greater than 0 because database 0 is used for cache.
Shraddha S.
Shraddha S.
Head of Content

Shraddha Singh has a lot of thoughts about Technology and the Cloud Services Industry. An Indian native and a professional Technical writer, she gets her management skills from IIT-B.


Get the fastest Magento Hosting! Get Started