Your cart is currently empty!
Category: hazelcast
-
Khóa học miễn phí Setting up multi-node instances nhận dự án làm có lương
Hazelcast – Setting up multi node instances
Given that Hazelcast is a distributed IMDG and typically is set up on multiple machines, it requires access to the internal/external network. The most important use-case being discovery of Hazelcast nodes within a cluster.
Hazelcast requires the following ports −
-
1 inbound port to receive pings/data from other Hazelcast nodes/clients
-
n number of outbound ports which are required to send ping/data to other members of the cluster.
This node discovery happens in few ways −
-
Multicast
-
TCP/IP
-
Amazon EC2 auto discovery
Of this, we will look at Multicast and TCP/IP
Multicast
Multicast joining mechanism is enabled by default. is a way of communication form in which message is transmitted to all the nodes in a group. And this is what Hazelcast uses to discover other members of the cluster. All the examples that we have looked at earlier use multicast to discover members.
Example
Let’s now explicitly turn it on. Save the following in hazelcast-multicast.xml
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config http://www.hazelcast.com/schema/config/hazelcast-config-3.12.12.xsd" xmlns="http://www.hazelcast.com/schema/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <network> <join> <multicast enabled="true" /> </join> </network> </hazelcast>
And then, let us execute the following −
java -Dhazelcast.config=hazelcast-multicast.xml -cp .targetdemo-0.0.1- SNAPSHOT.jar com.example.demo.XMLConfigLoadExample
Output
In the output, we notice the following lines from Hazelcast which effectively means that multicast joiner is used to discover the members.
Jan 30, 2021 5:26:15 PM com.hazelcast.instance.Node INFO: [localhost]:5701 [dev] [3.12.12] Creating MulticastJoiner
Multicast, by default, accepts communication from all the machines in the multicast group. This may be a security concern and that is why typically, on-premise, multicast communication is firewalled. So, while multicast is good for development work, in production, it is best to use TCP/IP based discovery.
TCP/IP
Due to the drawbacks stated for Multicast, TCP/IP is the preferred way for communication. In case of TCP/IP, a member can connect to only known/listed members.
Example
Let’s use TCP/IP for discovery mechanisms. Save the following in hazelcast-tcp.xml
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config http://www.hazelcast.com/schema/config/hazelcast-config-3.12.12.xsd" xmlns="http://www.hazelcast.com/schema/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <network> <join> <multicast enabled="false" /> <tcp-ip enabled="true"> <members>localhost</members> </tcp-ip> </join> </network> </hazelcast>
And then, let’s execute the following command −
java -Dhazelcast.config=hazelcast-tcp.xml -cp .targetdemo-0.0.1-SNAPSHOT.jar com.example.demo.XMLConfigLoadExample
Output
The output is following −
INFO: [localhost]:5701 [dev] [3.12.12] Creating TcpIpJoiner Jan 30, 2021 8:09:29 PM com.hazelcast.spi.impl.operationexecutor.impl.OperationExecutorImpl
The above output shows that TCP/IP joiner was use to join two members.
And if you execute following command on two different shells −
java ''-Dhazelcast.config=hazelcast-tcp.xml'' -cp .targetdemo-0.0.1-SNAPSHOT.jar com.example.demo.MultiInstanceHazelcastExample
We see the following output −
Members {size:2, ver:2} [ Member [localhost]:5701 - 62eedeae-2701-4df0-843c-7c3655e16b0f Member [localhost]:5702 - 859c1b46-06e6-495a-8565-7320f7738dd1 this ]
The above output means that the nodes were able to join using TCP/IP and both are using localhost as the IP address.
Note that we can specify more IPs or the machine names (which would be resolved by DNS) in the XML configuration file.
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config http://www.hazelcast.com/schema/config/hazelcast-config-3.12.12.xsd" xmlns="http://www.hazelcast.com/schema/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <network> <join> <multicast enabled="false" /> <tcp-ip enabled="true"> <members>machine1, machine2....</members> </tcp-ip> </join> </network> </hazelcast>
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí Hazelcast – Introduction nhận dự án làm có lương
Hazelcast – Introduction
Distributed In-memory Data Grid
A data grid is a superset to distributed cache. Distributed cache is typically used only for storing and retrieving key-value pairs which are spread across caching servers. However, a data grid, apart from supporting storage of key-value pairs, also supports other features, for example,
-
It supports other data structures like locks, semaphores, sets, list, and queues.
-
It provides a way to query the stored data by rich querying languages, for example, SQL.
-
It provides a distributed execution engine which helps to operate on the data in parallel.
Benefits of Hazelcast
-
Support multiple data structures − Hazelcast supports the usage of multiple data structures along with Map. Some of the examples are Lock, Semaphore, Queue, List, etc.
-
Fast R/W access − Given that all the data is in-memory, Hazelcast offers very high-speed data read/write access.
-
High availability − Hazelcast supports the distribution of data across machines along with additional support for backup. This means that the data is not stored on a single machine. So, even if a machine goes down, which occurs frequently in a distributed environment, the data is not lost.
-
High Performance − Hazelcast provides constructs which can be used to distribute the workload/computation/query among multiple worker machines. This means a computation/query uses resources from multiple machines which reduces the execution time drastically.
-
Easy to use − Hazelcast implements and extends a lot of java.util.concurrent constructs which make it very easy to use and integrate with the code. Configuration to start using Hazelcast on a machine just involves adding the Hazelcast jar to our classpath.
Hazelcast vs Other Caches & Key-Value stores
Comparing Hazelcast with other caches like Ehcache, Guava, and Caffeine may not be very useful. It is because, unlike other caches, Hazelcast is a distributed cache, that is, it spreads the data across machines/JVM. Although Hazelcast can work very well on single JVM as well, however, it is more useful is a distributed environment.
Similarly comparing it with Databases like MongoDB is also of not much use. This is because, Hazelcast mostly stores data in memory (although it also supports writing to disk). So, it offers high R/W speed with the limitation that data needs to be stored in memory.
Hazelcast also supports caching/storing complex data types and provides an interface to query them, unlike other data stores.
A comparison, however, can be made with Redis which also offers similar features.
Hazelcast vs Redis
In terms of features, both Redis and Hazelcast are very similar. However, following are the points where Hazelcast scores over Redis −
-
Built for Distributed Environment from ground-up − Unlike Redis, which started as single machine cache, Hazelcast, from the very beginning, has been built for distributed environment.
-
Simple cluster scale in/out − Maintaining a cluster where nodes are added or removed is very simple in case of Hazelcast, for example, adding a node is a matter of launching the node with the required configuration. Removing a node requires simple shutting down of the node. Hazelcast automatically handles partitioning of data, etc. Having the same setup for Redis and performing the above operation requires more precaution and manual efforts.
-
Less resources needs to support failover − Redis follows master-slave approach. For failover, Redis requires additional resources to setup Redis Sentinel. These Sentinel nodes are responsible to elevate a slave to master if the original master node goes down. In Hazelcast, all nodes are treated equal, failure of a node is detected by other nodes. So, the case of a node going down is handled pretty transparently and that too without any additional set of monitoring servers.
-
Simple Distributed Compute − Hazelcast, with its EntryProcessor, provides a simple interface to send the code to the data for parallel processing. This reduces data transfer over the wire. Redis also supports this, however, achieving this requires one to be aware of Lua scripting which adds additional learning curve.
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc