Drupal Memcache and APC caches compared

Highly scalable Drupal sites require a caching solution more robust than the default database-backed cache. In-memory storage is very popular because of its performance and relatively easy installation, and Memcache and APC seem to be the two most popular options.

Drupal Integration

My site uses the Pressflow build of Drupal 6, which is fully compatible with most Drupal 6 modules. Drupal provides a dedicated Memcache module for Memcache integration. There's also a Cacherouter module which allows use of Memcache and APC backends through a routing system. Cacherouter is not going to be ported to Drupal 7 in favor of modules for specific cache backends, but it's the only reliable way of using APC in Drupal 6. When using Drupal 7, use the dedicated APC module instead.

Server Installation and Administration

While technically easy to do, installing APC and Memcache can have its hangups. Google installation with the name of your version of linux for instructions. You may have to piece a few different tutorials together to get it right. APC consists of a PHP extension, while Memcache consists of both a daemon and a PHP extension. As opposed to APC, Memcache is started up separate from PHP & Apache (or your web server), and can be on another machine entirely. This can be a downside as you have to be aware of starting / restarting Memcache separate from Apache. The upside of the separation is that Memcache can be installed onto its own server and connected to by many web servers simultaneously. APC is tied into your web server making it easier to administer and monitor.

APC comes bundled with a script called apc.php which you can use to monitor usage of PHP. It displays all your configuration variables and a graph detailing the current usage of APC. Getting usage stats from Memcache is a little trickier, requiring you to telnet into your Memcache port and get back usage stats in a raw format. There are a few ports of the apc.php monitoring script for Memcache, but due to their limited usage, their reliability is unproven.

The bundled apc.php script

The bottom line is that Memcache scales better, but APC is easier to administer.

What about using both APC and Memcached?

The APC in this comparison is the APC user cache - this is different from the APC opcode cache which you should always install when using PHP. The user cache allows the arbitrary caching of user data (just like Memcache). Turning on the user cache when you're already using APC as an opcode cache is as simple as adding an additional line to your APC config file or php.ini setting the apc.user_ttl to an expiry time in seconds.

Performance

In my tests, both cache backends performed almost identically. The performed so closely that it's not even worth sharing the results. Compared to the standard Drupal DB backed cache (the "Normal" cache setting in the Drupal Performance menu), both APC and Memcache are lightning fast. I'd recommend them not only for scalability of highly trafficked websites, but also to increase the user-facing page load performance. Storage in memory is much faster than on disk.

Drupal Integration

The Drupal integration of both of these modules is painless. They require you to add additional configuration variables to your site's settings.php file. Below are my configurations for both APC via Cacherouter and Memcache.

Enabling APC via Cacherouter in Settings.php:

$conf['cache_inc'] = './sites/all/modules/contrib/cacherouter/cacherouter.inc';
$conf['cacherouter'] = array(
  'default' => array(
    'engine' => 'apc',
    'static' => FALSE,
    ),
);

Enabling Memcache via Cacherouter in Settings.php:

$conf['cache_inc'] = './sites/all/modules/contrib/memcache/memcache.inc';

Additionally, the Drupal Memcache module allows you to split up your caches into different bins. This allows you to shard parts of your cache over different Memcache servers if needed. Cacherouter has the same routing functionality, but it's not particularly useful because APC runs locally.

Conclusion

For my purposes, Memcache is the better cache for a highly trafficked site. Being able to move Memcache to a separate server or shard the cache over several servers is a great option. For most sites though, traffic is moderate and APC will already be installed, making using the APC user cache a few notches easier.