Thursday, June 26, 2008

Memcached - Boost Your Site Performance

I recently discovered a wonderful little server tool called memcached. I believe it stands for "memory cache daemon". For sites/servers that are heavily dependent on dynamic data (ie - databases), it can be a life saver. Over the years I have upgraded my server every couple years for more processors and more RAM, but it never seemed to be enough for my growing traffic. Memcached seems to be the solution I have been looking for that will allow my site(s) to scale up in traffic without needing to scale up in servers (necessarily). It works as a high-speed cache that you can pretty much dump whatever you like into. For me, I use it to cache my most popular pages. Having my site pull from memory versus the database saved me tons in regards to performance and server load.

How it works: You install the memcached server component on your webserver. Make sure you have plenty of RAM. I added an extra 2GB to my box for a total of 4GB just to accommodate memcache. Memcache runs as a background daemon. Once installed, you can launch it via the following command:

memcached -d -u nobody -m 1024 127.0.0.1 -p 11211

This starts the memcached daemon (-d) under no specific user account. It also allocates up to 1GB (1024) of RAM specifically for memcached. Lastly I tell it to run as a localhost on port 11211 (default).

I added this command line to my /etc/rc.d/rc.local file so it would run whenever my box is re-started.

If you have problems with the install, check out this handy install guide. It is important to note that you can run as many instances of the memcached daemon as you like, on as many machines as you like for maximum scalability. I went with the same machine install for simplicity since I had so much extra RAM on my webserver.

Next step, the API: Once you get the server component up and running, you need to evaluate your website code. Ideally you want to use memcache to cache your most frequently visited pages/queries. I used it to store my most visited pages. In a nutshell, you need to change your code to first always check the cache (using the API 'get" method) for the page/query you are about to show, and if you finds it (a hit), then simply use the cache instead of going to the database. If you don't find it, add it to the cache, using the API "set" method.

An important aspect of using memcache properly involves managing changes to your pages. I added some logic to monitor for changes and update the cache whenever a page was modified (ie - a new comment added, etc).

That is basically it! Once you have your code modified, your site can now enjoy the wonderful benefit of high-speed memory caching! Memcached is used by sites like YouTube, New York Times, SlashDot, Twitter and more. It is THE way of scaling your server and site for massive loads of traffic.

For more information on memcached, check out Danga's memcached resource pages.

No comments: