Call us: +1-415-738-4000
Distributed Cache, formally called Terracotta Distributed Ehcache, is Ehcache running in a Terracotta cluster. Distributed caching is the recommended method of operating Ehcache in a clustered or scaled-out application environment, as it enables data sharing among multiple CacheManagers and their caches in multiple JVMs.
You can find tutorials, installation procedures, best practices, details on the Terracotta Server Array, and more in the Terracotta documentation.
Distributed Ehcache combines an in-process Ehcache with the Terracotta Server Array acting as a backing cache store.
With Terracotta Server Array the data is split between an Ehcache node (the L1 cache) and the Terracotta Server Array itself (the L2 Cache). As with the other replication mechanisms, the L1 can hold as much data as is comfortable. But there is always a complete copy of all cache data in the L2. The L1 therefore acts as a hot-set of recently used data. Distributed Ehcache is persistent and highly available, leaving the cache unaffected by the termination of an Ehcache node. When the node comes back up it reconnects to the Terracotta Server Array L2 and as it uses data fills its local L1.

From a network topology point of view Distributed Ehcache consists of:

Another way to look at the architecture of Distributed Ehcache is as a tiered memory hierarchy. Each in-process Ehcache instance (L1s) can have:
Off-heap memory (BigMemory). This is stored in direct byte buffers.
The Terractta servers (L2s) run as Java processes with their own memory hierarchy:
Heap memory

Differences in behavior and available functionality between distributed cache and standalone and replicated caches are called out in the documentation. Some major differences are listed here:
<cache ... transactionalMode="xa">).session.refresh() as with replicated caches.<cacheWriter writeMode="write-behind">).As this example shows, running Ehcache with Terracotta clustering is no different from normal programmatic use.
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
public class TerracottaExample {
CacheManager cacheManager = new CacheManager();
public TerracottaExample() {
Cache cache = cacheManager.getCache("sampleTerracottaCache");
int cacheSize = cache.getKeys().size();
cache.put(new Element("" + cacheSize, cacheSize));
for (Object key : cache.getKeys()) {
System.out.println("Key:" + key);
}
}
public static void main(String[] args) throws Exception {
new TerracottaExample();
}
}
The above example looks for sampleTerracottaCache. In ehcache.xml, we need to uncomment or add the following line:
<terracottaConfig url="localhost:9510"/>
This tells Ehcache to load the Terracotta server config from localhost port 9510. For url configuration options, refer to "Adding an URL Attribute" in Terracotta Clustering Configuration Elements. Note: You must have a
Terracotta 3.1.1 or higher server running locally for this example.
Next we want to enable Terracotta clustering for the cache named sampleTerracottaCache. Uncomment or add the
following in ehcache.xml.
<cache name="sampleTerracottaCache"
maxEntriesLocalHeap="1000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="1800"
overflowToDisk="false">
<terracotta/>
</cache>
That's it!
With a Distributed Ehcache, there is a Terracotta Server Array. At development time, this necessitates running a server locally for integration and/or interactive testing. There are plugins for Maven and Ant to simplify and automate this process.
For Maven, Terracotta has a plugin available which makes this very simple.
<pluginRepositories>
<pluginRepository>
<id>terracotta-snapshots</id>
<url>http://www.terracotta.org/download/reflector/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<plugin>
<groupId>org.terracotta.maven.plugins</groupId>
<artifactId>tc-maven-plugin</artifactId>
<version>1.5.1</version>
<executions>
<execution>
<id>run-integration</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-integration</goal>
</goals>
</execution>
<execution>
<id>terminate-integration</id>
<phase>post-integration-test</phase>
<goals>
<goal>terminate-integration</goal>
</goals>
</execution>
</executions>
</plugin>
To start Terracotta:
mvn tc:start
To stop Terracotta:
mvn tc:stop
See the Terracotta Forge for a complete reference.
