Cache using Hazelcast InMemory Data Grid

  •        0

We aggregate and tag open source projects. We have collections of more than one million projects. Check out the projects section.

Hazelcast is an open source In-Memory Data Grid (IMDG). It provides elastically scalable distributed In-Memory computing, widely recognized as the fastest and most scalable approach to application performance. Hazelcast makes distributed computing simple by offering distributed implementations of many developer-friendly interfaces from Java such as Map, Queue, ExecutorService, Lock and JCache. It is released under Apache license. 

Hazelcast is comparable to Redis, Memcache, it speaks Memcached protocol. It provides a convenient set of APIs to access the CPUs in your cluster for maximum processing speed. Hazelcast is highly scalable and available. Distributed applications can use Hazelcast for distributed caching, synchronization, clustering, processing, pub/sub messaging, etc. Hazelcast is implemented in Java and has clients for Java, C/C++, .NET, REST, Python, Go and Node.js. 

In this article we will explore how Map, MultiMap, Queue and Lock works with Hazelcast in a distributed environment. Most of the cloud applications are distributed and we need a caching service to store the frequently accessed objects across the services.

Download Hazelcast binaries and extract the archive. In command line, go to the hazelcast bin folder and start the service. Hazelcast service will get started.


$ cd C:\Hazelcast\hazelcast-3.11.3\bin

$ start.bat


$ cd /opt/hazelcast/hazelcast-3.11.3/bin

$ ./

We will take simple producer consumer as an example. Producer process add elements to data structure and Consumer process will retrieve it. Both the clients are in Java. In subsequent article, we will explain how to access Hazelcast from C++ and Go. 

Add below dependency to pom.xml



Create hazelcast instance, it will connect to localhost Hazelcast server which is running in the port 5701. 

   HazelcastInstance hzInstance = HazelcastClient.newHazelcastClient();


If the Hazelcast server is running in different machines or in different port, then you can mention its IP and connect to the server.

   ClientConfig clientConfig = new ClientConfig();
   ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
   networkConfig.addAddress("", "");

   /* Create a client side HazelcastInstance */
   HazelcastInstance hzInstance = HazelcastClient.newHazelcastClient( clientConfig );

While exiting the process, shutdown the Hazelcast instance.



First we explore Map, below code will add items to Map and read the same. 

