One ColdFusion 9 feature I haven't heard much buzz about but I think has the potential to really enhance high performance and large scale ColdFusion applications is caching. ColdFusion has always had caching capability, but more often than not they've been black boxed, giving the developer limited control and visibility over the process. All that changes in ColdFusion 9 with a major overhaul of the cfcache tag. The biggest single enhancement here is the implementation of the popular distributed caching provider Ehcache under the covers. What this means is that ColdFusion now implements one of the most popular and certainly one of the fastest caching mechanisms available for Java.

Before I get too deeply into configuration and code, I want to take a little time to talk about caching theory, strategy, and patterns. Ehcache changes the caching game in ColdFusion, and a lot of the knowledge we have as ColdFusion developers about caching is no longer relevant. Some of it in fact is just plain problematic, and I hope to shed some light on those issues and talk about how Ehcache helps solve those problems as well as gotchas to look out for when implementing large caching systems.

Just so that we're all on the same page, let's start with a definition of caching as found on Wikipedia:

"...a collection of data duplicating original values stored elsewhere or computed earlier, where the original data is expensive to fetch (owing to longer access time) or to compute, compared to the cost of reading the cache."

There's two important concepts here. First is that cached data is duplicate data. The second is that we're going to duplicate it where it would otherwise be expensive to compute it or fetch it relative to how quickly it can be grabbed from the cache. Keep these two things in mind as we continue through this post.

When a lot of people talk about caching, they talk about it in terms of performance. You may want to cache a particular web page because it's slow to load, or perhaps you want to cache the stats shown on a particular page because it takes a long time to run the query that crunches the numbers you're going to display. These are both valid cases where using cached data can speed up the performance of your application. What I find to be a more compelling use case, though, is caching for scalability. What I mean by caching for scalability is using cached data to reduce the load on critical resources such as the database, app server, web server, network, or client. At each of these phases there's an opportunity to use cached data to allow you to do more with less. What's really cool here is that a byproduct of caching for scalability tends to be increased application performance.

Let's look at an example involving the database. Say for example your database is capable of handling 100 requests per second. Now what if you need to be able to handle more requests? One option would be to throw more hardware at the problem - increase the amount of memory available to the server, add more processors, or maybe even add a 2nd or 3rd database server to cluster and distribute the load. That's certainly one option, but it's also expensive and potentially complicated to manage. A second option would be to cache the data you're requesting. Let's assume you're able to cache the data such that you achieve a hit ration of 90% (hitRatio = hits/(hits+misses)). That is, 9 out of every 10 requests for data go to the cache instead of to the database (certainly doable in most circumstances). What you've now gone ahead and done is effectively reduced your database load to 10 requests per second. This means that the same database with the addition of a cache is now able to scale by a factor of 10. That's a pretty significant increase in scalability.

That's it for Part 1 of this series. Stay tuned for Part 2 where I'll discuss what to cache and why. If you're planning to be at Adobe MAX 2009, stop by my session on Advanced ColdFusion Caching where I'll be talking about this as well as all of the great new Caching features in ColdFusion 9 in a lot more depth.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Jordan's Gravatar Rob does ColdFusion 9 support ehcache server or just the in-process embedded version?
# Posted By Jordan | 10/16/09 1:23 PM
Rob Brooks-Bilson's Gravatar ColdFusion 9 supports the in-process embedded version natively. To use ehcache server, you need to install it standalone or deploy as a WAR. You can then access it from CF using either SOAP or REST.
# Posted By Rob Brooks-Bilson | 10/16/09 2:52 PM
Jose Galdamez's Gravatar You provide a compelling argument for caching when you talk about caching for scalability. I think for some organizations database clustering will always be a must. Else how would we keep all those brainy DBAs employed? :)

For many sites that can't afford that investment, however, smart use of caching can certainly do the trick for the short-run. Every time I implemented CF5-style caching or query caching I mainly had app performance in mind. Eventually scalability became a problem when spiders would take down our site on the weekend. I implemented trusted cache a month ago and haven't seen problems since. Granted, trusted cache is not new to CF9, but the concept is same.

I'm guessing the pre-condition for us is that we are dealing with a 64-bit install of CF on top of a 64-bit OS, right? (RAM limitations)
# Posted By Jose Galdamez | 11/28/09 10:55 AM



Copyright 1995-2010 Rob Brooks-Bilson. All rights reserved.
Aura skin for Raymond Camden's BlogCFC inspired by Joe Rinehart & Steven Erat. This blog is running version 5.9.004.