Some common caching techniques for dynamic websites

April 13, 2011

I'm not a deep expert on this field, so I'm not going to claim that this is a complete taxonomy of how dynamic sites implement caches. These are just the three sorts of caches that I've seen mentioned fairly frequently. As it happens, DWiki uses all three so I can give examples.

Query caches cache the results of expensive database queries and other lookups that are (hopefully) frequently used. DWiki uses a query cache to save the result of the filesystem walk it does to determine the order of entries (which is in turn used for things like the 'next' and 'previous' links on entries, the Atom syndication feeds, and so on).

Fragment caches cache the results of generating fragments of the page or, in general, the output; for example, a blog with a 'N most recent comments' sidebar might cache the rendered version of the sidebar since it's potentially expensive to create while being the same on all pages. DWiki uses a fragment cache to cache the rendered version of the content text of individual entries, for various reasons (including that converting things from DWikiText to HTML is one of the more expensive operations in DWiki).

Page caches cache whole pages (or URLs in general), and thus they shed the most load on a cache hit; an active page cache effectively turns your website into something close to a statically rendered site. DWiki uses a very simple brute force page cache when under sufficient load.

(For more details on DWiki's caching, see here.)

The simplest way for a dynamic website to handle cache validation is to not bother; you simply declare that it's acceptable to return stale data or an out of date page for N seconds (or in extreme cases, N minutes) and then do time-based cache expiry. This tends to be perfectly fine for things like blogs, which change rarely and where comments being delayed may be seen as a feature. DWiki uses this approach for its page cache.

(Such a simple cache will not make your site faster in general but it will make it faster under load, or at least under the right sort of load.)

Query caches and page caches are often quite easy to add to a dynamic website; for a query cache you just have to wrap a few of your function calls, and page caches are so simple that they can often be added by external programs.

Written on 13 April 2011.
« You don't need to bake your site to static files to be fast
More on baking websites to static files and speed »

Page tools: View Source, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Apr 13 00:09:38 2011
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.