Hazelcast most advanced in‐memory data grid (IMDG) - Yash-777/MyWorld GitHub Wiki

Hazelcast In-Memory Data Grid IMDG Doc

Hazelcast Clusters

Discovering Members by Multicast

With the multicast auto-discovery mechanism, Hazelcast allows cluster members to find each other using multicast communication. The cluster members do not need to know the concrete addresses of the other members, they just multicast to all the other members for listening. It depends on your environment if multicast is possible or allowed.

Hazelcast Tutorial Youtube we set up a basic Hazelcast cluster. We will create multiple Hazelcast nodes. We will create Eclipse maven project.

<img src="https://hazelcast.com/wp-content/themes/hazelcast/assets/img/hazelcast-logo.svg" alt="Hazelcast" height="22px" width="169px">


Hazelcast mvnrepository com.hazelcast:5.3.6 is an in-memory computing platform that provides distributed data structures and computing capabilities. It allows you to manage large-scale, high-performance data across multiple servers, which can be used for caching, distributed computing, and other in-memory solutions. Hazelcast supports both client-server models and single-machine configurations, enabling flexible and scalable architecture designs.

  1. Client-Server Model In the client-server model, you have a Hazelcast cluster running on the server-side (with multiple nodes), and one or more clients can connect to the cluster for data access and computation.
    • Scenario: A web application that needs to access distributed cache for faster retrieval of frequently accessed data (e.g., user sessions, product catalogs, etc.).
    • Usage: The client sends requests for data or computation, and the server-side Hazelcast cluster processes these requests and returns the result. The client does not need to have all data in memory but relies on the server cluster for distributed storage.
// Server-side Hazelcast configuration (cluster setup)
Config config = new Config();
config.getNetworkConfig().setPublicAddress("localhost:5701");
HazelcastInstance server = Hazelcast.newHazelcastInstance(config);

// Client-side Hazelcast configuration
ClientConfig clientConfig = new ClientConfig();
clientConfig.getNetworkConfig().addAddress("localhost:5701");
HazelcastClient.newHazelcastClient(clientConfig);

// Access distributed map on the client
IMap<String, String> map = client.getMap("myMap");
map.put("key", "value");
String value = map.get("key");

In this configuration, the client communicates with the Hazelcast server instance for data. The cluster can scale horizontally by adding more nodes, and Hazelcast takes care of data distribution and fault tolerance.

  1. Single-Machine Model In the single-machine model, all Hazelcast instances run on a single node, and there's no need for clustering. This is often used for development, testing, or low-traffic environments where the overhead of clustering isn't necessary.
    • Scenario: A local application or microservice running on a single machine needs caching or distributed data structures but doesn't require multiple nodes.
    • Usage: Hazelcast runs on a single machine, and all data and operations are contained within that machine. This simplifies deployment but limits scalability.
String imapName = "myMap";
// Single node setup
MapConfig iMapConfig = new MapConfig().setName( imapName );
Config config = new Config()
                .setInstanceName("HAZELCAST_INSTANCE")
                .addMapConfig( iMapConfig );
HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);

// Access a distributed map
IMap<String, String> map = instance.getMap( imapName ); // instance.getMap("myMap")
map.put("key", "value");
String value = map.get("key");

In this case, all operations occur on a single Hazelcast instance running on one machine, and no clustering or partitioning occurs.

HazelcastConfiguration to get HazelcastInstance (Single-Machine Setup)

hazelcast-<VERSION>.jar ~ Community Edition

<dependency>
  <groupId>com.hazelcast</groupId>
  <artifactId>hazelcast</artifactId>
  <version>3.10.6</version>
</dependency>
@Autowired
private HazelcastInstance hazelcastInstance;
@Configuration
public class HazelcastConfiguration {
  static String imapName = "MLM_CACHE_MAP"; // myDistributedMap
  /*
  Maximum number of seconds for each entry to stay in the map. Entries that are
  older than `time-to-live-seconds` and are not updated for `time-to-live-seconds` are automatically evicted from the map.
  Any integer between 0 and Integer.MAX_VALUE. 0 means infinite. Default is 0.
   */
  static int timeToLiveSeconds = 0;

  public Config hazelCastConfig() {
/*
This is used to limit the size of the map. It is helpful for setting a threshold on how much memory your map will use. When the map exceeds this size, an eviction policy (like LRU or LFU) can be applied to remove entries.

MaxSizePolicy can be either FREE_HEAP_SIZE (based on available heap memory) or PER_NODE (based on the number of entries per node).

You commented out the MaxSizeConfig, but it could be useful if you want to control the maximum size of your map. For instance, if you want to limit it to 200 entries and use the heap size policy:
*/
    int maxSizeOfMap = 200;
    MaxSizeConfig maxSizeConfig = new MaxSizeConfig(maxSizeOfMap, com.hazelcast.config.MaxSizeConfig.MaxSizePolicy.FREE_HEAP_SIZE);
    
    MapConfig setTimeToLiveSeconds = new MapConfig()
//This defines the name of the map. Every distributed map in Hazelcast has a unique name, and it's important to set this for identification.
        .setName( imapName )
        .setMaxSizeConfig( maxSizeConfig )
// This specifies how entries will be removed when the map exceeds the defined MaxSize
        .setEvictionPolicy( com.hazelcast.config.EvictionPolicy.LRU ) // Receiver: Least Recently Used
        .setTimeToLiveSeconds( timeToLiveSeconds ); // 3600 seconds = 1 hour
    
    Config config = new Config()
        .setInstanceName("HAZELCAST_INSTANCE")
        .addMapConfig( setTimeToLiveSeconds );
    config.setProperty("hazelcast.jmx", "true");
    config.setProperty("hazelcast.jmx.detailed", "true");
    
    NetworkConfig network = config.getNetworkConfig();
    JoinConfig join = network.getJoin();
    join.getMulticastConfig().setEnabled(false);
    return config;
  }

  @Bean
  public HazelcastInstance hazelcastInstance() {
    return Hazelcast.newHazelcastInstance(hazelCastConfig());
  }
}

IMap Class package changed in the following versions. hazelcast-config-3.10.xsd, hazelcast-config-5.3.xsd

/hazelcast-config-3.10.xsd (3.10.6) -> import com.hazelcast.core.IMap;
/hazelcast-config-5.3.xsd (5.3.6)   -> import com.hazelcast.map.IMap;

In case of performance `3.10.6` is much faster than `5.3.6` in Single-Machine Model

Key Use Cases of Hazelcast:

  • Distributed Caching: Store data across a cluster of machines for fast access and reduced database load.
  • Distributed Data Structures: Collections like maps, queues, sets, and lists that are distributed across multiple nodes.
  • Event Processing: Hazelcast can be used for stream processing and managing event-driven architectures.
  • Distributed Computing: Tasks can be parallelized and processed across a cluster.

To configure a Hazelcast cluster or client, you can choose either static or dynamic configuration.

NOTE: Hazelcast performs schema validation through the hazelcast-config-<VERSION>.xsd file, which comes with Hazelcast libraries.

Hazelcast Instance. Each instance is a member and/or client in a Hazelcast cluster. When you want to use Hazelcast's distributed data structures, you must first create an instance. Multiple Hazelcast instances can be created on a single JVM.


⚠️ **GitHub.com Fallback** ⚠️