Share AT

Reading Time: 3 mins

Overview: 

Being reputed for its unparalleled caching technology, Varnish is the most widely used web application accelerator and can also act as a load balancer. Designed exclusively for HTTP Protocol, it is also known as HTTP accelerator and an ideal solution to enhance the performance of content-heavy sites like Magento.

So, how does it work?

Popular E-commerce websites like Magento is comprised of considerably heavy-content and demands an extra-pull during peak times (to say, in a festive and holiday seasons). Usually, when a request from a client is being sent to a browser for the first time, the integrated Varnish cache (placed in the front) stores a copy of the request and retrieves it back (super-fast) whenever the same request hits the browser.

Thus, this mechanism simplifies and accelerates the data-fetching and retrieval process. Also, it helps the website not to depend solely on Server’s efficacy and comes in handy at times of peak hours. In simple words, Varnish as a forward proxy is placed in front of the web server to enhance the website’s speed and thereby, reduce the loading time of the web pages.

What is Magento Full Page Cache?

Basically a full-page cache is a built-in feature of Magento which is used for the quick display of product and CMS pages. One of the long-believed misconceptions is that Magento full-page cache is more than enough for a quick serving of cached requests, but that isn’t the way it works. The Magento full-page cache is designed to use during development mode as the developers are unaware of what is cached and what not. 

Thus, a separate caching technology must be integrated to improve the website’s performance post the development (i.e during the production mode) and especially while at the times of high-traffic days. 

Difference Between Full Page Cache and Varnish:

S.No                     Full-Page Cache                                             Varnish
1. Designed to use for development purposes. Developed to accelerate the web application’s performance during the production mode.
2. Not sufficient enough to boost the website’s performance. A perfect go to cache HTML docs (which is crucial in reducing the  server’s load) and thereby, accelerated site’s performance. 
3. Just a default feature built to tackle concerns during the development phase.   An exclusive entity for caching mechanism and HTTP protocol.

Note: The Magento  itself, recommends using Varnish cache as a web application’s accelerator in the production mode. To know more, https://docs.magento.com/m2/ce/user_guide/system/cache-full-page.html

Enhancing Magento’s Performance with Varnish Cache

Step 1: Installing Varnish

Before proceeding with the optimization of Magento’s performance with Varnish cache, it is important to download all the necessary packages to install Varnish on your Magento site. To know the how-to of downloading required packages and the installation process, visit our blog, Installing Varnish on Ubuntu 18.04

Step 2: Enabling Varnish 

Post the installation step, you have to enable the Varnish cache in the Magento backend.

To enable Varnish, go to your Magento backend and choose:

 Admin panel >> Stores >> Configuration

admin-panel-store-configuration-magento

Now, scroll down to tap System under the Advanced drop-down. 

admin-store-system-configuration-magento

Next, in the System page, tap Varnish Configuration.

admin-store-varnish-configuration-magento

Here, you need to download the varnish file (which goes in accordance with your Varnish version) which will be generated by the time you enable Varnish cache in the back-end.

admin-store-exporting-varnish-file-magento

Step 3: Uploading & Removing

Now, upload the downloaded file called varnish.vcl, into the web root directory:

/var/www/example.com/public_html

Next, you have to either back-up or remove the default.vcl file present in the following directory,

cd /etc/varnish/

To remove or backup the file, use the following commands:

sudo rm -rf default.vcl or sudo mv default.vcl default.vcl_bkp

Step 4: Creating Symlink

Then, within the web root directory, create a Symlink for the downloaded file using the command:

sudo ln -sf /var/www/example.com/public_html/varnish.vcl /etc/varnish/default.vcl

For checking syntax errors, use the command:

sudo varnishd -f /etc/varnish/default.vcl

Step 5: Testing Varnish

To verify the running status of the Varnish at the browser (website) level, look out for the Varnish header as shown in the below image:

testing-varnish-on browser-level-magento

 If you can see the Varnish header, it means that the installed Varnish is running successfully.

Note: 

    • By default, the Varnish headers are hidden in the default.vcl file, if you want to make it visible in the browser,
    • Change  the unset resp.http.X-Varnish;  to #unset resp.http.X-Varnish;

Thus, in this way you can enable and configure Varnish cache for  Magento to enhance your site’s performance and thereby, to reduce the loading time of web pages. The caching mechanism like Varnish comes to a greater use whenever your server goes off the track and at the times of high-traffic days. The above configuration is only applicable for a single domain or single website. To make it accessible to multiple domains, you need to create multiple back end files.