How to Optimize Magento 2 Performance with Redis Caching

In the world of eCommerce, speed and scalability are critical for providing an optimal user experience. Magento 2, a robust platform for online stores, offers various tools and technologies to enhance performance. One of the most powerful tools in this arsenal is Redis.

In this blog, we’ll dive into what Redis is, how it works, and how you can use it to supercharge your Magento 2 store.

What is Redis?

Redis (Remote Dictionary Server) is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and more. Redis is known for its speed and efficiency, making it an ideal choice for caching and real-time analytics.

Key Features of Redis:

  • In-memory storage: Redis stores data in memory, leading to extremely fast read and write operations.
  • Data Persistence: Redis provides options for persisting data to disk, ensuring data durability.
  • Scalability: Redis can handle millions of requests per second, making it highly scalable for large applications.
  • Flexibility: Supports multiple data structures, providing flexibility in how data is managed.

How Redis Works

Redis operates primarily in memory, making it incredibly fast compared to traditional databases. Here’s a simplified view of how it works:

  1. Data Storage: Data is stored in memory as key-value pairs. For example, you can store session data, configuration settings, or full web pages.
  2. Persistence: Although Redis is an in-memory store, it supports persistence by writing data to disk. This ensures that data isn’t lost in case of a server restart.
  3. Replication: Redis supports master-slave replication, allowing you to create multiple replicas of your data for high availability and load balancing.
  4. Pub/Sub Messaging: Redis can also be used as a message broker with its publish/subscribe messaging system, which is useful for real-time communication between different parts of your application.

Using Redis with Magento 2

Integrating Redis with Magento 2 can significantly improve the performance of your online store. Here’s how Redis can be used in Magento 2:

  • Caching: Redis can be used as a cache backend for Magento 2. By caching frequently accessed data like product information, categories, and configuration settings, Redis reduces the load on your database and speeds up page load times.

  • Session Storage: Redis can also be used to store session data. This is particularly useful for large stores with high traffic, as Redis can handle a large number of simultaneous sessions efficiently.

  • Full Page Cache (FPC): Magento 2 supports Redis for full-page caching, which can drastically reduce server response times and improve the overall user experience.

Configuration Steps:

To use Redis with Magento 2, you’ll need to modify your env.php configuration file. Here’s a basic setup:

'cache' => [
    'frontend' => [
        'default' => [
            'backend' => 'Cm_Cache_Backend_Redis',
            'backend_options' => [
                'server' => '127.0.0.1',
                'port' => '6379',
                'persistent' => '',
                'database' => '0',
                'password' => '',
                'force_standalone' => '0',
                'connect_retries' => '1',
                'read_timeout' => '10',
                'automatic_cleaning_factor' => '0',
                'compress_data' => '1',
                'compress_tags' => '1',
                'compress_threshold' => '20480',
                'compression_lib' => 'gzip',
            ],
        ],
    ],
],
'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'
    ]
]

This configuration sets Redis as the cache backend and session storage for your Magento 2 store.

Verifying Redis Configuration in Magento 2

After configuring Redis with Magento 2, it’s essential to verify that everything is working correctly. Here’s how you can do that:

1. Check Redis Connection

You can use the redis-cli command to ensure that Redis is running and accepting connections:

redis-cli ping

If Redis is working correctly, it should return PONG.

2. Verify Magento 2 Cache Backend

To verify that Magento 2 is using Redis as the cache backend, you can use the Magento CLI:

php bin/magento cache:status

This command will display the status of the cache types. If Redis is configured correctly, the cache types should be active and managed by Redis.

3. Monitor Redis Activity

Redis provides built-in monitoring tools to check the current activity. You can monitor the real-time activity of Redis using:

redis-cli monitor

This will show you the commands that Redis is currently processing. If Magento 2 is actively using Redis, you should see entries related to cache reads and writes.

4. Inspect Redis Data

To inspect the keys stored in Redis by Magento 2, you can use:

redis-cli keys '*'

This command will display all the keys currently stored in Redis. Magento 2-related keys usually have prefixes like mage-cache or sess.

5. Clear Magento Cache and Recheck

Finally, clear Magento’s cache and then monitor Redis to see if new cache entries are created:

php bin/magento cache:clean

After clearing the cache, run redis-cli monitor again to see Redis activity as Magento repopulates the cache.

If all these checks confirm Redis is being utilized, your Magento 2 setup is successfully using Redis.

Installing Redis on Ubuntu

To get started with Redis on your Ubuntu server, you’ll need to install it first. For a detailed guide on how to install Redis on Ubuntu, please refer to this blog post.

Conclusion

Redis is a powerful tool that can significantly enhance the performance and scalability of your Magento 2 store. By leveraging Redis for caching, session storage, and full-page caching, you can provide a faster and more reliable experience for your customers.

Stay tuned for the next part of this series, where we’ll cover the detailed steps on how to install Redis on an Ubuntu server.


Looking for more Magento 2 tips? Check out our other Magento Development Guide.

Updated: