What is memcached?
memcached is a lightweight cacheing program that allows you to store objects and items in an easily accessed format. The memcached server stores items in a binary format so that it's easy to get these items back. The memcached server is a very light-weight high-availabilty cache system. For you coldfusion heads out there, you can think of memcached as sort of a large struct in ram that holds your stores whatever you throw at it (with some exceptions). Access is quick and non-locking (so there's no waiting around to get stuff if you have several clients hitting it at one time). The server is a stand alone server that you can get to with a client library which zips info up and sends it to the server for you. Not only that, but the memcached server can be put up wherever you have spare ram that's sitting around unused. You can have several memcached servers sitting around and based on a specific algorithm, the memcached client will go out and fetch your stored items from it's servers, however many there are.
Wait... caching? coldfusion already has that... why bother?
True, coldfusion already has caching. it'll cache queries and it'll cache whatever you want in the application and server scopes. in fact you might not get much out of memcached unless you are trying to run a high-page hit site or are trying to cache a ton of data. For example, coldfusion runs on java under a java instance. there are limitations to the amount of memory you can safely use in coldfusion without running into problems.
These are underlying java limitations for the most part, because java intances can only have up to 2 gigs (if you are running cf 7) and i think 4 gigs if you are running cf 8 and remember, coldfusion takes up some of that ram for itself (about 100 mb). I had the opportunity of working at a company where their instance of coldfusion was having a hard time staying up because it was stuffed with too much cached information. The more info you have, the more coldfusion has to keep track of it, and the more your garbage collection has to run on that java instance (to clear out new space) and garbage collection running over 500 mb is faster than running across 2-4 gigs.
What memcached allows you to do is to offload that garbage collecting to a separate process and possibly into other servers if needed. It also allows you to off-load memory allocation costs to other machines. For example, if you have a large coldfusion machine with 10 gigs of ram and you are only running 1 instance of coldfusion, that's potentially 4-8 more gigs of ram that is sitting around unused.
Another nice thing about memcached is that if you have to restart coldfusion, you don't have to reload all those cached items. As long as the memcached server stays up, you can access those items.
Sure memcached sounds good, but aren't there some drawbacks?
Yup, there are some drawbacks to using memcached, like 1. using memcached takes a little more code to use than to store stuff in coldfusion. In coldfusion you can do this:
<cfquery name="mytestquery" datasource="mydatasource" cachedwithin="#createtimespan(1,0,0,0)#">
.... query here
</cfquery>
and you're done.. however, to use memcached, it takes a bit more code:
if ( variables.memcached.keyExists("mykey") {
mytestquery = variables.memcached.get("mykey");
} else {
<cfquery name="mytestquery" datasource="mydatasource">
.... query here
</cfquery>
variables.memcached.store("mykey",mytestquery,3600);
}
All in all, it's a little more code, but not a ton. It does get more involved if you are storing the query based off of parameters. Coldfusion automagically saves and updates stored queries based on the query parameters, whereas you have to manage that in memcached. It's not tragic, but its a bit more work. However, one benefit that you gain is that you can access the stored queries and purge the cached items whenever you want. You are able to do that in coldfusion as well, but you have more control over it with memcached.
That all sounds nice, where can i find it?
You can find the coldfusion memcached client here:
cfmemcached.riaforge.org
You can get the memcached server here:
www.danga.com/memcached