public void addItemsToMap() {
   System.out.println("Write entries to Map");

   IMap<String, String> map = hzInstance.getMap("my-distributed-map");
   map.put("title", "Find best open source");

   // Put if absent
   String loginId = ""; // get from database.
   map.putIfAbsent(loginId, "123-xxxx-xxx");

   //replace item in the map.
   map.replace("sessionId-" + loginId, "123-xxxx-xxx", "123-456-xxxx-xxx");

public void readItemsFromMap() {
   System.out.println("Read entries from Map");

   IMap<String, String> map = hzInstance.getMap("my-distributed-map");
   System.out.println("Title from Map: " + map.get("title"));

   // Put if absent
   String loginId = ""; // get from database.
   System.out.println("loginId from Map " + map.get(loginId));

MultiMap is a map which can multiple values for a same key. 

public void addItemsToMultiMap() {
   System.out.println("Write entries to MultiMap");

   MultiMap<String, String> subscriberMap = hzInstance.getMultiMap("my-subscriber-multimap");
   subscriberMap.put("channel1", "subscriber1");
   subscriberMap.put("channel1", "subscriber2");

public void readItemsFromMultiMap() {

  System.out.println("Read entries from MultiMap");

  MultiMap<String, String> subscriberMap = hzInstance.getMultiMap("my-subscriber-multimap");
  Collection<String> values = subscriberMap.get("channel1");

Queue is a data structure where data first in will be first out (FIFO). Here we talk about distributed queue where one process adds items to the queue and other polls it. White reading from the queue, instead of queue.poll(), if queue.take() function is used then it will block the queue till it receives data. 

 public void addItemsToQueue() throws InterruptedException {
   System.out.println("Write entries to Queue");

   BlockingQueue<String> queue = hzInstance.getQueue("my-distributed-queue");
   for(int i =0; i < 10; i++) {
     queue.put("item--" + i);

 public void readItemsFromQueue() throws InterruptedException {
   System.out.println("Read entries from Queue");

   BlockingQueue<String> queue = hzInstance.getQueue("my-distributed-queue");
   System.out.println("Entries in queue " + queue.size());

   String item = null;
   do {
     item = queue.poll(1, TimeUnit.MINUTES);    /* waits for a minute and if there is no item it returns null. */

    System.out.print("Items in queue " + item + " , ");
   } while(item != null);

Lock - Acquire a resource lock. In case of distributed environment, there might be a situation where only one service should access a particular resource. In that case this distributed lock will help.

public void tryLock() throws InterruptedException {
   Lock lock = hzInstance.getLock("resource-lock");
   if (lock.tryLock() ) {
       System.out.println("Producer process acquired lock.");
       Thread.sleep(1000 * 30);

   else {
     System.out.println("Producer process does not acquire lock.");

Below is the sample code for Producer and Consumer class. Copy the respective function above and add to Producer and Consumer class.

public class Producer {

  private HazelcastInstance hzInstance = null;

  public Producer() {
     hzInstance = HazelcastClient.newHazelcastClient();

   public void shutdown() {

   public void addItemsToMap() {}
   public void addItemsToMultiMap() {}
   public void addItemsToQueue() { }
   public void tryLock() {} 


public class Consumer {

  private HazelcastInstance hzInstance = null;

  public Consumer() {
     hzInstance = HazelcastClient.newHazelcastClient();

  public void shutdown() {

   public void readItemsFromMap() {}
   public void readItemsFromMultiMap() {}
   public void readItemsFromQueue() {}
   public void tryLock() {}


Main Application class to try out the above functions.

 public class App 
  public static void main( String[] args )
     if (args.length != 1) {
        System.out.println("use argument either -producer or -consumer");

    String argType = args[0];
    if ("-producer".equals(argType)) {
         Producer producer = new Producer();

         try {
        catch(Exception exp) {
  else if ("-consumer".equals(argType)) {
     Consumer consumer = new Consumer();

   try {
     catch (Exception exp) {

Execute the client in two terminals.

$ java -jar hazelcast-client.jar -producer
$ java -jar hazelcast-client.jar -consumer





We publish blog post about open source products. If you are interested in sharing knowledge about open source products, please visit write for us

Subscribe to our newsletter.

We will send mail once in a week about latest updates on open source tools and technologies. subscribe our newsletter

Related Articles

Caching using Ehcache Java Library

  • ehcache cache java map key-value

Ehcache from Terracotta is one of Java's most widely used Cache. It is concurrent and highly scalable. It has small footprint with SL4J as the only dependencies. It supports multiple strategies like Expiration policies, Eviction policies. It supports three storage tiers, heap, off-heap, disk storage. There are very few caching products supports multiple tier storage. If you want to scale, you cannot store all items in heap there should be support for off-heap and disk storage. Ehcache is licensed under Apache 2.0. In this article, we can see about basic usage of Ehcache.

Read More

An Introduction to the UnQLite Embedded NoSQL Database Engine

  • database nosql embedded key-value-store

UnQLite is an embedded NoSQL database engine. It's a standard Key/Value store similar to the more popular Berkeley DB and a document-store database similar to MongoDB with a built-in scripting language called Jx9 that looks like Javascript. Unlike most other NoSQL databases, UnQLite does not have a separate server process. UnQLite reads and writes directly to ordinary disk files. A complete database with multiple collections is contained in a single disk file. The database file format is cross-platform, you can freely copy a database between 32-bit and 64-bit systems or between big-endian and little-endian architectures.

Read More

Quick Start Programming Guide for redis using java client Jedis

  • redis jedis redis-client programming database java

Redis is an open source (BSD licensed), in-memory data structure store, used also as a database cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. This article explains about how to communicate with Redis using Java client Jedis.

Read More

Angular Service Workers Usage Guide

  • angular service-worker offline-app

Web developers come across scenarios like web application completely breaks when workstation goes offline. Likewise to get into our application, every time we need to open a browser and then access it. Instead if it is in app, it will be easy to access for end-user. Push notifications similar to email client need to be done through web application. All these are addressed by a magic called service worker.

Read More

ETag Easy With RESTEasy

  • resteasy etag http-header rest-api

RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It comprises of frameworks for mock, embeddable server, rest client, proxy servers, logging and so on.In this article, we will walk-through ETag implementation and show the behaviour related to ETag done by rest easy framework. Example is developed using RESTEasy 3.7 and deployed in tomcat as RESTEasy framework is portable.

Read More

PySpark: Installation, RDDs, MLib, Broadcast and Accumulator

  • pyspark spark python rdd big-data

We knew that Apace Spark- the most famous parallel computing model or processing the massive data set is written in Scala programming language. The Apace foundation offered a tool to support the Python in Spark which was named PySpark. The PySpark allows us to use RDDs in Python programming language through a library called Py4j. This article provides basic introduction about PySpark, RDD, MLib, Broadcase and Accumulator.

Read More

Holistic usage guide for OpenSSL

  • openssl security certificate tools

OpenSSL is a general purpose cryptographty toolkit that provides an open source implementation of Transport Layer Security(TLS) and Secure Socket Layer(SSL) protocols. It is written in C,assembly and Perl language but wrappers are available in all languages. This article explains about OpenSSL commands.

Read More

How hashmap works in Java. My style of learning.

  • java hashmap opensource-learning

This is the most frequently asked questions in the interview. Googling will throw many links related to this topic. How to learn the implementation of hash map? My style of learning using open source learning technique.

Read More

An introduction to web cache proxy server - nuster

  • web-cache proxy-server load-balancer

Nuster is a simple yet powerful web caching proxy server based on HAProxy. It is 100% compatible with HAProxy, and takes full advantage of the ACL functionality of HAProxy to provide fine-grained caching policy based on the content of request, response or server status. This article gives an overview of nuster - web cache proxy server, its installation and few examples of how to use it.

Read More

Build Simple Ecommerce site using React and Jotai

  • ecommerce react jotai

Retail Ecommerce website can be created quickly with React. It can be created using React, Bootstrap, React DOM and Jotai. Data flow within the commerce site is done using Jotai in a light-weight manner. The main workflow of showing all the items in the gallery to user and user adding to the cart will be done as part of this blog.

Read More

Connect to MongoDB and Perform CRUD using Java

  • java mongodb database programming

MongoDB is a popular and widely used open source NoSQL database. MongoDB is a distributed database at its core, so high availability, horizontal scaling, and geographic distribution is quite possible. It is licensed under Server Side Public License. Recently they moved to Server Side Public License, before that MongoDB was released under AGPL. This article will provide basic example to connect and work with MongoDB using Java.

Read More

Thymeleaf - Text display, Iteration and Conditionals

  • thymeleaf template-engine web-programming java

Thymeleaf is a server-side Java template engine for both web and standalone environments. It is a better alternative to JavaServer Pages (JSP). Spring MVC and Thymeleaf compliment each other if chosen for web application development. In this article, we will discuss how to use Thymeleaf.

Read More

Getting Started with Spring Batch

  • spring-batch spring-boot batch-processing

The best way to design a system for handling bulk workloads is to make it a batch system. If we are already using Spring, it will be easy to add a Spring Batch to the project. Spring batch provides a lot of boiler plate features required for batch processing like chunk based processing, transaction management and declarative input/output operations. It also provides job control for start, stop, restart, retry and skip processing also.

Read More

Should web application store images in Database or File system?

  • database image-store filesystem

Web developers most frequent question, Should user images be stored in database or file system? Which is the best way. Both has some pros and cons.

Read More

LetsEncrypt certificate using ZeroSSL tools

  • ssl-certificate certificate security

Let’s Encrypt is a free, automated, and open Certificate Authority. It uses ACME protocol to validate your domain. If you have complete control over your domain, you can get a certificate for free. In order to provide secure access to your public network like HTTPS, LDAPS etc you need a certificate from a Certificate Authority. The cost of the certificate range from 10$ to 100$. If you want a wildcard certificate then it may cost more. The certificate is valid for one year and you need to pay and renew every year. Let's Encrypt comes for the rescue. You can create and renew certificate for few.

Read More

How Bitcoin works? A simple introduction.

Bitcoin is an open source digital currency which could be transferred in a P2P payment network. It is decentralized and it is not controlled by any central authority or banks. It is transferred from person to person and no authority will be aware of your transaction. Its quite different from PayPal or Banks.

Read More

Introduction to Apache Cassandra

  • cassandra database nosql

Apache Cassandra was designed by Facebook and was open-sourced in July 2008. It is regarded as perfect choice when the users demand scalability and high availability without any impact towards performance. Apache Cassandra is highly scalable, high-performance distributed database designed to handle large voluminous amounts of data across many commodity servers with no failure.

Read More

React Window - Optimized way to display large list

  • react react-window large-list

In Web development, there will be always a need to display content in a large list. Rendering a large list will slow down the web page load and increase the First Contentful Paint(FCP). Usually large lists are displayed in a paginated grid where user has to move around pages by clicking next. It will be good user experience, if the same page provides data of large list as and when we scroll.

Read More

React Reduce Vs Jotai

  • jottai react redux

While developing web applications, we built many components and having a data model across the application wide model will be difficult. We need common store management to update and get information across all the components. In React based Web UI, redux is the official centralized state management to be used which is internally wired as state changes to the component. There are other alternatives to redux which are light-weight and simple to use. One among them is Jotai, a minimalistic API for state management. In this blog we will take one of the redux examples given in the redux site and do the same in Jotai for deeper understanding.

Read More

Scene.js - Library to Create Timeline-Based Animation

  • scenejs css timeline javascript animation motion

Scene.js is a JavaScript timeline-based animation library for creating animation websites. As an animated timeline library, it allows you to create a chronological order of movements and positions of objects.

Read More

We have large collection of open source products. Follow the tags from Tag Cloud >>

Open source products are scattered around the web. Please provide information about the open source projects you own / you use. Add Projects.