Experimenting with Memcached and Batcache

batmobile

WordPress is often accused of being slow, and you can definitely get it to grind to a halt if you try. But in true WordPress fashion, it also comes with interesting building blocks that helps speed it up to all the way to ludicrous speed. WordPress is like Lego, and I’ve been playing with a couple of interesting new bricks that promise to seriously improve performance.

Memcached

Memcached is an in-memory caching system that can store all sorts of data without the need to write to disk. It’s not WordPress-specific, but can be used as an object cache with WordPress. This means that “expensive” bit of code (which take long to generate) are stored in memory, and reused if possible. Memcached can create a unified cache spanning multiple servers, and is incredibly easy to set up.

sudo apt-get install memcached php5-memcache

On Debian Linux, its configuration options are stored in /etc/memcached.conf. It’s pretty self-explanatory, but there are two important options:

-m 64

This options sets the amount of memory that is used for caching. I set this to 128 MB (double the deafult), but found that my site uses far less than that. 64 MB probably is a pretty good starting point (also, it’s the minimum value).

-l 127.0.0.1

This limits connections to localhost for security. Memcached was designed to be used on server clusters, but if you’re running a single instance on your web server, only localhost (IP addres 127.0.0.1) will need to be able to connect. Without this setting, literally anyone will be able to connect to, read data from and write data to your Memcached server!

WordPress Memcached backend

WordPress comes with a built-in caching framework that revolves around two optional files. Ryan Boren’s Memcached backend 2.0 supplies one of those, object-cache.php. Once placed in wp-content, it overrides WP’s default object caching in favor of Memcached. Memcached servers are configured towards the end of the file, but ‘localhost’ is the default. By itself, this should already speed up WordPress, but there’s more.

Batcache

I’ve used WordPress caching plugins like WP Super Cache and W3 Total Cache on this blog. I prefer the former for its easy setup, and both can actually use memcached. Batcache takes a completely different approach, and I think it’s very elegant.

Once you have memcached set up, installing Batcache is as easy as copying another file to wp-content (plus one that’s optional). This file, advanced-cache.php uses the Memcached backend to store rendered pages in memory. What’s really interesting is that it only does that for pages that get a certain amount of visits within a certain timeframe. This means that only popular pages are cached. It uses a relatively short default expiration time for cached files, which helps keep your blog fresh.

Adaptive caching

Unlike with other page caching solutions, the long tail (usually older, less frequently visited posts) is not cached. In most cases, cashing these file will not help, because cached files are not served again before expiring. Batcache adaptively caches those pages on your site that are receiving traffic. To do this, it needs PHP, so it’s probably not as fast as a fully optimized WP Super Cache setup. Then again, Batcache is very easy to set up, and used on sites like wordpress.com, which probably means it’s not slow either.

I’ve set up this site with Memcached and Batcache for now, and initial results are promising. Load are similar to my previous WP Super Cache setup, and the site looks fine to me (although probably overdue for a redesign). Please let me know if you spot anything out of the ordinary.

1 Comment

  1. Great post! Thank you for that. I also can recommend the plugin “WP-Optimize”, which increases the speed as well.

    Reply Comment by Clemens — January 6, 2014 @ 11:37 am

Leave a Reply