| About Terracotta Documentation This documentation is about Terracotta DSO, an advanced distributed-computing technology aimed at meeting special clustering requirements. Terracotta products without the overhead of DSO meet the needs of most use cases and clustering requirements. To learn how to migrate from Terracotta DSO to standard Terracotta products, see Migrating From Terracotta DSO. To find documentation on non-DSO (standard) Terracotta products, see the Terracotta Product Documentation. Terracotta release information, such as release notes and platform compatibility, is found in Product Information. |
|
Terracotta Distributed CacheIntroductionThe Terracotta Distributed Cache is an interface providing a simple distributed eviction solution for map elements. The Distributed Cache, implemented with the Terracotta Integration Module (TIM) tim-distributed-cache, provides a number of advantages over more complex solutions:
How to Implement and ConfigureUnder the appropriate conditions, the Terracotta Distributed Cache can be used in any Terracotta cluster. If your application can use the Distributed Cache's built-in Map implementation for a cache, you can avoid having to customize your own data structure. See A Simple Distributed Cache for instructions on using the Distributed Cache with the provided Map implementation. RequirementsTo ensure that the Terracotta Distributed Cache performs well and without errors, check the following requirements. Classpath RequirementsThe TIMs tim-distributed-cache and tim-concurrent-collections must be on your application's classpath at runtime. See Installing the TIM to learn how to install tim-distributed-cache.
Eviction ParametersThe Terracotta Distributed Cache has the following eviction parameters: Time to Live To maximize cache performance benefits, configure and tune these parameters to optimize data retention and eviction behavior. To learn how to configure the Terracotta Distributed Cache eviction parameters, see Usage Pattern. JDK VersionThe Terracotta Distributed Cache requires JDK 1.5 or greater. Installing the TIMTo use the Terracotta Distributed Cache, you must both install tim-distributed-cache and include the evictor JAR file in your classpath. To install the TIM, run the following command from ${TERRACOTTA_HOME}: UNIX/Linux [PROMPT] bin/tim-get.sh install tim-distributed-cache Microsoft Windows [PROMPT] bin\tim-get.bat install tim-distributed-cache You should see output that appears similar to the following: Installing tim-distributed-cache 1.3.0-SNAPSHOT and dependencies... INSTALLED: tim-distributed-cache 1.3.0-SNAPSHOT - Ok INSTALLED: tim-concurrent-collections 1.3.0-SNAPSHOT - Ok Run the following command from ${TERRACOTTA_HOME} to update the Terracotta configuration file (tc-config.xml by default): UNIX/Linux [PROMPT] bin/tim-get.sh upgrade <path/to/Terracotta/configuration/file> Microsoft Windows [PROMPT] bin\tim-get.bat upgrade <path\to\Terracotta\configuration\file> For more information about installing and updating TIMs, see the TIM Update Center. Locking RequirementsTerracotta automatically provides locking for read (get) and write (put) operations on the distributed map. These locks last for the duration of the get or put operation. Mutating an object obtained from the distributed map requires a read/write lock to avoid race conditions and potential corruption to data. For example, assume a distributed map has an element <k1, v1> in it. The following operation does not require explicit locking: myObject = getFromMyDistributedMap(k1); // Terracotta provides a lock for the duration of getFromMyDistributedMap().
Adding a new element to the map also does not require explicit locking: putIntoMyDistributedMap(k2, v2); // Terracotta provides a lock for the duration of putIntoMyDistributedMap().
However, the following operation requires a read/write lock: myNewObject = myMutator(myObject); // myObject should be locked until it is put back into the map.
Note the following:
A Simple Distributed CacheClustered applications with a system of record (SOR) on the backend can benefit from a distributed cache that manages certain data in memory while reducing costly application-SOR interactions. However, using a cache can introduce increased complexity to software development, integration, operation, and maintenance. The Terracotta Distributed Cache includes a distributed Map that can be used as a simple distributed cache. This cache uses the Terracotta Distributed Cache, incorporating all of its benefits. It also takes both established and innovative approaches to the caching model, solving performance and complexity issues by:
Structure and CharacteristicsThe Terracotta Distributed Cache is an interface incorporating a distributed map (an extension of ConcurrentMap in the JDK) with standard map operations. For more information about the Terracotta Distributed Cache, see:
Usage PatternA typical usage pattern for the Terracotta Distributed Cache is shown in the MyStuff class below. The next section contains a full list of configuration parameters available to CacheConfigFactory. import org.terracotta.cache.CacheConfigFactory; import org.terracotta.cache.DistributedCache; public class MyStuff { // Mark as Terracotta root private DistributedCache<String, Stuff> sharedCache; public MyStuff() { if(sharedCache == null) { DistributedCache<String, Stuff> newCache = CacheConfigFactory.newConfig() .setMaxTTLSeconds(6*60 * 60) // Regardless of use, remove after 6 hours .setMaxTTISeconds(30*60) // Remove after 30 minutes of none-use. .newCache(); // Set root - if this doesn't succeed, shutdown the newCache as it has a worthless background evictor thread. sharedCache = newCache; if(sharedCache != newCache) { newCache.shutdown(); } } public void putStuff(String key, Stuff stuff) { sharedCache.put(key, stuff); } public Stuff getStuff(String key) { return sharedCache.get(key); } } Cache Configuration ParametersThe configuration parameters that can be set through CacheConfigFactory are summarized in the following table.
Usage ExampleThe following is an example of a cache that implements the Terracotta distributed cache: import org.terracotta.cache.*; import static org.terracotta.cache.CacheConfigFactory.*; DisributedCache<String,String> cache = CacheConfigFactory.newConfig() .setMaxTTLSeconds(10) .setMaxTTISeconds(5) .newCache(); // start() method not needed; start is automatic. cache.put("Rabbit", "Carrots"); cache.put("Dog", "Bone"); cache.put("Owl", "Mouse"); // wait 3 seconds cache.get("Rabbit"); // wait 2 seconds - expire Dog and Owl due to TTI assert ! cache.containsKey("Dog"); assert ! cache.containsKey("Owl"); assert cache.containsKey("Rabbit"); // wait 5 seconds - expire Rabbit due to TTL assert ! cache.containsKey("Rabbit"); Terracotta Distributed Cache in a Reference ApplicationThe [Examinator reference application] uses the Terracotta Distributed Cache to handle pending user registrations. This type of data has a "medium-term" lifetime which needs to be persisted long enough to give prospective registrants a chance to verify their registrations. If a registration isn't verified by the time TTL is reached, it can be evicted from the cache. Only if the registration is verified is it written to the database. The combination of Terracotta and the Terracotta Distributed Cache gives Examinator the following advantages:
|