WordPress and Caching

One of the things I really love about WordPress is its always-fresh style of pulling pages directly from the database rather than generating hundreds or thousands of static pages. It makes publishing much quicker - you're just adding a new row to the posts table - since there is no "Publishing Pages" stage, as in MovableType etc...

However, that is also the Achilles heal of WordPress - works fine as long as the load is miniscule, but if you get Slashdotted, or have a bunch of simultaneous views, the database can bog down pretty dramatically. Usually, that's not a problem, but the threat is there.

There is a simple solution, however. The WP-Cache 2.0 plugin - it watches pages as they're being requested, and caches the output to a static file on the fly. If a page has already been cached, it just returns that, rather than crunching through the database each time. Makes request handling much faster. Cached pages automatically timeout after 1 hour, and I think cached pages may automatically invalidate if the database is modified. Very slick. Cached pages have only a trivial amount of overhead over traditional static pages, so load handling should scale up much more smoothly than with strictly dynamic pages.

As an example - the main page for this blog takes 87 database queries to generate. I'm sure it would be possible to optimize that, but with the plugins that I like to use, it takes a fair number of queries to generate a full page. A cached version of the same page takes 0 queries, freeing up the database and CPU to more readily handle the fewer uncached dynamic pages that are requested. Already, I've noticed the generation time of the main page has dropped significantly, I'm assuming because of lower overall load. Previously, it could have taken close to 3 seconds to generate the page (slow, but tolerable). Now, it's hovering around 500ms for a dynamic generation, and even that is skipped if it's cached.

I've been running it here for a couple of days to see if there were any wrinkles of hiccoughs, and I haven't been able to find any. If you look at the source for any page on this blog, scroll waaaay down to the bottom to see if the page was cached or not. That's the only way to tell. Well, that, and it should be coming at you faster than before.

See Also

comments powered by Disqus