Aggressive Caching With Memcached
There’s many different methods to use caching, with memcached. I call this one the Aggressive approach.
The basic logic is this: if you don’t even touch the database, you will be able to serve far more requests, than if you did. The biggest downside is, you can’t really do things too dynamically, and really, this only works well for flat pages (ie., it’s not for anything that displays content, based on the user).
In my case, I added the following code (shown as pseudo code) to the main/controller/dispatch script:
$memcache = new Memcache;
$memcache->connect(...);
$data = $memcache->get($_SERVER['REQUEST_URI'])
if ($data) {
echo $data;
die();
}
$db = new PDO(...);
ob_start();
ob_implicit_flush(false);
...
... do whatever ...
...
$data = ob_get_contents();
$memcache->set($_SERVER['REQUEST_URI'], $data, 0, 3600);
ob_end_flush();
One of the biggest downsides to this approach, is figuring out how to delete cached objects when something’s updated. If you have a single table that holds the pages that you’re caching, however, you can just add a hook that deletes the cached object, by URI, when anything is written that would affect it.
This really doesn’t work well for anything super dynamic, but it’s great for increasing performance in areas that would otherwise waste resources.

August 17th, 2008 at 1:47 pm
As you scale up, this approach doesn’t work as well. You end up needing something like a NetScaler, which handles load-balancing and caching.
Depending on your traffic rates, you can enable output caching for anonymous users only and see significant benefits.
You will also see improvement if you cache your data, rather than doing output caching. This shifts the load off the database, at the cost of a little more processing in your app code. You have enough flexibility that you can cache complex data and filter it down fairly easily in your application. For example, you may cache every action by all users on the site, then fetch them and only show ones for the user being viewed.
You’re just a step away from explicit dirtying, which is a good goal. You just keep your data in the cache forever, and remove (or better yet, update) it when needed. With that layout, you basically never have to worry about populating it, and it’s a step towards decoupling things further, putting the DB query / cache logic into a completely different system than the cache fetch / display logic.
August 24th, 2008 at 1:18 pm
There are far more efficient techniques for output caching than hitting memcached (as efficient as it may be). Varnish is a good bet! But I also agree with Ian, object caching is often very beneficial and works just fine in combination.