Use Server-Side Caching When Possible (memcached)

Purely out of necessity, I’ve become a system administrator/architect for digitalpoint.com servers… and a few people have been asking me for general admin tips to make things stable and scalable, so here’s a good one for you…

Use memcached… no really, use it.

memcached is a distributed memory caching system that allows multiple servers to access the same shared memory (you can use it just for single local server too of course). You can cache pretty much anything… SQL query results (especially ones that can’t hit indexes), dynamically generated web pages, dynamic RSS feeds, etc.

If you compile the memcache() functions/class directly into PHP, you have a an easy (yet powerful) way to incorporate it.

For example, let’s say I have some existing code that makes a call to some sort of API (I use memcached for the keyword suggestion tool in this way)… you can add a couple lines of code to cache the results.

[code=php]$data = @file_get_contents (“//some_api_call_url?keywords=$keywords”);[/code]

could become this:

[code=php]$memcache = new Memcache;
$memcache->addServer(‘192.168.1.1’);
$memcache->addServer(‘192.168.1.2’);
if (!$data = $memcache->get($keywords)) {
$data = @file_get_contents (“//some_api_call_url?keywords=$keywords”);
$memcache->set($keywords, $data, false, 86400); // 24 hour cache
}
$memcache->close();[/code]

Now we are only hitting the API for the same keywords once every 24 hours… that setup utilizes 2 memcached servers working as a single entity for pooling and failover.

I use memcache for all sorts of things (this blog’s pages are cached for 60 seconds as another example because WordPress sucks, and protects against a massive influx of traffic [front-page digg for example]).

Here’s some things I personally use it for…

  • Caching this blog’s pages (for 60 seconds)
  • Caching Keyword Suggestion Tool queries (for 7 days)
  • Caching AdSense Sandbox queries (for 24 hours)
  • Temporary counters in Keyword Tracker (using decrement() for displaying remaining keywords on a “Check All”)
  • Recent forum topics that show on all the webmaster tools
  • vBulletin forum datastore

Any high-traffic site that is dynamically generated (especially if it includes SQL queries) could benefit from it… you could manage which memcache items need to be updated when you insert/update to the database, or an easy way to do it is just check the cache for the pages that potentially have high traffic… if they aren’t in the cache, put them in the cache with a certain expiration date. Then the page is dynamically generated no more often than the cache expiration time (in the case of this blog, the pages are generated no more than once every 60 seconds).

P.S. – Make sure you block outside access to your memcached port since there’s no authentication used!

9 thoughts on “Use Server-Side Caching When Possible (memcached)”

  1. We have been using memcached on Article Dashboard for about a month now and it really is awesome. My favorite feature is the ram access from all servers in the cluster. I won’t go into details about the performance increases but it was nothing short of amazing!

    -Patrick

    P.S. Here is a great article from Brad Fitzpatrick about using memcached on livejournal. http://www.linuxjournal.com/article/7451

  2. Hi Shawn,

    Just wondering if it was possible for you to share the WordPress cache that you created? I’d love to cache my wordpress pages for 60 seconds (or even longer 🙂 ). Thanks for your vBulletin Stats plugin for Memcached by the way. Very nice to have that information in a place I visit daily!

Leave a Reply

Your email address will not be published. Required fields are marked *