If you remember back to my previous post, Intro to Ehcache Server and RESTful Web Services, I went into detail regarding how to set up an out-of-process caching server. If you’re lucky enough to be using ColdFusion 9 and have upgraded to the latest 9.0.1 patch, you may have noticed that one of the updates was to update Ehcache 1.6 to version 2.0. This update enables you to tie your ColdFusion instance in with a Terracotta distributed caching server.
Why Distributed Caching?
The standard ColdFusion 9 installation provides some new caching mechanisms which utilize Ehcache. This method of caching happens on the same server as the application and within the same JVM. Unfortunately, whenever you perform any caching at this level, your cache will consume the same resources that are normally allocated to your application server.
The obvious downside is that you’re going to run out of memory easier this way. So instead of scaling up (by adding more ram) you can utilize a distributed caching method in order to scale outward.
Installation and Configuration
These steps have been borrowed from the Ehcache Documentation.
The easiest way to get started is to download the Terracotta/Ehcache server installer. You can get those files from the Terracotta website.
Once installed, go into the ehcache directory from where you installed Terracotta and copy ehcache-terracotta-2.0.0.jar from there and place it into your lib folder where you installed ColdFusion. In my case (multiserver deployment) this folder was found under C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\cfusion\lib.
Next edit the ehcache.xml file from that same folder to modify the defaultCache tag to look like the following:
<terracottaConfig url="localhost:9510" /> <defaultCache ... > <terracotta clustered="true" /> </defaultCache>
Finally, start the Terracotta server by invoking start-tc-server.bat from your terracotta/bin directory. Make sure you also restart your ColdFusion services to ensure the new configuration is loaded properly.
Download Terracotta 3.2.1 Installer
Testing Your Distributed Cache
Now for the fun part. With Terracotta running and ColdFusion connected to it correctly, we can now start putting some objects into our cache. For my test I added the following code:
<h2>Get "CacheTest" from the cache:</h2>
<cfdump var="#CacheGet('CacheTest')#" /><br />
<h2>Set "CacheTest" to be Now():</h2>
<cfset CachePut('CacheTest', Now()) /><br />
<h2>Get "CacheTest" from the cache:</h2>
<cfdump var="#CacheGet('CacheTest')#" />
And as (hopefully!) expected, our cache is working:
It would be a good idea to run the Terracotta Developer Console. From here you’ll be able to manage and monitor your Terracotta cluster. Simply call dev-console.bat from the terracotta/bin directory to start it up.
You can see that the CacheTest object we added to the cache is visible from this console:
Okay, What Next?
It’s all well and dandy that we have our caching server running, but that’s using a default installation and configuration setup for Terracotta. If we want to make it even more powerful, we’ll need to configure a tc-config.xml file to work with our server. You can find a default one with plenty of annotations under the directory where you installed terracotta at /docs/tc-config-express-reference.xml.
For a lot more information about how to configure it, check out the documentation at Terracotta.org.
You may also be interested in configuring Terracotta to reconnect automatically to prevent short network disruptions (which it doesn’t do by default). Check it out here.
Terracotta.org – tc-config.xml reference
Tags: advanced, Caching, ColdFusion, distributed, Ehcache, Terracotta



Thanks for this post it is informative and timely in my opinion. In our day to day performance work, we were already encountering more uses on external caching, mainly memcached though. I think the inclusion of EHCACHE in CF9 and now the latest version of EHCACHE in the 9.0.1 updater gives us a really great opportunity to scale up CF Applications more easily. Dan Wilson and myself along with Mike Allen from EHCACHE will be talking about CF and EHCACHE at CFUnited so once again thanks for posting this piece.
I’m doing a lot of digging around the net this week to no avail so far, looking for the answer to this question:
How does one connect TWO ColdFusion 9.0.1 instances, say on different servers, to the same Ehcache Terracotta cluster in such a way that allows them to share cache objects? Specifically with Hibernate, but at this point I’ll take the standard object cache.
I’m seeing each application on each instance construct it’s own instance of the cache manager, which just isn’t helpful toward the goal of having an EntitySave() on server A update the same cached instance that server B is going to attempt to hit.
Any ideas/leads/etc? Any help would be so greatly appreciated, as was this post when I was starting out not long ago with this technology.
I’m trying to get this up and running but am encountering trouble when I change the virtual paths in the jvm.config to absolute paths. Has anyone successfully set this up on a CF standalone installation in a windows environment? If so can you share the changes you made to the jvm.config -Dcoldfusion.classpath section to make it all work?
Thanks,
-Ben
Hello,
Did anyone else here experience the following error setting up Terracotta with CF?
Unable to load class net.sf.ehcache.terracotta.StandaloneTerracottaClusteredInstanceFactory. Initial cause was com/tc/config/schema/beanfactory/ConfigBeanFactory
I get this error on a CacheGet call.
Terracotta installed without incident and the ehcache.xml was edited as per this article.
The only bean in net.sf.ehcache.terracotta is ClusteredInstanceFactory.
Any help would be greatly appreciated!
Regards,
Mike