Magento L2 Cache Configuration

Magento L2 Cache Configuration

Are slow load times slowing your Magento store? Optimizing Magento L2 cache configuration can enhance store performance and Magento UX. It increases site speed and reduces network congestion. This tutorial covers Magento L2 cache configuration, focusing on real-world examples.

Best Magento Hosting now

Key Takeaways

  • Learn how Magento L2 cache configuration can improve your Magento store's performance.

  • Understand the importance of reducing network congestion and optimizing site speed.

  • Discover the mechanisms behind Magento L2 cache, including remote and local caching strategies.

  • Explore practical examples and configurations for implementing Magento L2 cache setup.

  • Gain insights into the use of the stale_cache_enabled option.

What is Magento L2 Cache?

Magento L2 cache minimizes network congestion and bandwidth usage between microservices. This mechanism's implementation occurs with the

\Magento\Framework\Cache\Backend\RemoteSynchronizedCache class.                                   

Caching reduces network traffic between remote cache storage and the Commerce application.  To reduce network bandwidth to Redis, web nodes store cache data. The remote cache is important to check for the latest version and transfer it when necessary. 

Commerce stores a hashed version of the data in Redis. It appends ‘:hash’ to keys. It ensures local caches stay up-to-date through a cache adapter.

Why Use Magento L2 Cache?

Why Use Magento L2 Cache Configuration?

Reasons Explanations
Reduce network congestion Magento L2 cache minimizes network congestion and bandwidth usage between microservices. It enhances site speed.
Increase site speed By caching data on web nodes, Magento L2 cache boosts site speed and responsiveness. It uses remote cache storage only when necessary.
Optimize network bandwidth Caching data reduces the need for constant communication with remote cache storage. It optimizes network bandwidth and improves data retrieval efficiency.
Enhance cache efficiency Magento L2 cache configuration employs both remote and local caching strategies. It ensures efficient cache usage and maximizes data retrieval performance.
Improve user experience Optimizing Magento L2 cache configuration reduces load times. It also improves site performance, resulting in a better user experience.

Magento L2 Cache Configuration Example

Change or replace the cache section in the app/etc/env.php file as shown in the following example:

'cache' => [
    'frontend' => [
        'default' => [
            'backend' => '\\Magento\\Framework\\Cache\\Backend\\RemoteSynchronizedCache',
            'backend_options' => [
                'remote_backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
                'remote_backend_options' => [
                    'persistent' => 0,
                    'server' => 'localhost',
                    'database' => '0',
                    'port' => '6379',
                    'password' => '',
                    'compress_data' => '1',
                ],
                'local_backend' => 'Cm_Cache_Backend_File',
                'local_backend_options' => [
                    'cache_dir' => '/dev/shm/'
                ]
            ],
            'frontend_options' => [
                'write_control' => false,
            ],
        ]
    ],
    'type' => [
        'default' => ['frontend' => 'default'],
    ],
],

In the example above:

i) backend = L2 cache implementation.

ii) backend_options = L2 cache configuration.

  1. remote_backend = remote cache implementation: Redis or MySQL.

  2. remote_backend_options = remote cache configuration.

  3. local_backend = local cache implementation: Cm_Cache_Backend_File

  4. local_backend_options = local cache configuration.

  5. cache_dir is an option specific to file caching. It  specifies the directory for storing the local cache.

Adobe advises utilizing Redis for remote caching (\Magento\Framework\Cache\Backend\Redis) and Cm_Cache_Backend_File for local data caching in shared memory, with the configuration: 'local_backend_options' => ['cache_dir' => '/dev/shm/']

Adobe also suggests using the cache preload feature to reduce the load on Redis. Remember to append the suffix ':hash' to preload keys.

How to Configure Stale Cache Options

Starting with Commerce 2.4, the use_stale_cache option is available. It can enhance performance by reducing lock waiting times in certain situations. Yet, with an increase in the number of Blocks or Cache, lock waiting times can increase. 

In extreme cases, the Block/Config cache may contain hundreds of keys. Even minor lookup timeouts can cause delays. These delays can last several seconds.

The stale cache is compatible only with the L2 cache. It allows sending an outdated cache when a new one's creation occurs in parallel. To enable stale cache, include 'use_stale_cache' => true in the top config of the L2 cache.

