Using memcached to Cache Content

July 26th, 2011 Leave a comment
Like the article?
Caching Architecture

Web servers in the modern era are expected to do a great deal of processing and serving of all different types of content; the amount and type of content now, in fact, is far greater and more varied than it was even ten years ago. In response to these demands placed on web servers and database servers, caching systems have come into existence in order to speed up performance and help with database and web performance.

memcached is a distributed memory caching system developed for use in LiveJournal’s web and database service. It is now a general-purpose, open caching server for use in any web applications on any Linux server; it stores key and value pairs in RAM, and by doing so greatly speeds up database-driven dynamic implementations.

Thankfully, memcached is available for everyone running a Linux distro, and the technology’s fairly simple to set up and get working on your system. What isn’t always so obvious, however, is how caching can help you: unless you cache smart, it’s possible that you may not see the most improvement you can get from your caching server. Let’s take a run through the process and see how you can get memcached up and running!

1. Setting up memcached

memcached is, thankfully, available on most modern Linux distros through their respective package managers. On most YUM and APT based installs, a simple

yum install memcached

or

apt-get install memcached

will take care of your install woes. (Don’t forget to apt-get or YUM the appropriate php module for Apache as well!) There are, however, a few extra tweaks you’ll want to take care of before you start up and head out into the memcache wild.

First off, you are going to want to edit your rc.local with parameters specifically for your memcache install. Pop this line into your /etc/rc.local file:

memcached -u www-data -d -m 16 -l 127.0.0.1 -p 11211

Replace the variables as befits your installation, most specifically the webserver username and the amount of memory memcached should use. You can also use other means to have your memcached server start on boot, and set the variables in your memcached.conf like so:

#Memory usage
-m 16
# default port
-p 11211
# user
-u www-data
# only listen locally
-l 127.0.0.1

then just have your memcached run using whatever startup script you prefer!

Since we’ve also installed the PHP module for memcached, as noted above, we are also going to have to restart our apache install with:

/etc/init.d/apache restart

or whatever your apache server runs as (httpd, possibly).

2. Using memcached appropriately

The question after this, of course, is: how and when do I use memcached with PHP?

The first thing you have to understand is that memcache is not a magic bullet: installing it does not, by itself, magically alleviate any problems you may be facing in regards to database slowdown. Your current web applications must be re-written and re-designed in order to take advantage of memcache and its ability to access things extremely quickly in RAM.

Before that re-design, however, you have to ask yourself an important question: What needs to be cached? Caching everything in memory is often, except in rare cases, a fruitless and ultimately detrimental way of using a cache. The best way to use a cache is to think about the typical use scenario of your website: what database calls are taking the most time? Some database queries that take a long time can be cached, like searches with COUNT variables. Many administrators will enable debug logging in their web applications and pinpoint what database searches are taking the longest times and which can be optimized to stay in cache and speed up access times.

You can also think in a planning-ahead perspective, and try to determine smart caching decisions that will improve both the user experience and database performance. For example, many web applications developers may choose to have their application cache all the information related to a user’s account when a user logs in; it can be assumed that the user will be working with their information heavily and making frequent database calls, so having the information stored in cache will make the user experience feel much more fluid and increase database performance in general.

Conclusion

Developers and DBAs have long been plagued by the bottleneck of hard disk drive speeds when programming web applications and configuring databases. Along came memcache, a solution designed to alleviate the amount of reads from disk by storing key critical information in memory, making the system faster and more efficient, and memcache is as easy as it is powerful. memcache is, at its very core, just a key and value pair stored in memory; it has no redundancy, libraries, or anything else attached to it, part of what makes it so fast and usable.

Though not a silver bullet on its own, it is a tool that potentially can help a great deal of web administrators and developers shift load off their databases without resorting to hardware upgrades, sharding, or other methods for distributing database load.

We hope this article has shown you the basics of getting a memcache installation up and running as well as given you some use cases for memcache; quite often the technology is there but the difficult part is understanding just what to cache, when, and how!

Help us spread the word!
  • Twitter
  • Facebook
  • LinkedIn
  • Pinterest
  • Delicious
  • DZone
  • Reddit
  • Sphinn
  • StumbleUpon
  • Google Plus
  • RSS
  • Email
  • Print
Don't miss another post! Receive updates via email!

Comment