KEMBAR78
Hazelcast Introduction | PDF
1
Introduction to Hazelcast
Sandeep Pandey
Motivative Data Structures behind Hazelcast development
[ Java Behind Hazelcast]
● HashMap
● ConcurrentHashMap
● Multicast Discovery Mechanisms
2
HashMap
● Hashmap is Array of LinkedList.
● HashMap is best fit for Caching.
● HashMap has two very important function i.e. put(K,V) and get(K).
How put(K,V) and get(K) work?
3
Node<K,V>
int hash;
K key ;
V value;
Node<K,V> next;
Concurrency in HashMap
HashMap is not thread safe
and throws
ConcurrentModificationException.
new MyThead(stores).start()
resulting
ConcurrentModificationException.
4
ConcurrentHashMap (CHM)
● ConcurrentHashMap also have same functionality as HashMap,
except in case of multiple thread access instead object lock, Segment
gets lock.
● Map<K,V> stores= new ConcurrentHashMap<>(int initialCapacity, float
loadFactor, int concurrencyLevel);
● By default initialCapacity (n) equals to concurrencyLevel (cl), unless
we specify. n segment gets created and only particular segment get
lock for operating thread.
○ If n=16 and cl =8 , One lock per 2 segments.
○ If n=16 and cl=32, Two thread will manage one segment
○ CHM never throws CME.
5
Discovery Mechanisms
● TCP/IP
○ List all or a subset of the members' hostnames and/or IP
addresses as cluster members.
● Multicast
○ Allows cluster members to find each other using multicast
communication.
This is not broadcasting!
6
Problem with Current Infrastructure
● Traditional system has single Database and that leads to
Performance issue.
● We can resolve performance issue by introducing cache, but there are
cache consistency issues. Also there is no way recover data if system
fails before data persist to DB.
7
Introducing Hazelcast
● Apache License
● Super light, simple, no-dependency (~7 MB Jar)
● Hazelcast cluster scales elastically
● Distributed/partitioned implementation of map, queue, set, list, lock
and executor service
● No Master, each member in the cluster configured to be functionally
same
● Build your own custom-distributed data structures using SPI.
8
Introducing Hazelcast..
● User defined object must implement Serializable interface (by java
provided or Hazelcast provided), to store in Hazelcast node else
exception thrown at Runtime.
● Hazelcast is thread safe as ConcurrentHashmap.
● Hazelcast configuration can be controlled in declarative or
programmatic way.
9
Hazelcast Architecture
10
Hazelcast Architecture..
● Hazelcast is an In-Memory Data Grid.
● Hazelcast is extension of ConcurrentHashMap, here object shared
between multiple JVM’s.
● Discovery mechanisms being use to connect JVM’s (Node)
● Data stored in-memory (RAM) of the servers.since it is incredibly fast.
● Multiple copies are stored in multiple machines for automatic data
recovery in case of failures.
11
Hazelcast Architecture..
12
13
Data Partitioning
13
● Hazelcast stores data in partitions
● Default partition number is 271
● Backups partitions for redundancy
● Partition table
● Repartition
○ When member joins the cluster
○ When member leaves the cluster
● Partition can be increased, if data size
per partition is bigger than 100 Mb.
● pid = hash(convert key to byte []) % pc
(271) *pid: partitionId pc: partitionCount
14
Hazelcast Topology
14
Client/Server Embedded
Cache
Replicated cache
15
Cache..
Distributed cache
16
Hazelcast use cases
● Highly available distributed cache for applications
● Publish/subscribe communication at highest speed and scalability
● An alternative to Coherence and Terracotta
● As storage for session data in web applications
● Parallel processing on multiple JVMs
17
Hazelcast Demo
1. Caching
2. User Defined Object for caching
18
Basic Configuration
1. Get Hazelcast specific object reference
Config cfg = new Config();
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
Map<Integer, String> citesMap = instance.getMap("mapName");
2. Configuration
hazelcast.xml is default file used for configuration of node, password,
discovery mechanism etc.
19
References:
http://docs.hazelcast.org/docs/latest-development/manual/html/index.ht
ml
20
Questions?
21

