<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ashish's Tech Blog &#187; Ehcache</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/category/ehcache/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 17 Aug 2010 12:04:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Caching and Database Offloading &#8211; Best of both worlds with Terracotta Darwin Release</title>
		<link>http://www.ashishpaliwal.com/blog/2010/03/caching-and-database-offloading-best-of-both-worlds-with-terracotta-darwin-release/</link>
		<comments>http://www.ashishpaliwal.com/blog/2010/03/caching-and-database-offloading-best-of-both-worlds-with-terracotta-darwin-release/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 11:20:41 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Ehcache]]></category>
		<category><![CDATA[Terracotta]]></category>
		<category><![CDATA[Darwin]]></category>
		<category><![CDATA[Teracotta]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=424</guid>
		<description><![CDATA[Abstract The Terracotta Darwin release a feature packed release. I love to call it a Developer's delight. Integrating Terracotta with existing apps has been simplified a lot with Express Mode. In this series we shall see some touch upon some of the new features of the Darwin release How is this post organized? We shall [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Abstract</strong></p>
<p>The <a href="http://www.terracotta.org/start/?src=darwin-release-badge">Terracotta Darwin release</a> a feature packed release. I love to call it a Developer's delight. Integrating Terracotta with existing apps has been simplified a lot with Express Mode. In this series we shall see some touch upon some of the new features of the Darwin release</p>
<p><strong>How is this post organized?</strong></p>
<p>We shall take a sample Use Case(s), and discuss the needs. Using the Use Case we shall see the fitment of Terracotta features. Discussion will be around the following</p>
<ul>
<li>Bulk Loading to Cache</li>
<li>Write Behind Cache</li>
</ul>
<p><strong>The Use Case - Bulk Loading</strong></p>
<p>Bulk loading the cache is a common need for Cache warmup. Here we try to load the data into Cache in a single go, rather then on Cache miss. This help us in reducing the database lookup upon each miss.</p>
<p><em><strong>Some Use Cases</strong</em><br />
<strong>Case 1:</strong> For a Catalogue site, loading all the available catalogues/Products during startup</p>
<p><img src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2010/03/catalogue_use_case-300x100.png" alt="catalogue_use_case" title="catalogue_use_case" width="300" height="100" class="aligncenter size-medium wp-image-445" /><br />
<script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>The cache can be loaded at the startup with the hot set o Products or as desired, against loading it at runtime, upon a Cache miss.</p>
<p><strong>Case 2:</strong> For an Element Management System, loading all the Managed Devices into cache, available to all nodes in the cluster</p>
<p><img src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2010/03/ems_use_case-300x152.png" alt="ems_use_case" title="ems_use_case" width="300" height="152" class="aligncenter size-medium wp-image-446" /><br />
<script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>For an EMS Device Information for the Devices it is managing is used most often. The Device info can be bulk loaded at the startup. The example show the use of cache by a Poller, which Polls the Device status.</p>
<p>There are other common use cases. Please add comment with your Use case.</p>
<p><strong>Ehcache Bulk Loading with Terracotta</strong></p>
<p><a href="http://www.terracotta.org/start/?src=darwin-release-badge">Terracotta Darwin release</a> has optimized bulk loading in Ehcache. In normal scenario, loading elements in Ehcache, would update the cache entry to all the clustered caches, making the bulk loading a bit slower.</p>
<p>Let's see how bulk loading in action</p>
<pre class="brush: java">
      // Get the Cache to be preloaded
      Cache ehcache = CacheManager.getInstance().getCache(&quot;mycache&quot;);

      // Set this property before bulk loading
      ehcache.setNodeCoherence(false);

      // Load everything here
      loadCache(); 

      // reset the property
      ehcache.setNodeCoherence(true);

      // Wait for all data to be distributed across cluster
      ehcache.waitUntilClusterCoherent();
</pre>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>What's being done inside the loadCache()?<br />
Here is a glimpse</p>
<pre class="brush: java">
private void loadCache() {
   // Query the DB get the data and get the result into array
   // or what ever data structure you need

   // iterate over result and put into cache
   for(int i = 0; i &lt; data.length; i++) {
       Element element = new Element(data.getId, data);
       ehcache.put(element);
   }
}
</pre>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Here is how the cache look when we are bulk loading the data. We are using both nodes to load distinct data to speed up loading process. The loading can be taken care by a single node also.</p>
<p><img src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2010/03/ehcache_-_while_bulk_loading-300x198.png" alt="ehcache - While Bulk uploading" title="ehcache - While Bulk uploading" width="300" height="198" class="aligncenter size-medium wp-image-441" /></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Once the bulk loading is completed, and Nodes are set to coherent again, the data is replicated across the cluster, and all Nodes have the same data set.</p>
<p><img src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2010/03/ehcache_-_after_bulk_loading-1-300x208.png" alt="ehcache - After Bulk upload" title="ehcache - After Bulk upload" width="300" height="208" class="aligncenter size-medium wp-image-442" /></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15, created 1/25/10 */
google_ad_slot = "2118617107";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>What's Next?</strong><br />
In the next post we shall cover the Write-Behind feature of Ehcache for DB offloading. I will rewrite the code of one my previous post <a href="http://www.ashishpaliwal.com/blog/2010/01/demystifying-terracotta-tim-async-scalable-way-to-off-load-db-updates-from-app-transaction/">Exploring Terracotta tim-async - Scalable way to off-load DB updates from App Transaction</a>, to use the write-behind feature.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x60ImageOnly */
google_ad_slot = "0221726572";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2010/03/caching-and-database-offloading-best-of-both-worlds-with-terracotta-darwin-release/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>So you want Distributed, Scalable and Highly Available Cache?</title>
		<link>http://www.ashishpaliwal.com/blog/2010/02/so-you-want-distributed-scalable-and-highly-available-cache/</link>
		<comments>http://www.ashishpaliwal.com/blog/2010/02/so-you-want-distributed-scalable-and-highly-available-cache/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 09:22:53 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Ehcache]]></category>
		<category><![CDATA[Terracotta]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=402</guid>
		<description><![CDATA[Abstract So you want Distributed, Scalable and Highly Available Cache? If yes, then this is the right place for you. The Terracotta Ehcache release has these features inbuilt. The Express Installation mode has simplified Terracotta integration in your application. How this post is organized First we shall start with simple standalone cache and then see [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Abstract</strong></p>
<p><em><strong>So you want Distributed, Scalable and Highly Available Cache?</strong></em> If yes, then this is the right place for you. The Terracotta Ehcache release has these features inbuilt. The <a href="http://www.terracotta.org/documentation/ga/product-documentation-2.html#382351408_pgfId-11318">Express Installation</a> mode has simplified Terracotta integration in your application. </p>
<p><strong>How this post is organized</strong><br />
First we shall start with simple standalone cache and then see how Terracotta can easily help us create a Distributed, Scalable and Highly Available cache, with a few minor configurations</p>
<p><strong>What all do you need run the example?</strong></p>
<ul>
<li>Terracotta 3.2.0 - Download it from <a href="http://www.terracotta.org/dl/">here</a></li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15LinkUnit */
google_ad_slot = "4400881690";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>Sample Application</strong><br />
Lets design a sample application (a real simple one), which we shall use to demonstrate the features. Device Monitoring is a common requirement in OSS System, and caching the Device information reduces the Database hits. The essential components of our application are<br />
- DevideInfo - A POJO that stores device information<br />
- CacheHandler - Class that handles cache initialization and other ops</p>
<p>Lets see the CacheHandler class, which is the heart of our implementation</p>
<pre class="brush: java">
public class DeviceInfo  implements Serializable {

    String deviceId;

    String name;

    // .... other device information

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }
}
</pre>
<p>The class is simple and contains information related to the device.</p>
<p>Let see our CacheHandler class</p>
<pre class="brush: java">
public class CacheHandler {

    public Cache deviceCache;

    public void initCache() {
        // Initialize the CacheManager
        CacheManager cacheManager = CacheManager.create();
        // Get the Cache to store device information
        deviceCache = cacheManager.getCache(&quot;deviceCache&quot;);
    }

    public void addToCache(DeviceInfo device) {
        Element el = new Element(device.getDeviceId(), device);
        deviceCache.put(el);
    }

    protected int getDeviceCacheSize() {
        return deviceCache.getSize();
    }

    public static void main(String[] args) {
        CacheHandler handler = new CacheHandler();
        handler.initCache();

        // Lets add 10 devices
        // just to keep life simple
        for(int i = 0; i &lt; 10; i++) {
            DeviceInfo device = new DeviceInfo();
            device.setDeviceId(&quot;Device-&quot;+i);
            handler.addToCache(device);
        }

        // Not recommended in production
        System.out.println(&quot;Cache Size = &quot;+handler.getDeviceCacheSize());

    }
}
</pre>
<p>The class has two main functions<br />
- initCache - The API initializes the cache<br />
- addToCache - The API adds the device information to the device cache</p>
<p>Lets see our ehcache.xml</p>
<pre class="brush: xml">
&lt;ehcache xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:noNamespaceSchemaLocation=&quot;ehcache.xsd&quot;
             updateCheck=&quot;true&quot; monitoring=&quot;autodetect&quot;&gt;

   &lt;defaultCache
            maxElementsInMemory=&quot;10000&quot;
            eternal=&quot;false&quot;
            timeToIdleSeconds=&quot;120&quot;
            timeToLiveSeconds=&quot;120&quot;
            overflowToDisk=&quot;true&quot;
            diskSpoolBufferSizeMB=&quot;30&quot;
            maxElementsOnDisk=&quot;10000000&quot;
            diskPersistent=&quot;false&quot;
            diskExpiryThreadIntervalSeconds=&quot;120&quot;
            memoryStoreEvictionPolicy=&quot;LRU&quot;
            /&gt;

    &lt;cache name=&quot;deviceCache&quot;
           maxElementsInMemory=&quot;1000000&quot;
           maxElementsOnDisk=&quot;1000&quot;
           eternal=&quot;false&quot;
           overflowToDisk=&quot;false&quot;
           diskSpoolBufferSizeMB=&quot;20&quot;
           timeToIdleSeconds=&quot;100&quot;
           timeToLiveSeconds=&quot;100&quot;
           memoryStoreEvictionPolicy=&quot;LFU&quot;&gt;
     &lt;/cache&gt;

&lt;/ehcache&gt;
</pre>
<p>In the configuration we have defined a deviceCache, that we shall use to store device information.</p>
<p>Following is the list of jars you would need to run this app </p>
<ul>
<li>ehcache-core-1.7.2.jar</li>
<li>slf4j-api-1.5.8.jar</li>
<li>slf4j-jdk14-1.5.8.jar</li>
</ul>
<p>All these jars are available as part of Terracotta installation under <TC Install Dir>/distributed-cache directory</p>
<p>You can run this example and see the same in action.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x60ImageOnly */
google_ad_slot = "0221726572";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>Making it Distributed....</strong></p>
<p>Now lets assume that we want to make the Cache fault tolerant, survive JVM crashes, as well as have it available on different JVM's. It should be able to Scale as we add more JVM's to it and other stuff. With Terracotta, we can just do it in a few steps without changing the code. the magic lies with Express Installation mode, introduced in Terracotta version 3.2.0 and above</p>
<p>Lets see the 3 steps that we need to perform</p>
<p>1. Updates to the ehcache.xml<br />
Add following element to ehcache.xml</p>
<p>&lt;terracottaConfig url=&quot;localhost:9510&quot; /&gt;</p>
<p>and for the deviceCache, we add &lt;terracotta /&gt; element</p>
<p>here is the updated ehcache.xml</p>
<pre class="brush: xml">
&lt;ehcache xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:noNamespaceSchemaLocation=&quot;ehcache.xsd&quot;
             updateCheck=&quot;true&quot; monitoring=&quot;autodetect&quot;&gt;

   &lt;terracottaConfig url=&quot;localhost:9510&quot; /&gt; 

   &lt;defaultCache
            maxElementsInMemory=&quot;10000&quot;
            eternal=&quot;false&quot;
            timeToIdleSeconds=&quot;120&quot;
            timeToLiveSeconds=&quot;120&quot;
            overflowToDisk=&quot;true&quot;
            diskSpoolBufferSizeMB=&quot;30&quot;
            maxElementsOnDisk=&quot;10000000&quot;
            diskPersistent=&quot;false&quot;
            diskExpiryThreadIntervalSeconds=&quot;120&quot;
            memoryStoreEvictionPolicy=&quot;LRU&quot;
            /&gt;

    &lt;cache name=&quot;deviceCache&quot;
           maxElementsInMemory=&quot;1000000&quot;
           maxElementsOnDisk=&quot;1000&quot;
           eternal=&quot;false&quot;
           timeToIdleSeconds=&quot;100&quot;
           timeToLiveSeconds=&quot;100&quot;
           memoryStoreEvictionPolicy=&quot;LFU&quot;&gt;

        &lt;terracotta /&gt;
     &lt;/cache&gt;

&lt;/ehcache&gt;
</pre>
<p>There is no need to change the code <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>2. Add ehcache-terracotta-1.8.0.jar to the classpath</p>
<p>Lets start the Terracotta Server</p>
<p>3. Goto Terracotta_Install_dir/bin and execute </p>
<blockquote><p>$ ./start-tc-server.sh</p></blockquote>
<p>This shall start the Terracotta server</p>
<p>Now run the application on multiple JVM's. The device information is available on all client JVM's <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>To monitor Cache, you can use <a href="http://www.terracotta.org/confluence/display/docs/Terracotta+Developer+Console">Terracotta Dev Console</a></p>
<p>If you have further question/queries, please visit <a href="http://forums.terracotta.org/forums/forums/list.page">Terracotta Forums</a>.</p>
<p><strong>What's coming up Next</strong>?<br />
The next post shall touch on write-behind feature in the latest <a href="http://www.terracotta.org/beta/darwin">Terracotta Darwin</a> release. Its essentially to get best of both worlds - Caching and Database Offloading <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  Stay tuned..</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 300x250, created 2/9/10 */
google_ad_slot = "1177095982";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2010/02/so-you-want-distributed-scalable-and-highly-available-cache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
