logo

Get in touch

Awesome Image Awesome Image

Java Spring Boot July 22, 2024

Hazelcast Integration with Spring Boot: A Step-by-Step Guide

Written by Mahipalsinh Rana

17,024

Hazelcast Integration with Spring Boot_ A Step-by-Step Guide

Hazelcast – an in-memory data grid – fits well with Spring Boot to provide a solid solution for developing distributed applications. In the ever-expanding landscape of modern computing, distributed systems have become the foundation for developing applications that are scalable, fault-tolerant, and high-performance.

A distributed system can be described as a network of communicating processes, or more accurately, computers that cooperate to provide a predefined service. These nodes further divide work into tasks that they collectively perform with the help of a network over which they share information.

The main properties of distributed systems include scalability, fault tolerance, concurrency and performance, and decentralization.

With the help of Hazelcast when used with Spring Boot, the developers and other professionals working on the projects can have full control over the dissemination of the data, its caching, and processing across the multiple nodes so that it can achieve the highest level of efficiency and scalability with the provision of enhanced fault tolerance.

This also promotes concurrency, that is the ability to perform tasks at the same time, and is also implemented in a distributed fashion for higher availability and scalability.

What is In-Memory Data Grid (IMDG)?

IMDG stands for in-memory data grid which is a networked or clustered set of computers designed to pool its RAM to allow applications to share data with other applications residing in the cluster.

IMDGs are actually designed for high-speed data processing and intended for use in constructing and implementing expansive applications that are apt for installation in massive computer server systems with more RAM than what is ordinarily ascertainable in a typical server.

Key Features and Characteristics of Imdgs Include

  1. In-Memory Storage: IMDGs preserve data in the main memory of multiple nodes of the supercomputer and thus are more efficient compared to disk-resident databases.
  2. Distributed Architecture: IMDGs store data at nodes of a network so that they can horizontally scale and tolerate faults. To maintain the availability and reliability of the data, it is divided, and copies are created that can be placed in various nodes.
  3. Parallel Processing: IMDGs take advantage and advantage of parallel processing of such operations, and threads on distributed data for high I/O throughputs and performance.
  4. High Availability: Each IMDG that is used in the system usually has the data material replicated in several nodes to have fault tolerance. In the case of the dying node, the data can also be recovered from other nodes within the cluster.
  5. Low Latency: Though, storing data in IMDGs minimizes the access time for data as compared to disk-based storage for storing and accessing data, as they require faster access time because some use cases really need real and low-time access.
  6. Caching: Some IMDGs contain caching modules; it is beneficial to store the most frequently utilized data in memory and thus, avoid constantly accessing backend databases or other systems.

IMDGs are therefore a critical factor for use in applications requiring superior performance, generation, and expedited data processing.

What is Hazelcast?

Hazelcast is an open-source, distributed, in-memory data grid (IMDG) and caching platform that speeds up, scales up, and scales out applications. It enables the distribution of computing in that you can store and process data in memory across several nodes of a cluster. This architecture provides very high availability, built-in redundancy, and equally elegant scalability.

Key Features of Hazelcast

Distributed Data Structures: The distributed data structures include maps queues lists and sets that are provided by Hazelcast. Such structures help you to implement efficient storage and processing of data among specific segments of the cluster.

In-Memory Storage: Caching in Hazelcast puts data into the memory across different nodes and ensures fast access to the frequently used data. This positively impacts the rate at which data is pulled off and processed relative to disk-based systems.

Distributed Computing: In terms of scalability Hazelcast enables distributed computing through deployment of tasks and computations over the cluster. This makes use of the parallel processing capabilities of the number of nodes to accomplish its task in the most optimized manner.

Scalability: The management of Hazelcast can be done easily by scaling out, the number of nodes in the cluster is increased. This capability gives the ability to scale up the numbers of data and traffic which is crucial when your application grows in popularity.

Caching: Hazelcast also includes a feature that provides a distributed caching that is capable of storing frequently used data in the cluster. This minimizes the load of having to pull data from other applications that are normally slower than the target application thus enhancing the application’s performance.

