Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Using cache to optimize your website performance

Wednesday, March 4th, 2015 by Servage

cache-timeMost growing web projects will experience performance problems at some point. Usually this can be relieved by optimizing code and workflows. Especially database request are a big concern when it comes to optimizing website performance. These request happen while a script executes and must finish before the visitor can receive the response. This is often a bottleneck due to inefficient programming, and can be highly optimized. There are different approaches to this, and also very different stages during a response generation where caching can be applied. Therefore please take this as inspiration how to improve your site performance.

The example below starts with the most generic caching available, ending up with the most specific and compartmentalized caching methods.

Browser caching

The simplest way of avoiding that the visitor has to wait for a response is by avoiding the visitor making a request in the first place. This can be done by using browser cache. You can define headers to be returned with a gives resource, that instruct the browser to keep the response cached for a specified period. This should make the browser save it internally and use the cached data instead of resubmitting the request again within the defined period. This type of caching is great for very static data like Javascript, Stylesheets and HTML you are certain will rarely change. You can also experiment with short caching periods and then even use it for content that changes more frequently. However, be aware that browser may not necessarily always respect your caching instructions, so you should not completely rely on this caching method alone.

Response caching

Response caching is a good method to accompany browser caching. If the browser uses it’s own cache, then you save the request and transfer time. However, if the browser requests the resource again, then your backend will simply return the response immediately instead of building anything dynamically. This will help avoiding unexpected problems due to browsers not accepting your cache instructions. Response caching will store the entire response HTML.

Partial caching

Partial caching can be a wide area of many different things, it depends on your application. In principle partial caching means that you cache some smaller elements of a response, and render them together with other partials and/or fresh data. Partials are great for combining chunks of data, where maybe just parts of a page have been refreshed, but you want to save performance by reusing older cache parts for other parts of the page. Partial caching is especially useful when saving blocks of HTML.

Query caching

If partial caching is not available or needs to be refreshed, you may still want to use database query caching. Selecting data with complex queries in large databases with numerous conditions are heavy on the database by default, and can often be avoided by caching a specific result. In practice you store the outcome (array, object or similar) from a specific query, and reload that into your script code instead of reloading the result from the database. This can reduce queries to the database and thereby improve performance.

When analyzing website performance you will often find many small queries for information that is rarely changing. For example validating a page id, or checking a language. These kinds of checks need to be made, but if the data rarely changes, you can get rid of many requests to the database by caching them.

No matter which system you use, or how well your performance is, you are likely to benefit from implementing caching, thus increasing overall user experience and satisfaction. Caching is a topic that can be elaborated to great extend, but even some of the simple suggestions above can make a big difference.

Using cache to optimize your website performance, 4.0 out of 5 based on 4 ratings
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.