Memcache for Dummies

Creative Commons License teclasorg
I mentioned last week that compromises suck. The first step to a win-win situation for everyone is to grab memcached and get it up and running. And don’t let the title insult your intelligence – it’s more of a warning to those that have decided memcache is just a fad which will soon pass.

Memcached for Debian

I found this installing memcached and the php5 memcache module on debian etch with apache2 fairly straightforward. It recommends you lock down connections (especially if you’re server isn’t running in a DMZ or behind a firewall):

$ sudo vi /etc/memcached.conf
...
-l 10.228.44.203
$ sudo /etc/init.d/memcached restart

Doesn’t get much easier than that – and again, if you haven’t installed memcache for your application what the hell are you waiting for?

Memcached for Mac OS

In order to do some local development and testing, I grabbed the latest libevent (1.4.9) for ./configure , make , sudo make install .
Then grab the latest memcached (1.2.6) again for ./configure , make , sudo make install .
Kick the tires:
$ memcached -h
memcached 1.2.6
-p TCP port number to listen on (default: 11211)
-U UDP port number to listen on (default: 0, off)
...

PHP memcache on the Mac

Now that we got the daemon installed, its time to prep the client. The php memcache module was automatically installed on debian when we installed it above. If you’re developing on a Mac, here’s a quick howto to get your php apps playing nicely with your memcache daemon.

$ cd /Applications/MAMP/bin/php5/bin
$ ./pecl i memcache
...
Build process completed successfully
Installing '/Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20060613/memcache.so'
install ok: channel://pecl.php.net/memcache-2.2.4
configuration option "php_ini" is not set to php.ini location
You should add "extension=memcache.so" to php.ini
$ vi /Applications/MAMP/conf/php5/php.ini
...
extension=memcache.so
$ mv /Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20060613/memcache.so /Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20050922/

Restart your Apache and check out your info page:
memcache installed

Running memcached

Great – so how can you tell if everything is working? Check out A.J. Brown post on how to hook up your Zend based php application to memcached. Once your app is memcache ready, just execute memcached from the cmdline in verbose mode and start clicking through your site:

$ memcached -vv
<10 new client connection
10 END
10 STORED
10 sending key b630e5e130b6bd79679e60dd478545ee
>10 END
<11 new client connection
11 sending key 0efcc343030e0b6ac979aec5c3f3404f
>11 END
...

Here you see in real time the keys being created, stored and retrieved. Neat stuff 🙂

Sure, idling this sexy beast in the driveway is fun, but let’s take it out for a spin. To start it in daemon mode with 32MB of RAM, just execute:

$ ./memcached -d -m 32
$ ps -fA | grep memcache
501 18247 1 0 0:00.00 ?? 0:00.00 ./memcached -d -m 32

Monitoring memcached

Ok, but now what – how can we tell what’s going on beneath the hood? If you haven’t made the necessary logging entry in your code, there are some server side tools to keep an eye on memcached. There are both Cacti and Munin plugins. I even found a pretty nifty Rails based web application for monitoring memcached written by we4tech.

Using a distributed memcached I can finally offer my co-workers instant website updates and my customers a truly performant and scalable application. Again, I’m amazed at how trivial it is to set up such advanced and performant libraries on a variety of operating systems. We really have come into the age of globalized computing and are building upon each other’s work faster than ever before. Please share some of your memcache stories with us!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.