Creating Terracotta Server Arrays with EC2 CloudFormation for use by Ehcache

This is the first in a series of articles showing how to automate deployment of Ehcache in EC2.

Ehcache is a distributed cache which works with a Terracotta Server Array (“TSA”) which acts as the in-memory store over the network. While Ehcache is simply a jar and is included in your app, provisioning a distributed cache in EC2 requires running up a Terracotta Server Array. Some approaches to this include using Chef, rolling your own AMIs and manual installation.

TSA as a Utility

From the point of view of an application running on EC2. the TSA is a utility. As a utility it would be great if you could provision it much like you do RDS or S3. Now while those are built-in to EC2, CloudFormation templates aim to bring the same ease of provisioning to third party utilities like the TSA.


On 31 May CloudFormation was upgraded with new features: validation for template parameters, resource deletion policies and the ability to block stack creation until your application is ready. Combine that with Ubuntu’s cloud-init feature supported in Amazon Linux AMI and you have a flexible and powerful provisioning infrastructure that you can drive from the AWS Console, command line or SDKs for multiple programming languages. My example uses the Java SDK for CloudFormation.

JUnit Integration Test

This example shows you how to, within a JUnit test, fire up a Terracotta server with CloudFormation and perform an Ehcache integration test. Though relatively simple, it exercises most of the moving parts of CloudFormation.

The example uses standard 32 bit AMIs. It creates a single active Terracotta server, using the open source version.  First it starts an AMI, downloads and installs Terracotta and starts it using the config. We then query the stack to find the EC2 Instance and then use the EC2 API to resolve the public DNS name. Using ehcache.xml’s token replacement feature we then inject the public dns name into the tcconfig and start Ehcache. Ehcache creates a distributed cache across the Internet to the TSA you just created. We then stick something in the cache and read it back out.

You can use this example to set up your own integration testing.

You can check out and run my sample code from GitHub with the following URL: git://

To run the example you need to:

  1. have an AWS account
  2. Edit and add your accessKey and secretKey (available from your account page)
  3. (Optional) Edit and set your keyChainName. It is currently set to gluck. This is an ssh keychain that lets you access the running AMIs.

Though not done in the sample, there are a few other Terracotta deployment options which should be easy with these tools:

  1. Create a tc:config for a striped production cluster
  2. Use a custom terracotta configuration
  3. Place the config on a web server and tart each TC server via a remote URL to the TC:config and the -n switch

In the next article I will show you how to automatically deploy an app using Ehcache to BeanStalk, and how to connect your Ehcache to the TSA created.

Leave a Reply