How to Realise Performance Gains With Multi-Tiered Caching Systems
Caching systems are one method to increase performance and to gain the most benefit, you must carefully construct a multi-tiered cachcing system which will enable you to realise a significant performance gain.
When we say cache, we mean using a server-side caching approach as opposed to client-side caching. There are various methods to achieve client-side caching, including:
- Employing Memcache to store key/value pairs
- Storing information to disk
- Caching it to a DB
Whether it is stored on the hard disk or in memory, a cache is a temporary store of content with the aim of being retrieved in a faster manner than generating them in the first place.
The Multi-Tiered Approach
A multi-tiered approach seeks the create discrete blocks of cached content which can be cached and retrieved separately from one another, but to stack your layers of caching in such a way that should an element change, you just have to regenerate the affected values and not the entire cache.
A typical multi-tiered approach would be:-
- Cache individual database queries – each query should be cached to save load on the database
- 1st level Cache page fragments – individual chunks of HTML can be cached
- 2nd level page fragment – by caching several HTML chunks together, you can help avoid regenerating them again
- Page level caching – the last level of caching should be page level caching – allowing you to cache the HTML for the entire page in one chunk
An Illustrated Example:
1st Level Page level Fragments
As you can see from below, the different coloured areas indicate which parts of the page are cached as the 1st level cache.
2nd Level Page Level Fragments
The 2nd level page fragments combine several of the other page fragments which help provide us with a speed improvement.
Page Level Cache
The last level is the page level cache – we cache the complete HTML for the page as shown below.
Getting the Most From Page Fragment Caching
When choosing to break up your page into page chunks, be sure to cache those sections of logical HTML code which take the longest to generate – for example, if your generating table rows over and over again, caching the table in its entirety can save the expensive operation time and time again (provided the data doesn’t change to regularly). Dynamic content is typically the best candidates for caching – whilst caching larger sections of static code can also help save response times.
If your page includes frequent user-triggered updates such as a shopping cart, then caching the page level can only really be done by caching it on a user by user level – in effect, each user will have their own cache entry for frequently used pages (this should be driven by analytic data to figure out which ones to cache and which ones to ignore).
If you have any comments or questions, please leave them below.
There are no comments currently for this post!