Over a million developers have joined DZone.

Edge Side Includes with Varnish in 10 minutes

· Performance Zone

See Gartner’s latest research on the application performance monitoring landscape and how APM suites are becoming more and more critical to the business, brought to you in partnership with AppDynamics.

Varnish is a tool built to be an intermediate server in the HTTP chain, not an origin one like Apache or IIS. You can outsource caching, logging, zipping and other filters to Varnish, since they are not the main feature of an HTTP server like Apache.

What we'll see today is how to work with Edge Side Includes in Varnish, as a way to compose dynamic pages from independently generated and cached fragments; we won't encounter logging or other features. If you are familiar with PHP, ESI is an (almost) standard for executing include()-like statements on a front end server like Varnish; the proxy is able not only to assembly pages but also to cache them according to different policies: a certain time, for a single user, and so on.

Thijs Feryn and Alessandro Nadalin introduced me to Varnish and ESI respectively, for the first time. I recommend you to consider their blogs and talks as additional sources on these topics.

Installation

The default version of Varnish in Ubuntu 11.04 is instead 2.1, and apparently does not support ESI very much.

Installation via packages means adding a public key and a repository to your list of software sources, and install the varnish package via apt-get or an equivalent command. You can install version 3.0.0 via packages, but only in Ubuntu LTS (10.04).

A way that always works in these cases is the installation from sources. The linked page will list the package dependencies and give you a sequence of 3-4 commands to seamlessly compile varnish. I used checkinstall instead of make install to get a binary package that I can reuse later:

$ sudo checkinstall -D --install=no --fstrans=no --maintainer=youraddress@gmail.com --reset-uids=yes --nodoc --pkgname=varnish --pkgversion=3.0.0 --pkgrelease=201108231000 --arch=i386

After installation with dpkg, check that varnishd is available and of the right version:

[10:18:17][giorgio@Desmond:~]$ varnishd -V
varnishd (varnish-3.0.0 revision 3bd5997)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2011 Varnish Software AS

Varnish needs minimal configuration: a server to point at. For our tests you can edit /etc/varnish/default.vcl and check (or add) the following:

backend default {
    .host = "127.0.0.1";
    .port = "80";
}

You can execute ps -A | grep varnishd at any time to see if varnish is already in execution.

Execution

[09:55:18][giorgio@Desmond:~]$ sudo varnishd -f /etc/varnish/default.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:8080
storage_malloc: max size 1024 MB.
  • 1 gigabyte of memory is allocated for keeping fragments in RAM.
  • An administrative interface will respond on port 2000, and only be accessible from localhost.
  • http://localhost:8080/ is the exposed HTTP server, and will point to http://localhost:80 as defined in the configuration.
Look at man varnishd for more switched and to man vcl for additional explanations on the configuration language.

A bit of ESI

ESI is a technique for leveraging HTTP cache and at the same time build dynamic pages.

The problem with today's pages is that they are highly dynamic: some sections change very often or according to the current user (Welcome, John Doe or the current posts timeline); some sections do not change at all for days (the navigation bar and the layout structure); some sections change in response to external events (the list of incoming messages only when a new message arrives).

It would be ideal to set different caching configurations for all the page's fragments. But implementing this strategy in the application code is error-prone and means reinventing the wheel. To use HTTP cache you will be forced to load with Ajax every single fragment of the page, even a single paragraph.

With ESI, your application produces only the pieces, and lets an implementor of the Edge Side Include specification like Varnish assemble the whole thing.

Example

HTML page (very static):

<p>Varnish will work on this page: <esi:include src="/date.php" />.</p>

PHP page (really dynamic, can change at any time):

<?php
echo date('Y-m-d');

Varnish configuration to add:

sub vcl_fetch {
    set beresp.do_esi = true;
}

if you want something more precise:

sub vcl_fetch {
    if (req.url == "/index.html") {
        set beresp.do_esi = true;
    }
}

You can add settings for the ttl and other options for the different pieces.
The result is neat:

<p>Varnish will work on this page: 2011-08-23.</p>

No sign of Varnish interventions, and totally transparent for the client. And sometimes you can also throw away Zend_Layout and similar components to assemble HTML on the PHP side.

The Performance Zone is brought to you in partnership with AppDynamics.  See Gartner’s latest research on the application performance monitoring landscape and how APM suites are becoming more and more critical to the business.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}