Tuesday, 17 November 2009

Improving Day CQ Performance Using Caching

Recently, we looked at how best to improve performance on one of our CQ4 extranet sites and found that although the page content was being served very fast, a lot of time was being spent by the client downloading various associated resources for that page.  In fact for every page, there were about 40 static resources (png, css, js, etc) that simply didn't change between application releases, yet for every page request the browser would request each of these 40 resources and a 304 Not Modified response returned for each.  Given the fact that each of these requests was laden with SSO, load balancer & CQ session cookies (another problem); the overhead for this was quite great.

In keeping with good practices for speeding up websites, we wanted to set far future Expires headers on these static resources so that once the client populated their cache, there would no more requests for these resources.
The first step towards this was to add some directives to our apache httpd.conf that set the expires headers for us.

This would add an Expires header to each static resource that was requested with a date that was 1 year in the future.

<Location "cq/myapp/static">
ExpiresDefault "access plus 1 year"
</Location>

However, from time to time we make application releases which do change these "static" resources.  For example, any change to the site's CSS design or javascript functionality would need to be represented immediately for users of the site.

To get around this we used the CQ4 mapper functionality to rewrite the resource URLs within the system to include the version number.


This meant changing the /config/delivery/mapper_live_publish.xml file to contain the following mapping:





This adapted the URLs output from CQ templates from
/myapp/static/js/logo.png > /myapp/static/v.1.0.2/js/logo.png

All this means that a client with a populated cache will rarely request any of the static resources of the site unless they change, in which case they are requested immediately.  This saves a tremendous amount of effort for the client browser, and the user experience of the site performance is greatly improved.  These simple steps gave us great performance boost for very little effort.