Alex Bosworth's Weblog

developing software, living the expat life in beijing, other fun stuff!

previous projects: alchemy, swik, open source stuff, now adylitica.

Incremental Responsive Caching

One tool I’ve fallen in love with over the past year for web development is Memcache. I give credit to Brian Aker for telling me that just depending on MySQL to intelligently cache is a bad idea. Since then I actually have stopped using MySQL but haven’t for a second stopped using Memcache.

I started using Memcache too much really. One negative consequence of using memcache heavily is that you can forget how slowly your pages load with an empty cache, or with just a cold cache.

After my pages started to take a minute to load with an empty cache, I thought what I would do is build a cache builder that runs before the site comes up. Some of that minute is devoted to pulling in data shared with every page load altogether, some with similar pages, and some with just that page. It would be fairly easy to pull in data shared with every page load, but I paused thinking about how I would warm a cache where data was shared only with similar pages.

Thats when I started with the idea of incremental responsive caching. In my pages now I identify parts of the page that are optional but add to page generation time. Tag clouds, related pages, computationally or data heavy stuff.

Now if a page is cold, I only generate as much of those optional elements as I can within a time limit. I also save my work as I go along. The next time the page loads, I take whatever was done last time, and add to it. This also works from the specific to the general: similar page loads will add to similar cache groups, specific page loads will only help the specific page.

I like this strategy because it is responsive to traffic: only cache what needs to be cached, and because it allows pages to load quickly at an acceptable speed after a memcache wipe.

Comments (View)
blog comments powered by Disqus