Showing posts with label Cache. Show all posts
Showing posts with label Cache. Show all posts

Wednesday, September 07, 2011

Clustered Caching with Tangosol Coherence

(From http://mdavey.wordpress.com/2007/01/16/clustered-caching-with-tangosol-coherence/)
 Below, in no particular order are a few items worth noting about Tangosol:
  • When installing Tangosol, make sure all the nodes are running the same version
  • Don’t use any classes in the component.net packages – Coherence was build with its own development environment, compiler etc. component.net contains these internal components.
  • BetFair a few years ago had 80 nodes running Tangosol Coherence. The largest installation today is around 1000 nodes.
  • Coherence uses TCMP (UDP based) protocol for data movement
  • From an installation perspective, 5 machines is probably the minimum requirement. Lots of RAM for each machine, installed across different racks to reduce the risk. The more machines the Coherence cluster contains, the lower the risk.
  • All machines in the cluster should be configured with identical config files, network settings, NIC’s (full/half duplex) etc.
  • Multi-cast disconnect issues can be as simple as a miss-configured router.
  • Production checklist
  • Near Topology (near-*)
  • During development set TTL=0 and changing the group address and port.
  • Client that come/go from the Coherence cluster should set local storage=false, while cluster servers should set local storage=true
  • NamedCache.putAll is far more efficient that put since it can reduce network hops.
  • Prior to cluster usage, run the multicast test for 24 hours.
  • Use Ethereal or similar to monitor the network to help identify unreliable sockets and NIC configuration issues.
  • Java serialization can be a performance bottleneck, consider using Coherence’s ExternalizableLite and its helper, ExternalizableHelper. Externalizable offers reduced GC and ~6x speed improvement.
  • High ticking volumns can cause problems for any application. In the case of Tangosol, and caching ticks, one possible solution is to queue the incoming ticks, and use a thread pool to insert the ticks into the cache.
Tangosol Coherence as its simplest:
import java.io.IOException;

import java.util.Date;
import com.tangosol.net.CacheFactory;
import com.tangosol.net.Cluster;
import com.tangosol.net.NamedCache;


public class firstExample {
  public static void main(String[] args) throws IOException {
    try {
      Cluster cluster = CacheFactory.ensureCluster();
      System.out.println(cluster);
     
      NamedCache myCache = CacheFactory.getCache(“test”);
      Object existingVale = myCache.put(“message”, “someMesasge “ + new Date());

      System.out.println(“Existing Val :” + existingVale);

      Object val = myCache.get(“message”);
      System.out.println(“Val :” + val);
      System.out.println(“Press any key”);
      System.in.read();
    }finally {
      CacheFactory.shutdown();
    }
  }
}

Getting started with Tangosol Coherence

(From http://www.javalobby.org/java/forums/t78008.html)

This tip will get you started with Coherence so you can see how easy it is to begin to use in your applications for caching data within a cluster.

Grab a download of Coherence and unzip it, say into a directory called tangosol

Run the cache-server.cmd located in the tangosol/bin directory (replace the .cmd with .sh if you're running on *NIX). This starts a cache server which is storage enabled - the storage enabled bit simply means that this member within the cluster will store data.

Compile and run the below little program, it puts a couple of entries into a cache. It then gets their values out, along with a couple entries which don't currently exist in the cache.

The only JARs you need to reference are tangosol.jar and coherence.jar in the tangosol/lib directory.

import com.tangosol.net.CacheFactory;
import com.tangosol.net.NamedCache;


public class Tip1 {
  public static void main(String[] args) {
    NamedCache cache = CacheFactory.getCache("people");


    String key1 = "dave";
    cache.put(key1, new Long(36));
    String key2 = "jenny";
    cache.put(key2, new Long(25));


    String key3 = "stan";
    String key4 = "jane";


    System.out.println(key1 + "=" + cache.get(key1));
    System.out.println(key2 + "=" + cache.get(key2));
    System.out.println(key3 + "=" + cache.get(key3));
    System.out.println(key4 + "=" + cache.get(key4));
  }
}

Next run coherence.cmd in the tangosol/bin . This starts a nice little command-line application which allows you to explore some of Coherence's functionality without writing any programs.

At the Coherence command-line application's prompt type cache people - this just tells the command-line application that we want to manipulate the people cache. Some XML should have been displayed for the default distributed cache. The distributed cache is nowadays referred to as a 'partitioned cache' to better describe what it is actually doing with the data within the cluster.

Type get dave and press enter, you should see the value for Dave.
Next type put stan 55 and press enter, then put jane 40 . This will put two new entries into the cache using the Coherence command-line application.
Run the Tip1 application again and you should now see values for Stan and Jane.
From the Coherence command-line application, type remove stan .
Running the Tip1 application will show that Stan no longer has an entry.

To remove an entry using Java code simply use cache.remove(key) . As you might have noticed by now, it all seems very Map like - it is, because Coherence's NamedCache interface extends from interfaces which extend from java.util.Map.

For a bit of fun, start another cache-server.cmd and from Coherence command-line application type: get jane . You'll see no difference from before, the value will be displayed.

Now, kill the first instance of the cache-server.cmd and from the Coherence command-line application type get jane . Yep - still no difference, Coherence has automatically failed over without any loss of data. The data had been partitioned (split) across the two cache servers, when one of them was killed, the other simply promoted the 'backups' it was storing to be the primary copies ensuring all of the data was still accessible. If you start the first instance again, then Coherence will seamlessly 'fail back'.

Hope this gets you going with Coherence. If you want to explore the Coherence command-line application a little more then type help to see the valid commands and their syntax. The following link has descriptions for many of the commands: http://forums.tangosol.com/thread.jspa?threadID=51&tstart=0

Don't forget to check out the Wiki as well: http://wiki.tangosol.com