Coherence - Up and running in 15 minutes!

What is Coherence and why would you used it?

Oracle Coherence stores data in-memory so access to it is very fast. Therefore, you would commonly use Coherence to provide fast access to data. Typically this is necessary in applications where performance and latency are key.

How does it work?

Oracle Coherence is a small Java programme that stores data in lookup tables, key/value pairs, or a Map of Java objects in memory (or a cache in Coherence terminology). Well I can do that myself I can hear you say, why do I need Coherence. Well if you do it yourself and crate a lookup table in your own Java program what happens if the Java program crashes?, all your data in-memory is lost. You could persist the data to disk or a database as soon as it changes, but then that would degrade performance. Another problem is what happens when you need to store more data than the Java program has access to (i.e. in its heap)?. Well you could increase the amount memory the Java program has available, but if you are running on a 32 Bit Operating System (OS) this will be limited to 2 GB and even if you are on a 64 Bit OS this can cause problems. Java programs which use large amounts of memory can suffer from un-predictable pauses, anything upwards from 1-2 seconds depending on the amount of memory used. This happens when the Java Virtual Machine (JVM) tidies up all the un-used data (or performs a full Garbage Collection (GC)). For applications that need to provide a consistent level of service, e.g. a customer facing web application, this is often unacceptable.

Oracle Coherence address both of these problems. When lots of Coherence Java programs (or cache servers) are started on one or more connected computers they 'glue' themselves together (to form a cluster) using mulit-cast (or a specified IP of addresses). The look-up table, or cache, can then be spread or partitioned across all the Coherence Java programs (nodes) in the cluster. More nodes can be started to increase the size of the cache ,you just have to start more nodes and they will automatically join the cluster – no re-start  is required. Since a cache can be built from lots of nodes with a relatively small memory footprint processing outages, when the JVM is performing memory housekeeping chores (GC's), is also greatly reduced as you can run lots of JVM’s with a small memory footprint and the effect of any particular JVM GC is also diluted across the whole cluster.

To address the problem of loosing data, if a node or server running a number of nodes crashes, Coherence can keep backup copies of data on different nodes and servers. All changes to the primary copy of a value are synchronously made to the backup, to make sure they are always the same, and if either are lost a new primary or backup is created from the remaining value. Furthermore, Coherence will automatically re-organise the partitioned data in a cache if a failure occurs to ensure that it is re-balanced across the cluster and any in-flight client transactions or queries will still complete successfully.

So simplistically Coherence is just a in-memory lookup table. The clever thing that Coherence does is enable the lookup table to grow and be resilient.

How do I use it?

Well that's enough background information. To use Coherence you only need the Java runtime (1.4+). You can download the Oracle JRockit Java runtime and Coherence from the Oracle Technology Network.

  • Coherence can be downloaded from here. To install Coherence you just unzip it - that's it!.
  • Oracle JRockit JVM can be downloaded from here (if you do not already have Java installed)

In addition to being able to put data in a lookup table and look it up Coherence also supports queries, aggregations, transactions, events and other features. Here we will just concentrate on putting data in a cache and looking it up - to keep things quick and simple.

Coherence works in a sort of client-server mode. I say sort of because a client will make a direct request to the node in the cluster which has the data it wants, rather than go through a central server node or lookup service. This also means that Coherence has no 'single point of failure' or 'single point of bottleneck'. Every node in a cluster is effectively equal and is just responsible for its own data.

So what does a Coherence client look like?

Well the object we are going to cache looks like this:

package com.oracle.coherence.demo;

import java.io.Serializable;

import java.util.Date;

/**
* Order class representing an order for a shares
*/
public class Order
  implements Serializable
{
  private String symbol;
  private int quantity;
  private double amount;
  private Date timeStamp;

  /**
   * Default constructor
   */
  public Order()
  {
  }

  /**
   * Constructor
   * @param symbol the Order symbol, e.g. ORCL
   * @param quantity the number of shares
   * @param amount the amount of the shares to buy at
   * @param timeStamp a time stamp
   */
  public Order(String symbol, int quantity, double amount, Date timeStamp)
  {
    this.symbol = symbol;
    this.quantity = quantity;
    this.amount = amount;
    this.timeStamp = timeStamp;
  }

  /**
   * Gets the symbol
   * @return the symbol
   */
  public String getSymbol()
  {
    return symbol;
  }

  /**
   * Sets the new symbol
   * @param symbol the new symbol value
   */
  public void setSymbol(String symbol)
  {
    this.symbol = symbol;
  }

  /**
   * Gets the quantity
   * @return the quantity
   */
  public int getQuantity()
  {
    return quantity;
  }

  /**
   * Sets the new quantity
   * @param quantity the new quantity
   */
  public void setQuantity(int quantity)
  {
    this.quantity = quantity;
  }

  /**
   * Gets the amount
   * @return the amount
   */
  public double getAmount()
  {
    return amount;
  }

  /**
   * Sets the amount
   * @param amount the new amount
   */
  public void setAmount(double amount)
  {
    this.amount = amount;
  }

  /**
   * Gets the time stamp
   * @return the time stamp
   */
  public Date getTimeStamp()
  {
    return timeStamp;
  }

  /**
   * Sets the new time stamp
   * @param timeStamp the new time stamp
   */
  public void setTimeStamp(Date timeStamp)
  {
    this.timeStamp = timeStamp;
  }
}

The Coherence client application looks like this:

package com.oracle.coherence.demo;

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

import java.util.Date;
import java.util.Iterator;
import java.util.Set;

/**
* Simple Coherence cache client example
*/
public class SimpleCoherenceClient
{
  /**
   * Main method
   * @param args not used
   */
  public static void main(String[] args)
  {
    // Get a handle/reference to a cache by name - which does not have to exist
    NamedCache namedCache = CacheFactory.getCache("test");

    // Create an object value to put in the cache
    Order value = new Order("ORCL", 400, 19.4, new Date());

    // Create a key for the object
    Integer key = new Integer(0);

    // Put an item into the cache
    namedCache.put(key, value);

    // Retrieve the object based upon its key
    Order order = (Order) namedCache.get(key);

    System.out.println("------------------------------------------------------");
    System.out.println("Retrieved order: " + "symbol=" +
                       order.getSymbol() + ", amount=" +
                       order.getAmount() + ", quantity=" +
                       order.getQuantity() + ", timestamp=" +
                       order.getTimeStamp());
    System.out.println("------------------------------------------------------");

    // Shutdown client connection to cache
    CacheFactory.shutdown();
  }
}

As you can see the client code to add and retrieve an object from a Coherence cache is trivial. The above example can be downloaded from here, with all the files ready to run it. Look at the readme.txt file for instructions.

When you run a Coherence client all you need to do is include the Coherence Jar files in the CLASSPATH and either include the XML configuration file in the CLASSPATH or indicate its location through a system property. That's it. Through multi-cast it will locate the cluster on the default TCP port. Using a specific IP address etc. for clustering is simply a matter of overriding default configuration file settings.

So how fast is Coherence?

Well that depends on how much data you need to access, what your network is like, how fast your hardware is etc, but it is usually run on commodity hardware (blades and GBit Ethernet). For more information on performance and scalability see the results of our internal test here.

How much data can Coherence store?

Some Coherence customers have caches with 100's of GB of data and 100's of nodes. Others only cache a few GB of data.

Can I save cache entries to a database?

Yes. Coherence provides the facility to persist cache data to any database (using JPA, Hibernate or Toplink) synchronously or asynchronously. Its also very easy to plug-in persistence to everything from a file system to a Mainframe.

I use .NET of C++ not Java. Can I still use Coherence?

Coherence also comes with native .NET and C++ client libraries. When using these technologies, .NET or C++ objects are created to map onto Java objects and Coherence then transforms the .NET or C++ objects to and from the Java objects when the clients communicate with the Java cache servers.

 

There is obviously a lot more to Coherence than has been covered here, but hopefully this has given you a flavour of how to use it. For more information about the other features mentioned, examples and white papers etc,go to the Coherence home page on Oracle Technology Network.

Comments:

If you are using the Coherence ReadWriteBackingMap so that cache objects are mapped to rows in a database table(s) then if a client application performs a get() request and the data is not a in the cache, then Coherence can be configured to load the data from the database on-demand and return it to the client. See here for more information on the Coherence "read-through" mechanisum.

Posted by Dave Felcey on August 05, 2009 at 05:15 AM GMT #

if i want to use coherence with PHP, do i have to use coherence REST?

Still confused about the concept. With the oracle, we can store the data using a syntax that similar like sql. but when using Coherence REST, the concept is kind of difference, right?

It uses json. so does that mean that $GET is similar like SELECT and POST/ GET is similar like INSERT /UPDATE?

Regards,

ardi

Posted by Ardi on December 07, 2012 at 11:00 AM GMT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Views and ideas about Oracle Coherence and other software

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today