Loading...


Related Products


Date Published: October 25, 2023

Introduction to Varnish

This article is based on the Varnish for Beginners talk that Adam Malone presented at DrupalACT.

What is Varnish?

Varnish is a reverse proxy HTTP accelerator that is often placed in front of Drupal websites to cache content, usually for anonymous website visitors. Because Varnish is a separate service, the type of the Drupal site's web server software (such as ApacheNginxComanche, or Internet Information Services (IIS)) doesn't matter - it just works.

If your website has content that a large group of visitors periodically and simultaneously visit, it doesn't make sense to size your server large enough to handle even the largest possible group of website visitors. Instead, you would make your site's content available to an application that can serve copies of content instead (Varnish) of having to re-create each content page dynamically for each website visitor..

Why is Varnish recommended so often for Drupal websites?

Scalability. For each end user accessing the back end of your Drupal site, queries are made between the website and the database, active cache, spam filter, and update service. A web page is then built dynamically and delivered to the user. This system works fine for low-traffic sites, but if you re looking to support the heavy traffic a large audience brings, your server alone will struggle under the weight. Varnish works to take the load off of your server.

There are many advantages to using Varnish, including the following:

  • Serving content from an in-memory cache means no slow PHP execution and no slow MySQL queries.
  • Varnish delivers content at a much higher rate than stock Drupal can.
  • The content and headers are respected entirely from Drupal, so unless something is specifically overridden in the Varnish configuration, Varnish will cache whatever content Drupal directs it to cache.

All of these together make websites behind Varnish fast! Even though the effects are felt only by anonymous users, the majority of traffic for most websites is likely anonymous. Reducing the strain from anonymous users also frees up resources for authenticated users.

Providing alternatives

Although Drupal has several potential caching strategies, it's arguably Varnish that provides the easiest one to set up when balanced against speed benefits:

Note: Acquia Cloud uses Memcached, and does not necessarily support other methods.

  • Drupal's built-in database cache system - A slow system compared to memory; locked to the database
  • Memcached - A great choice that works well with Varnish, although primarily used for lower-level cache (for example, caching bootstrap modules and views)

    Memcached forms a cache backend that Drupal can interact with actively to set and get cached items where Varnish is a passive cache that Drupal does not know about.

  • Redis and MongoDB - Similar to Memcache, they act as an active cache that the Drupal website can set and get items from. Drupal requires additional work to get either Redis or Mongo working, which makes this method more technical to implement.

 

 

 

Configuring Varnish

Acquia Cloud uses Varnish in front of its load balancers. For information, see Using Varnish.

Acquia has configured the shared Varnish installation in a way we believe is optimal for most applications. If you are a Cloud Platform Enterprise customer, and you have dedicated load balancers, custom Varnish configurations may be available through an Acquia Professional Services engagement. See Custom Varnish configuration Cloud Platform Enterprise and contact your Acquia account manager for more information.

Verifying that Varnish is working

Website check

One of the easiest ways to ensure that Varnish is working as expected is to visit the Is Varnish Working website and enter your website's URL. You will get a success or fail message along with your site's headers.

Curl

You can also manually verify Varnish operations. Use the following command at a command prompt:

curl -IXGET http://domain.com

Where domain.com is your site's domain. This returns the headers for the document you request and generally looks like this:

HTTP/1.1 200 OK
Date: Mon, 22 Jan 2018 23:04:51 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d6c58f722d3094e112b801cda1b28ad611516662291; expires=Tue, 22-Jan-19 23:04:51 GMT; path=/; domain=.acquia.com; HttpOnly
Cache-Control: max-age=2764800, public
X-Drupal-Dynamic-Cache: UNCACHEABLE
Link: </node>; rel="canonical"
Link: </node/1>; rel="shortlink"
Link: </node>; rel="revision"
X-UA-Compatible: IE=edge
Content-language: en
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Mon, 22 Jan 2018 14:14:53 GMT
Vary: Cookie,Accept-Encoding
X-Generator: Drupal 8 (https://www.drupal.org)
X-Drupal-Cache: HIT
X-Request-ID: v-9fda8610-ff7e-11e7-95e6-06fa32497e48
X-AH-Environment: prod
X-Varnish: 5054236 5052240
Age: 31794
Via: 1.1 varnish-v4
X-Cache: HIT
X-Cache-Hits: 2750
Server: cloudflare
CF-RAY: 3e162797e45b5dba-BNE
 

The important lines are X-Cache: HIT, which says that it was not a cache MISS, and Age: 31794 because that confirms that the page has been stored in cache for that amount of time.

Developer Tools

In Chrome, you can also use Developer Tools to see the HIT or MISS value.

  1. Open Developer Tools.
  2. Click the Network tab.
  3. Click the resource you want to view, which is usually (but not always) the HTML page.
  4. View the Headers tab.
  5. Look for both the "X-Cache" line to see if it was a HIT or a MISS and the "Age" line to see how old the document is.

What happens when I update my site's content?

Varnish will hold cached pages in its memory until it expires. After this period, the page will be discarded from the cache. The next time this content is requested, it will be created and delivered by the backend, and a new static copy will be stored in the Varnish cache. If necessary, you can manually invalidate cached objects by either purging them from the cache or banning them from being served from the cache. Varnish Software provides training material for these cache invalidation strategies.

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation