In recent days we may seen application which get millions of hits per day like online flight/ train booking or hotel searching etc., Basically most of the search operations are mean to static and fixed data for some like list of flights available per day, list of hotels in Bangalore etc., When we see these kind of request to our application each request may need to hit database to get the appropriate results which makes multiple request to database for same type of request and makes application slow. By one way we can over come this problem by using load balancer or distributed network etc., But even then hitting to database for these requests are common and in some ways application can be faster but not scale-able to most extend. |
- Distributed implementations of java.util.{Queue, Set, List, Map}
- Distributed implementation of java.util.concurrent.ExecutorService
- Distributed implementation of java.util.concurrency.locks.Lock
- Distributed Topic for publish/subscribe messaging
- Transaction support and J2EE container integration via JCA
- Distributed listeners and events
- Support for cluster info and membership events
- Dynamic HTTP session clustering
- Dynamic clustering
- Dynamic scaling to hundreds of servers
- Dynamic partitioning with backups
- Dynamic fail-over
- Super simple to use; include a single jar
- Super fast; thousands of operations per sec.
- Super small; less than a MB
- Super efficient; very nice to CPU and RAM
By using Hazelcast we can achieve
- To reduce the load on database, deploy a distributed cache with several nodes running in the cluster
- Cache data from database
- Cached data is distributed equally between all the nodes
- To avoid cache from ballooning, keep expiry on items.
- Old untouched data from cache will expire from cache base on configuration.
- We can restrict using caching to limited nodes from the entier network. For example from total 10 cluster nodes if we need to use Hazelcast caching only in 5 nodes then we can done through Hazelcast configuration file. Or by default Hazelcast will deduct all nodes and makes first node as master.
Lets see simple example for using Hazelcast caching in java application and first lets see how to configure Hazelcast.
Download the latest Hazelcast release zip.
Unzip it and add the lib/hazelcast.jar to your class path.
Next we will create Java class and import Hazelcast libraries using any Java IDE. Below are the simple code sample to use Hazelcast.
import java.util.Map;
import java.util.Queue;
import com.hazelcast.config.Config;
import com.hazelcast.core.*;
public class HazelcastTesting {
public static void main(String[] args) {
Config cfg = new Config();
HazelcastInstance ins = Hazelcast.newHazelcastInstance(cfg);
Map<Integer, String> myMap = ins.getMap("names");
myMap.put(1, "IOS");
myMap.put(2, "Android");
myMap.put(3, "Java");
myMap.put(3, "Microsoft");
System.out.println("Second Name : "+myMap.get(2));
System.out.println("Map Size : "+myMap.size());
Queue<String> myQueue = ins.getQueue("brand");
myQueue.offer("Apple");
myQueue.offer("Samsung");
myQueue.offer("Nokia");
myQueue.offer("HTC");
myQueue.offer("Google");
System.out.println("First Brand Name : "+myQueue.poll());
}
}
OUTPUT:
May 06, 2014 3:47:56 PM com.hazelcast.instance.DefaultAddressPicker
INFO: Prefer IPv4 stack is true.
May 06, 2014 3:47:56 PM com.hazelcast.instance.DefaultAddressPicker
INFO: Picked Address[10.70.54.75]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
May 06, 2014 3:47:56 PM com.hazelcast.system
INFO: [10.70.54.75]:5701 [dev] Hazelcast Community Edition 3.1.7 (20140321) starting at Address[10.70.54.75]:5701
May 06, 2014 3:47:56 PM com.hazelcast.system
INFO: [10.70.54.75]:5701 [dev] Copyright (C) 2008-2014 Hazelcast.com
May 06, 2014 3:47:57 PM com.hazelcast.instance.Node
INFO: [10.70.54.75]:5701 [dev] Creating MulticastJoiner
May 06, 2014 3:47:57 PM com.hazelcast.core.LifecycleService
INFO: [10.70.54.75]:5701 [dev] Address[10.70.54.75]:5701 is STARTING
May 06, 2014 3:48:06 PM com.hazelcast.cluster.MulticastJoiner
INFO: [10.70.54.75]:5701 [dev]
Members [1] {
Member [10.70.54.75]:5701 this
}
May 06, 2014 3:48:06 PM com.hazelcast.core.LifecycleService
INFO: [10.70.54.75]:5701 [dev] Address[10.70.54.75]:5701 is STARTED
May 06, 2014 3:48:06 PM com.hazelcast.partition.PartitionService
INFO: [10.70.54.75]:5701 [dev] Initializing cluster partition table first arrangement...
Second Name : Android
Map Size : 3
First Brand Name : Apple
In above code we have created Map and Queue instance under Hazelcast instance. From output we can see using server socket localport and address are binding and allowing other nodes to listen. Also we can see new Hazelcast member created and allowed to listen through new port 5701 like
Members [1] {
Member [10.70.54.75]:5701 this
}
No comments:
Write comments