Caching in the Clouds


When it comes to running content management systems there are always more reads then writes (hopefully) so caching becomes of utmost importance for a successful WordPress install. With a product as mature as WordPress there are tons of options out there, from APC object caching to static HTML file generation to caching with CDN distribution management.

Cloud is Different

Unfortunately lots of the caching options out there were not written with cloud infrastructure in mind and none optimized for WordPress on Heroku. With servers in the clouds we no longer have one beefy monolithic server but rather lots of small ephemeral instances that can go away at any time. This is especially true of Heroku dynos, which come in tiny 512MB (or the 2x at 1GB) sizes.

With these tiny instances we should expect each dyno to generate and cache only what’s needed and share that cache with other dynos for maximum efficiency. In addition, each dyno should not have to coördinate or know the exact state of all cached objects. That way dynos benefit from work done by all the others and we don’t care if any one goes down nor about the cold cache on every new dyno we spin up.

Persistence Layer

To do this we need to move our persistence layer down the stack and make dynos truly independent. Luckily Heroku provides just an add-on to do this. MemCachier is a Memcached as a Service provider, they run a cluster of Memcached servers to which we can rent out a slice and use across all of our dynos.

In order to secure our slice of the Memcache cluster MemCachier uses the SASL protocol to add user authentication. Sadly this is not a common use case as most people run unauthenticated Memcached clusters on private networks instead. Thus most of the tools built do not support SASL secured connections. To make matters worse the default Heroku PHP stack does not even come with the newer SASL enabled Memcached PECL library.

PHPMemcacheSASL & Memcached Object Cache

There is however an open source PHP Memcached client that supports SASL, PHPMemcacheSASL and Memcached Object Cache, a WordPress plugin that allows the object cache to use Memcache as the data store. After adding a couple more methods to PHPMemcacheSASL and rejiggering Memcached Object Cache I’m happy to announce that these two forks now work happily together and I’ve published the SASL-object-cache WordPress plugin to GitHub.

Bonus round, Batcache

Having a persistent, shared WordPress object cache is great but what if you wanted even more speed? Well the fine folks at Automattic (full disclosure, I’m a newly minted Automattician myself,) have developed Batcache, a full-page cache that will use the WordPress object cache backend to store the entire generated page and set the proper cache headers for any reverse proxies you may have. Because it relies on the standard WordPress object cache backend Batcache is fully compatible with SASL-object-cache and it’s an easy way to give Heroku WordPress installs another extra boost.