Adobe advises activating the "use_stale_cache" option. You should only use it for cache categories. These categories should be the ones that benefit the most from it, such as:

  1. block_html

  2. config_integration_api

  3. config_integration

  4. full_page

  5. layout

  6. reflection

  7. translate

Adobe recommends not enabling the use_stale_cache setting for the default cache.

Here is an example configuration:

'cache' => [
    'frontend' => [
        'default' => [
            'backend' => '\\Magento\\Framework\\Cache\\Backend\\RemoteSynchronizedCache',
            'backend_options' => [
                'remote_backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
                'remote_backend_options' => [
                    'persistent' => 0,
                    'server' => 'localhost',
                    'database' => '0',
                    'port' => '6379',
                    'password' => '',
                    'compress_data' => '1',
                ],
                'local_backend' => 'Cm_Cache_Backend_File',
                'local_backend_options' => [
                    'cache_dir' => '/dev/shm/'
                ]
            ],
            'frontend_options' => [
                'write_control' => false,
            ],
        ],
         'stale_cache_enabled' => [
            'backend' => '\\Magento\\Framework\\Cache\\Backend\\RemoteSynchronizedCache',
            'backend_options' => [
                'remote_backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
                'remote_backend_options' => [
                    'persistent' => 0,
                    'server' => 'localhost',
                    'database' => '0',
                    'port' => '6379',
                    'password' => '',
                    'compress_data' => '1',
                ],
                'local_backend' => 'Cm_Cache_Backend_File',
                'local_backend_options' => [
                    'cache_dir' => '/dev/shm/'
                ],
                'use_stale_cache' => true,
            ],
            'frontend_options' => [
                'write_control' => false,
            ],
        ]
    ],
    'type' => [
        'default' => ['frontend' => 'default'],
        'layout' => ['frontend' => 'stale_cache_enabled'],
        'block_html' => ['frontend' => 'stale_cache_enabled'],
        'reflection' => ['frontend' => 'stale_cache_enabled'],
        'config_integration' => ['frontend' => 'stale_cache_enabled'],
        'config_integration_api' => ['frontend' => 'stale_cache_enabled'],
        'full_page' => ['frontend' => 'stale_cache_enabled'],
        'translate' => ['frontend' => 'stale_cache_enabled']
    ],
],

FAQs

1. What is the purpose of the stale_cache_enabled configuration?

The stale_cache_enabled configuration aims to enhance site performance. It uses both remote and local caching strategies. This approach serves stale content from the cache while new content generation occurs. As a result, it reduces load times and improves the user experience.


2. How does the remote_backend configuration enhance Magento's caching capabilities?

The remote_backend configuration uses Redis for caching. It offers high availability and persistence capabilities. This setup boosts Magento’s cache efficiency. Dedicated Magento hosting provides a scalable and reliable caching layer. It helps in handling high traffic loads.


3. Why is the local_backend set to Cm_Cache_Backend_File?

The local_backend configuration occurs as Cm_Cache_Backend_File. This configuration uses the file system to store cache data. It serves as a fallback or secondary cache. It allows for quick data retrieval without needing remote cache access. It helps reduce latency and boosts performance.


4. What role does the use_stale_cache option play in cache management?

The use_stale_cache option lets Magento serve cached content, even when it's stale. Users can access content without delay or interruption. Meanwhile, the system updates the cache in the background. It ensures the website stays fast and responsive.


5. Can customization of cache configuration occur for different parts of a Magento site?

Yes, Magento's cache configuration is flexible. Its customization can take place for various site parts. They include layouts, block HTML, and full-page caches. This granularity permits optimized caching strategies for each area. It enhances site performance and efficiency.

Summary

Magento L2 cache configuration boosts store performance by cutting database load and speeding up page loads. Key features include:

  • Implementing both remote and local caching strategies: 

  • Ensuring automatic cache clearing for efficiency

  • Achieving improved page load times for faster access

  • Enhancing overall better user experience through optimization

  • Offering a stale cache option for better data management

Ready to enhance your Magento store's cache performance? Explore managed Magento hosting plans.

CTA

Shivendra Tiwari
Shivendra Tiwari
Technical Writer

Shivendra has over ten years of experience creating compelling content on Magento-related topics. With a focus on the Magento community, he shares valuable tips and up-to-date trends that provide actionable insights.


Get the fastest Magento Hosting! Get Started