The vast majority of sites on the web today use content management systems. The benefits are obvious: they remove the need to tangle with code, make it easy for non-technical people to run a site, and usually provide all the extensions you could ever need for adding functionality.
But, for all those benefits, there’s something of a trade-off. Content management systems like Joomla! and WordPress are dynamic site generators. Every time a user requests a page, it is generated on the fly. There are various ways of doing this, but the most common method — and the one used by WordPress — is by executing PHP code and making database requests. From a functionality perspective, that’s incredibly powerful, but the downside is that each stage in generating a page takes time. Compared to static sites, where everything is pre-made, dynamic sites can be slow — and that’s bad for users and for conversions.
There are various ways to make a WordPress or similar site faster, but today I’d like to look at Varnish. Varnish is a caching HTTP reverse proxy, or more simply put, a web accelerators Varnish uses caching to make dynamic websites much faster and take load off the web server application.
If you’re not familiar with caching, don’t worry; it’s easy to understand. A cache is basically a store in which the results of previous page generations are stored. When those pages are requested again, they are returned from the cache, so that they don’t have to be generated again. That means that pages on dynamic sites can be returned as quickly as a static page would be.Varnish is an application that sits between your web server (often Apache, but it works with other servers too) and the Internet. When someone requests a page from your site, Varnish looks at the page being sent to the user, and if it sees an HTTP Cache-Control header, it will store that page in memory for however long the header tells it to. The next time a user requests that page, Varnish sends it the one that is already stored in memory. Your server doesn’t have to generate a new page, significantly cutting down on resource use and the time it takes to get a page to the user’s browser.Varnish has a number of other tricks to make sure that a site is served as quickly as possible. If multiple requests arrive for a particular page that has expired in the cache and needs to be regenerated, Varnish will condense those requests into one request, so that the server does not have to generate the same page many times.
Many of the biggest sites on the web use Varnish to make sure that their web servers don’t die under load spikes, including the New York Times. Installing Varnish is not as easy as just dropping a plugin into your CMS, but if you can handle running a server, you shouldn’t have much problem setting up Varnish with your web server. If you’d like to know more, take a look at the resources below.