Varnish Caching

Forums: 

 


What is Caching?

Drupal is a content management system which stores all the content and most of the configurations in a database. When someone visits a page of a website running in Drupal content management system, Drupal makes a query to the database and generates the page on the fly. Most of these queries require accessing multiple tables in the database. As you can imagine, when the traffic is more, there will be many requests to the database and can potentially slow down the website's response or make it unresponsive in some cases. To avoid this overload on database, Drupal provides a mechanism to cache these pages. Drupal generates the pages once and stores them in a different easy to access to tables. This cached version is then served to future queries for a period of time. We can configure the duration these pages can serve an older cache before needing to re-cache.

What is Varnish?

Varnish is a reverse proxy server that provides a caching service on top of any website. Varnish servers stores a copy of webpages for a pre-defined duration. Being an external server, Varnish can only cache those pages that are visible to public. When someone tries to access your website, they are first sent to the Varnish server which then either serves the previously stored copy of that page or creates a new copy depending on if the pre-defined duration for the stored copy has expired or not.

Why is Varnish required for Drupal site?

Although Drupal provides its own caching system, as explained above, the cached versions are still stored in the same database within the same infrastructure. If the database is experiencing issues then Drupal's cache is subjected to those issues as well. Varnish on the other hand is external to the Drupal infrastructure. It will store the cached versions outside the Drupal environment and serve those pages from there. Varnish works only if Drupal caching is enabled.

Can I control Caching?

Yes, if you are full admin to your Drupal site, these settings are available under Configuration --> Development --> Performance.

A good explanation of the settings on this page is at https://pantheon.io/docs/articles/drupal/drupal-performance-and-caching-...

Can I control Varnish Caching?

Yes, Varnish works based on the Drupal caching settings explained in the above question.

Can I switch off Caching?

Yes, though we strongly recommend against it.

Are files stored in Database?

The PDFs, document, image and other files linked in the Drupal websites are not stored in the database. These are served directly by the web server.

Are files cached?

Drupal's caching system does not cache files because the files are served directly by the web server. Varnish caching system caches the publicly visible files for the duration defined in the Drupal's cache settings. Most web browsers cache files locally on the end-users systems as well. This cannot be controlled on the Drupal level.