Cloud-Native Support: Hazelcast says that they can be deployed and run on clouds from AWS, Azure, Google Cloud Platform, etc. It works with Kubernetes and Docker for containerization, which helps in easily managing Hazelcast clusters in cloud environments.

Enterprise Features: Hazelcast offers enterprise-grade features such as advanced security, monitoring, management, and clustering capabilities. These features cater to the needs of enterprise deployments requiring robust security, detailed monitoring, and comprehensive management capabilities.

Hazelcast’s robust feature set makes it a powerful tool for building high-performance, scalable, and reliable distributed applications.

Read More: Jaeger Integration with Spring Cloud

HazelCast Implementation with Spring Boot

Step 1: Download and Install HazelCast Management Center Zip from

https://hazelcast.com/open-source-projects/downloads/

Download and Install HazelCast Management Center

Step 2:

Now go to the bin directory in hazelcast-management-center and run this command to start hazelcast-management-center on port 8202

sh start.sh 8202

Step 3:

Now go to this URL http://localhost:8202/ on which hazelcast-management-center will be started and create an admin user. After creating an admin user and logging in, you will get to see this dashboard of hazelcast-management-center.

3

 

Step 4:

Now go to Spring Initializr and create a sample spring boot project.

Step 5:

Make sure to add the following dependency for hazelcast in pom.xml.

<dependency>

<groupId>com.hazelcast</groupId>

<artifactId>hazelcast-all</artifactId>

<version>4.1.1</version>

</dependency>

Step 6:

Create Student Entity class, StudentService Interface, StudentServiceImpl class, and application.properties as follows:

Step 7:

Create one hazelcast.yaml file in /src/main/resources/ in which we will specify the url of hazelcast-management-center and cluster-name to which this spring boot app should be connected.

Step 8:

Now, create a controller class to perform CRUD operations.

Here, we have used a few of the annotations for caching our data:

@CacheConfig(cacheNames = “students”)

  • This annotation will streamline some of the cache configuration into a single place at the class level, so that we don’t have to declare things multiple times

Here the name of the cache that will be used for caching the data is students.

@Cacheable(key=”#id”)

As the name implies, we can use @Cacheable to demarcate methods that are cacheable — that is, methods for which the result is stored in the cache so that, on subsequent invocations (with the same arguments), the value in the cache is returned without having to actually invoke the method.

Here, we are caching the getStudentById() method based on its id field.

@CachePut(key= ‘#id’)

It is generally used with update methods if we want our cache to be updated with the result of the method execution.

@CacheEvict(key=’#id’)

The @CacheEvict annotation is used to remove one or more entries from a cache. When a method annotated with @CacheEvict is called, Spring will remove the cached data associated with the specified cache name and key (or keys) along with the method execution.

Step 9:

Now, run the Spring Boot app

Read More: Spring Security with Auth0 Integration

Step 10:

We can test that caching using Hazelcast is working by using various endpoints of the controller class using postman.

In the first call to the method in which we are using caching, the operation time will be very high since it will interact with the database. After that all subsequent calls to that method will fetch data from cache so it will be very quick.

Github Link to the demo Project:

https://github.com/AnkitKJSInexture/Hazelcast-With-Spring-Boot/tree/main

Meet the idealistic leader behind Inexture Solutions – Mahipalsinh Rana! With over 15 years of experience in Enterprise software design and development, Mahipalsinh Rana brings a wealth of technical knowledge and expertise to his role as CTO. He is also a liferay consultant with over a decade of experience in the industry. Apart from all he has technical background spans more than 15 years, making him a go-to authority for all things enterprise software development.

Bringing Software Development Expertise to Every
Corner of the World

United States

India

Germany

United Kingdom

Canada

Singapore

Australia

New Zealand

Dubai

Qatar

Kuwait

Finland

Brazil

Netherlands

Ireland

Japan

Kenya

South Africa