Loading...
difference between the Varnish and Redis cache in Magento performance?

Difference between the Varnish and Redis cache in Magento performance?

Introduction

In Magento Cache Management, both Redis and Varnish are used to improve your website's performance and scalability. Let's check together their areas of improvement in the Magento website performance.

Redis

Redis Cache is an in-memory data structure store for Page Caching, Session Storage, and Cache Storage. Let's Know about them in detail below.

Page Caching

Page Caching: Redis can be the backend cache for full-page caching, caching the generated HTML output of Magento blocks. Redis enables fast retrieval and decreases the load on the web server. Configure Redis in the Magento 2 env file to allow it for page caching.

Redis Configuration for Magento website optimization till version 2.3.4

php

'cache' => [
   'frontend' => [
       'page_cache' => [
           'backend' => 'Cm_Cache_Backend_Redis',
           'backend_options' => [
               'server' => '127.0.0.1',
               'port' => '6379',
               'database' => '1',
               'compress_data' => '0'
           ]
       ]
   ]
],

php

'cache' => [
   'frontend' => [
       'page_cache' => [
           'backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
           'backend_options' => [
               'server' => '127.0.0.1',
               'port' => '6379',
               'database' => '1',
               'compress_data' => '0'
           ]
       ]
   ]    
],

Redis Configuration for Magento website optimization from version 2.3.5

Session Storage

Session Storage: Using Redis as a session storage backend, you can experience faster read and write operations for session data. Redis helps optimize performance and provides a seamless user experience. Configure the Magento 2 env file to leverage Redis for session storage.


Redis Configuration

php

'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' => '4',
       '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'
   ]
],

Cache Storage

Cache Storage: Magento 2 utilizes a cache system to store different cache types. You can use Redis as the cache storage backend to improve the performance of cache read and write operations. Configure the Magento 2 env file configuration to enable Redis for cache storage.

Redis Configuration for Magento website optimization till version 2.3.4

php

'cache' => [
   'frontend' => [
       'default' => [
           'backend' => 'Cm_Cache_Backend_Redis',
           'backend_options' => [
               'server' => '127.0.0.1',
               'port' => '6379',
               'database' => '1',
               'compress_data' => '0'
           ]
       ]
   ]
],

php

'cache' => [
   'frontend' => [
       'default' => [
           'backend' => '\\Magento\\Framework\\Cache\\Backend\\Redis',
           'backend_options' => [
               'server' => '127.0.0.1',
               'port' => '6379',
               'database' => '1',
               'compress_data' => '0'
           ]
       ]
   ]    
],
 

Redis Configuration for Magento website optimization from version 2.3.5

You can verify that Magento and Redis cache is working together via this command
redis-cli monitor

Varnish cache

Varnish is an HTTP accelerator and reverses the proxy cache to manage incoming HTTP requests on port 80. Varnish cache is primarily used for full-page caching in Magento

When a user requests a page, Varnish checks if it has a cached copy of the page; if it does, it serves the cached version directly without involving Magento or the web server. Varnish significantly reduces the server load and improves the website's performance and scalability.

Full-Page Caching

Varnish can cache the entire HTML output of Magento 2 pages at the network layer, serving cached blocked directly to successive users. Varnish remarkably decreases the load on the web server ( NGINX or APHACHE ) and enhances response times. Set up Varnish in Magento 2 by editing the store settings and setting the Varnish server details at Stores > Configuration > Advanced > System > Full Page Cache.

To deploy the custom Magento VCL, use the command provided below. 

cmd

sudo bin/magento varnish:vcl:generate --export-version=6 | sudo tee /etc/varnish/default.vcl > /dev/null

This command will generate the VCL file, and We will create the output. To /etc/varnish/default.vcl

Edge Side Includes (ESI)

Magento uses ESI to isolate navigation blocks from other dynamic content blocks within a cached page. In Magento 2, you can keep specific blocks as "hole-punched," supplying dynamic rendering within cached pages. Varnish empowers a blend of static and dynamic content, enhancing personalization, minimizing cache invalidation needs, and improving Magento website performance. Varnish currently does not support ESI Blocks over HTTPS and automatically switches to HTTP. ESI works when conditions like Varnish are enabled in Magento and XML layout block elements are added with the TTL attribute.

An example is below

xml

<referenceContainer name="content">
   <block class="Magento\Framework\View\Element\Template" template="Elightwalk_Extension::esi.phtml" ttl="30" />
</referenceContainer>

Varnish cache in Magento, you can achieve faster response times, reduce server load, and provide a better user experience by delivering cached content to users instead of processing each request from scratch.

Conclusion

Using Redis Cache and Varnish Cache jointly can enhance Magento website performance. Redis manages application-level caching (page caching, session storage, and cache storage), and Varnish serves as a powerful reverse caching proxy for Full-Page Caching and Edge Side Includes.

 You can contact us if you want Magento website performance improvement and any query related to Magento website optimization and Magento website caching.

 

Contact us:+91 8128405131

Email send us at hello@elightwalk.com

Bhavisha Rathod