6

I have a question about using Varnish along with the Drupal page cache. I was wondering if using both of them is a bit redundant, since Varnish already caches pages for anonymous users?

Currently I have page cache off, page cache max-age zero and cache lifetime set to 10 minutes.

Am I reasoning this right or am I just missing something obvious?

Thanks.

4
  • 1
    Are you using Varnish HTTP Accelerator Integration? If so, it should take care. But basically you are right, you only need one page cache, be it Varnish, Boost, built-in or any other. By the way, with "page cache" set to "off", your settings to "page cache max-age" are ignored, so no need to post them. On the other hand, "minimum cache lifetime" applies to blocks too, so don't make it to short if your blocks are long lived.
    – Mołot
    Jul 16, 2013 at 13:58
  • Some people use Varnish together with APC (Advanced PHP caching) and Memcached. See stackoverflow.com/questions/7353756/… for more.
    – Beebee
    Jul 16, 2013 at 14:10
  • I don't use the Varnish HTTP Accelerator Integration module. Should I be doing that? I use the Purge and Expire modules.
    – dicix
    Jul 16, 2013 at 14:26
  • First, use @Mołot if you want me notified you are talking to me ;) Second - dunno about "should" - it tries to prevent cache purges when they are not needed and forces them when they are, and supposedly takes care about Drupal's internal cache (but I'm not sure about that). Using memory sessions and opcode cache is good idea, too, to optimize logged-in users experience ;) I don't have quite enough experience to post an actual answer, sorry.
    – Mołot
    Jul 16, 2013 at 14:48

4 Answers 4

4

As answered by @dicix, Drupal will not return the correct Cache-Control headers to Varnish without "Page Cache" being enabled in the Drupal Performance configuration.

The problem I ran into with having both the Drupal Page Cache and Varnish at the same time is that it potentially increases the delay before stale content is refreshed to a maximum of both TTLs. For example, having a max-age of 5min could result in the stale content sticking around and being served for up to 10 minutes (or 15 minutes if Varnish doesn't reset the cache-control header given to the browser to something less).

My solution was to set "Cache pages for anonymous users" to be on in the Drupal Performance settings with a reasonable "Expiration of cached pages" of 5min. I then added the following to my settings.php to prevent Drupal from actually caching any page content:

// Use the fake cache for pages since we are caching anonymous HTML with Varnish
if (!class_exists('DrupalFakeCache')) {
  $conf['cache_backends'][] = 'includes/cache-install.inc';
}
$conf['cache_class_cache_page'] = 'DrupalFakeCache';

The DrupalFakeCache just provides stubs that don't store anything or return anything.

Reference: Suppress caching (for development) or to use an external page cache

3

Good point Jimajamma, but my question was different.

Apparently I got the answer for this, if case someone else is interested.

Varnish will not cache pages unless both "Page Cache" is turned on and a "Page cache max-age" is set. Drupal will not set the correct caching headers if "Page Cache" is not enabled. "Cache lifetime" and "Block Cache" does not affect Varnish caching.

Basically Drupal sets the cache-control headers. If Drupal is told not to cache pages, then it tells everything else (Varnish, Akamai, etc) not to cache either.

Varnish can be configured to bypass this feature, but it doesn't do it by default.

Hope this helps someone.

3
  • Are you talking about situation with or without Varnish HTTP Accelerator Integration module?
    – Mołot
    Jul 18, 2013 at 13:17
  • I'm talking about the situation without Varnish HTTP Accelerator Integration module, but with a real Varnish server configured in front of my Drupal website. I just want to avoid the drupal cache completely. It adds unnecessary info to the database. I still have block cache on.
    – dicix
    Jul 19, 2013 at 8:10
  • Well, integration module still needs real varnish. All it takes care is allowing varnish to cache without leaving Drupal's one on.
    – Mołot
    Jul 19, 2013 at 8:43
1

A number of sites are set up with this double caching, especially if a Varnish server is front ending/edging multiple sites AND the cache lifetime on the Varnish end is less than the cache lifetime on the drupal end.

Example.

You set up files to have a cache lifetime of 10 minutes on your drupal site, but you set up Varnish to have a cache lifetime of 5 minutes.

As anonymous users hit your website, Varnish serves all pages up to that 5 minute mark, but then allows drupal to come into play for any not there, or that have expired.

If expired or otherwise not in Varnish, and they are less than 10 minutes old, drupal's cache serves them up quickly and Varnish then does the same for the next 5 minutes.

While this might seem odd, it allows Varnish to keep a "hotter" cache, and since its in memory, you really want it only to keep stuff that is being hit often, so by keeping it's cache lifetime lower, odd ball pages aren't kept cluttering it up (especially if as mentioned it is serving multiple sites) yet are somewhat readily available on the drupal end in its most probably larger disk based cache.

1

You need to enable the page cache, so Drupal sets the correct caching headers.

But this doesn't mean Drupal will store the page in its own cache. If you use the Varnish module, you will have this in your settings.php:

$conf['cache_class_cache_page'] = 'VarnishCache';

which prevents the page from being written to Drupal's default page cache, the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.