Dave Johnson on open web technologies, social software and Java
In my last post, I explained the details of Roller cache configuration and I mentioned that Roller's caching system is pluggable but I didn't explain what that really buys you. Basically, what it means is that you can replace Roller's in-memory LRU caching mechanism with something else. In this blog entry, I'll explain how to do that and specifically, how to replace Roller's built-in cache with the popular Memcached caching system via the Roller Memcached plugin, which, like the rest of the Roller caching system was written by Allen Gilliland for use on blogs.sun.com.
First, what is Memcached? Here's what the Memcached web site says:
Lots of big sites use Memcached for distributed caching. In addition to LiveJournal, there's also Wikipedia, Slashdot, SourceForge and our very own blogs.sun,com. If you're wanting to use Memcached, then you're in good company.
Why would you want to use Memcached with Roller? Well, if you are running a large Roller site and you've got multiple instances of Roller running then you might run into problems with cache consistency. A blog update that hits server A will update the cache on server A, but server B will still have the old blog data for some time. Using a distributed cache will solve this problem. Another, hopefully much less common, reason for wanting to use Memcache is to avoid using the JVM heap -- some folks have experienced some weird problems with the JVM heap management and might want to use plain old C memory management instead via Memcached.
Download and install Memcached
The first thing you need to do is to get yourself a copy of Memcached. You can get downloads and docs on the Memcached web site. You can build it yourself if you're a manly man or a womanly woman, but there are some much easier options.
For example, on Ubuntu or Debian, you can install Memcached with one simple command:
apt-get install memcached
Or, if you're on Solaris and you're a Blastwave user, then you can do this:
pkg-get install memcached
And, of course, you'll have to install Memcached on each machine on which you want to act as a cache server.
Setup your Memcached caches
Now that you've got Memcached installed, you need to start it up.
For example, let's say you want to have two 2GB caches running on your two machines with IP addresses 10.0.0.40 and 10.0.0.41. One one maching you'd run this command to start the Memcached daemon with 2048M RAM and listening on port 11211.
$ ./memcached -d -m 2048 -l 10.0.0.40 -p 11211And on your other machine 10.0.0.41, you'd start Memcached with this command:
$ ./memcached -d -m 2048 -l 10.0.0.41 -p 11211
And, you'll almost certainly want to set things up so that your Memcached daemons start up whenever your servers start up. On Solaris or Linux that means adding things under the /etc/init.d directory. You'll have to figure that out for yourself.
Download and install the Roller Memcached plugin
Next, you need to get yourself a copy of the Roller Memcached plugin and install it into Roller. You can get it from the Roller Support project. Go to the project's file download page and navigate to Roller Support 4.0 / Plugins, or just use the link below:
Unzip that file and add the two jars within to your Roller install's WEB-INF/lib directory. Assuming that the Roller context is in the directory $ROLLER_HOME, you might do that like so:
$ tar xzvf roller-memcached-4.0.tar.gz $ cp roller-memcached/* $ROLLER_HOME/WEB-INF/lib
Configure the Memcached Plugin and Roller caching
The final step is to configure Roller to use Memcached for caching. To do that, you simply add some properties to your roller-custom.properties override file. To use Memcached for all of your caches, set the following property:
If you don't want to use Memcached for all of your caches, then configure it for just the caches you want. Refer to my previous post about Roller caching for details, but basically you just set the factory for the cache to use the Memcached cache factory. For example, to use Memcached for caching feeds only, you'd add this instead of the above:
You also need to tell the Roller Memcached plugin how to find the Memcached servers. To continue with the example we started above with servers 10.0.0.40 and 10.0.0.41, you could add this to set the default Memcached servers for all caches:
You can also set the servers to be used for specific caches . For example, here we set the weblog feed cache to use two different Memcached servers from that used by the weblog page cache:
cache.memcached.weblogpage.servers=10.0.0.40:11211, 10.0.0.41:11211 cache.memcached.weblogfeed.servers=10.0.0.50:11211, 10.0.0.51:11211
Fire it up!
Now you're ready to start up Roller. You might want to add some debug logging the first time around just to make sure things are working. Roller uses Log4j caching and you can add the Log4j properties directly to your roller-custom.properties file. For example, to enable debug caching for the Roller Memcached plugin you'd add this:
And that's that. Let me know via comments if I've left something out or gotten something wrong. I'd also like to hear if you've had some success with Roller and Memcached.