August 11, 2014, 1:45 PM — It sounds delicious, and if you’re implementing a caching strategy on a web application it kind of is. Donut caching is a slang term for the more formal label of substitution caching. So how does it work and why should you care?
There is a mildly famous quote that goes “There are only two hard things in Computer Science: cache invalidation and naming things.” If you’ve written computer software, you’ll relate to that I’m sure. Caching is extremely difficult to get right and there are many different tactics you’ll need to employ to reach your goals.
One of the biggest hurdles when implementing caching on a web application is recognizing content specific to individual user accounts, such as their username and profile photo. If you were to simply cache the entire page, the next visitor would receive the cached page along with the previous user’s name and photo etc. To avoid this, you need to poke holes in the page cache for sections which should remain dynamic. The technical term for this is substitution caching but the more fun term is donut caching.
Donut caching gets its name for two reasons, 1) it’s similar to Do NOT cache; 2) if you picture your web page layout with a doughnut on top of it, the hole in the center would represent what’s not being cached, the solid would be cached.
This strategy allows you to cache the majority of the page which may be common to every user while keeping the user specific stuff outside of the cache. This is also helpful for permissions based features on a page where, depending on your level of access, you see different options. While it’s still possible to cache these user specific areas by varying the cache per user or some custom parameter, this is the gist of donut caching.
On the flip side of things there is something called Donut Hole Caching. This is the exact opposite of donut caching. In donut hole caching, only specific portions of a page are cached while the majority of the other content is left dynamic. This is useful when there are especially resource intensive portions of a page that you want cached but the complexities of the application make it best left dynamic. Using our same image analogy from before, the solid part of the doughnut hole is the cached part while the empty area is dynamic.
This has been a hunger inducing explanation of where these terms came from and what they mean. In a follow up post I’ll discuss some tactics for actually using these types of cache strategies and how you can go about managing their validity.