Hazelcast Introduction

  • 1.
  • 2.
    Motivative Data Structuresbehind Hazelcast development [ Java Behind Hazelcast] ● HashMap ● ConcurrentHashMap ● Multicast Discovery Mechanisms 2
  • 3.
    HashMap ● Hashmap isArray of LinkedList. ● HashMap is best fit for Caching. ● HashMap has two very important function i.e. put(K,V) and get(K). How put(K,V) and get(K) work? 3 Node<K,V> int hash; K key ; V value; Node<K,V> next;
  • 4.
    Concurrency in HashMap HashMapis not thread safe and throws ConcurrentModificationException. new MyThead(stores).start() resulting ConcurrentModificationException. 4
  • 5.
    ConcurrentHashMap (CHM) ● ConcurrentHashMapalso have same functionality as HashMap, except in case of multiple thread access instead object lock, Segment gets lock. ● Map<K,V> stores= new ConcurrentHashMap<>(int initialCapacity, float loadFactor, int concurrencyLevel); ● By default initialCapacity (n) equals to concurrencyLevel (cl), unless we specify. n segment gets created and only particular segment get lock for operating thread. ○ If n=16 and cl =8 , One lock per 2 segments. ○ If n=16 and cl=32, Two thread will manage one segment ○ CHM never throws CME. 5
  • 6.
    Discovery Mechanisms ● TCP/IP ○List all or a subset of the members' hostnames and/or IP addresses as cluster members. ● Multicast ○ Allows cluster members to find each other using multicast communication. This is not broadcasting! 6
  • 7.
    Problem with CurrentInfrastructure ● Traditional system has single Database and that leads to Performance issue. ● We can resolve performance issue by introducing cache, but there are cache consistency issues. Also there is no way recover data if system fails before data persist to DB. 7
  • 8.
    Introducing Hazelcast ● ApacheLicense ● Super light, simple, no-dependency (~7 MB Jar) ● Hazelcast cluster scales elastically ● Distributed/partitioned implementation of map, queue, set, list, lock and executor service ● No Master, each member in the cluster configured to be functionally same ● Build your own custom-distributed data structures using SPI. 8
  • 9.
    Introducing Hazelcast.. ● Userdefined object must implement Serializable interface (by java provided or Hazelcast provided), to store in Hazelcast node else exception thrown at Runtime. ● Hazelcast is thread safe as ConcurrentHashmap. ● Hazelcast configuration can be controlled in declarative or programmatic way. 9
  • 10.
  • 11.
    Hazelcast Architecture.. ● Hazelcastis an In-Memory Data Grid. ● Hazelcast is extension of ConcurrentHashMap, here object shared between multiple JVM’s. ● Discovery mechanisms being use to connect JVM’s (Node) ● Data stored in-memory (RAM) of the servers.since it is incredibly fast. ● Multiple copies are stored in multiple machines for automatic data recovery in case of failures. 11
  • 12.
  • 13.
    13 Data Partitioning 13 ● Hazelcaststores data in partitions ● Default partition number is 271 ● Backups partitions for redundancy ● Partition table ● Repartition ○ When member joins the cluster ○ When member leaves the cluster ● Partition can be increased, if data size per partition is bigger than 100 Mb. ● pid = hash(convert key to byte []) % pc (271) *pid: partitionId pc: partitionCount
  • 14.
  • 15.
  • 16.
  • 17.
    Hazelcast use cases ●Highly available distributed cache for applications ● Publish/subscribe communication at highest speed and scalability ● An alternative to Coherence and Terracotta ● As storage for session data in web applications ● Parallel processing on multiple JVMs 17
  • 18.
    Hazelcast Demo 1. Caching 2.User Defined Object for caching 18
  • 19.
    Basic Configuration 1. GetHazelcast specific object reference Config cfg = new Config(); HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg); Map<Integer, String> citesMap = instance.getMap("mapName"); 2. Configuration hazelcast.xml is default file used for configuration of node, password, discovery mechanism etc. 19
  • 20.
  • 21.