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.

Windows:

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

$ start.bat

Linux:

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

$ ./start.sh

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

  <dependency>
     <groupId>com.hazelcast</groupId>
     <artifactId>hazelcast-client</artifactId>
      <version>3.11</version>
 </dependency> 

 

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("192.168.100.1:5701", "192.168.100.2:5701");

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

While exiting the process, shutdown the Hazelcast instance.

  hzInstance.shutdown();

 

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 = "student@college.com"; // 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 = "student@college.com"; // 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");
  System.out.println(values);
}

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);
    System.out.println();
 }

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);

       lock.unlock();
   }
   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() {
     hzInstance.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() {
    hzInstance.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");
        System.exit(0);
    }

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

         try {
            producer.addItemsToMap();
            producer.addItemsToMultiMap();
            producer.addItemsToQueue();
            producer.tryLock();
        }
        catch(Exception exp) {
           exp.printStackTrace();
   }
    producer.shutdown();
  }
  else if ("-consumer".equals(argType)) {
     Consumer consumer = new Consumer();

   try {
         consumer.readItemsFromMap();
         consumer.readItemsFromMultiMap();
         consumer.readItemsFromQueue();
         consumer.tryLock();
    }
     catch (Exception exp) {
        exp.printStackTrace();
    }
    consumer.shutdown();
  }
}
}

Execute the client in two terminals.

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

 

Reference:

https://hazelcast.org/

 


Sponsored:
To find embedded technology information about MCU, IoT, AI etc Check out embedkari.com.


   

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


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


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


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


Univention Corporate Server - An open source identity management system

  • ucs identity-management-system

Univention Corporate Server is an open source identity management system, an IT infrastructure and device management solution and an extensible platform with a store-like App Center that includes tested third party applications and further UCS components: This is what Univention combines in their main product Univention Corporate Server, a Debian GNU/Linux based enterprise distribution. This article provides you the overview of Univention Corporate Server, its feature and installation.

Read More


RESTEasy Advanced Guide - Filters and Interceptors

  • resteasy rest-api filters interceptors java

RESTEasy is JAX-RS 2.1 compliant framework for developing rest applications. It is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It is a fully certified and portable implementation of the JAX-RS 2.1 specification, a JCP specification that provides a Java API for RESTful Web Services over the HTTP protocol.

Read More


Ngnix - High Performance Web Server, Proxy Server, Content Cache and Reverse Proxy

  • load-balancer proxy-server web-server

Nginx is a High Performance Web Server, Proxy Server, Content Cache and Reverse Proxy server. It can also be used as mail proxy server and a generic TCP/UDP proxy server. Nginx claims to be more efficient and faster in the Web space compared to the other web servers. This can be evident with the architecture which is based on asynchronous event-driven approach. The event driven architecture enables to scale to hundreds / thousands of concurrent connections.

Read More


An introduction to MongoDB

  • mongodb database document-oriented-databse no-sql c++ data-mining

MongoDB is the most exciting SQL-free database currently available in the market. The new kid on the block, called MongoDB is a scalable, high-performance, open source, schema free and document oriented database that focuses on the ideas of NoSQL Approach. Written in C++, it has taken rapid strides since its emergence into the public sphere as a popular way to build your database applications.

Read More