Monday, September 13, 2010

Hazelcast - a simple distributed caching alternative

I have over the last couple years used EhCache either via Spring (with AOP) or having it configured as hibernates' cache. I have never had the time or the need to look into the whole distributed caching available with Terracotta. However I recently stumbled on to Hazelcast and read the following:

"Hazelcast is pure Java. JVMs that are running Hazelcast will dynamically cluster. Although by default Hazelcast will use multicast for discovery, it can also be configured to only use TCP/IP for environments where multicast is not available or preferred. Communication among cluster members is always TCP/IP with Java NIO beauty. Default configuration comes with 1 backup so if one node fails, no data will be lost. It is as simple as using java.util.{Queue, Set, List, Map}. Just add the hazelcast.jar into your classpath and start coding."

I was instantly intrigued. So it sounds simple enough...Open IDE...jump in... being a maven fan, I figure, why download and manually "install" when you can just use maven, so I search around a little and add the following to a pom.

Edit: 1.8.4 is now outdated check http://www.hazelcast.com/downloads.jsp for the latest.

From the online documentation, I saw there was an InstanceListener interface. I create a simple Main class, that implements that and lists the instances.



So running this just adds a listener to the Hazelcast instance. According to the documentation Hazelcast allows for the following (and lots more):
# Distributed java.util.{Queue, Set, List, Map}
# Querying distributed data

I created a couple Main classes to run these separately, so that I could see if they are usable across different JVM executions.








So within minutes... I have a distributed cache across 4 applications, that I can monitor, control, query... (and btw. Hazelcast allows you to add indexes to the caches, same as a database to optimize queries).
If I was to place these 4 applications on 4 different machines, they will by default cluster (multicast for discovery) and have built in failover, monitoring and even persistence if required.

Coming from an environment where we have dozens of servers all caching data, this has got to be one of the most exciting open source products I have come across. Now I just need to convince the powers that be to prototype this and this may be the first open source project I actually try get involved in. The possibilities I am seeing for this type of functionality and then integrating it with the Spring Framework may actually make my brain explode.

9 comments:

  1. This sounds awesome - does anyone use it yet? It sounds a lot like memcache but it persists the data. I like that.

    ReplyDelete
  2. Thanks for this article. I've tried to run your example on my laptop but got confused by the result I received.

    1:) If I run AddThingsToCache and UseTheCache once, i got list has 10000 items and map has 10000 items as expected.
    2:) If I run AddThingsToCache and then UseTheCache again, I am getting incorrect result for the number of items in the list. e.g. 19877!

    Am I doing something wrong?

    ReplyDelete
  3. Hi,
    How can we achieve the statistics of the map, like hits , access\requests.

    Benny

    ReplyDelete
  4. Best place to ask this would be in the google group. I personally have not spent time looking at the monitoring.
    http://groups.google.com/group/hazelcast

    ReplyDelete
  5. Here I’d like to mention another Distributed Cache alternative "NCache”. I experienced that this is the most mature solution and has more features than all other options. The option that you mentioned is supported in java only but NCache is supported in .NET and JAVA both. But I’d recommend NCache and Hazelcast both as I read the details of Hazelcast and I found it useful too.

    ReplyDelete
  6. Just come across this excellent open source project , hazelcast is truly amazing it provides not only distributed cache but also failover and scalability. great software.

    Javin
    Difference between ClassNotFoundException vs NoClassDefFoundError

    ReplyDelete
  7. Regarding clustering, did you ever try to compare with glassfish v3.1 capabilities ?

    ReplyDelete
  8. Hazelcast version 1.8.4 is quite old. Make sure to use the latest hazelcast version available at http://www.hazelcast.com/downloads.jsp.

    -talip
    Hazelcast

    ReplyDelete
  9. Talip, I now updated the post to include the download link, yeah 1.8.4 was a while back.

    ReplyDelete

Popular Posts

Followers