Cache with Spring Boot and Hazelcast

Welcome, in this tutorial, we will implement a Hazelcast Cache example in a Spring Boot app.

Hazelcast is used to implement the cache in a distributed environment. This cache mechanism approach offers reliable performance and effective consistency. Hazelcast is a distributed, highly available, and scalable caching mechanism.

1. Introduction

Before going further in this tutorial, we will look at the common terminology such as introduction to Spring Boot and Lombok.

1.1 Spring Boot

  • Spring boot is a module that provides rapid application development feature to the spring framework including auto-configuration, standalone-code, and production-ready code
  • It creates applications that are packaged as jar and are directly started using embedded servlet containers (such as Tomcat, Jetty or, Undertow). Thus, no need to deploy the war files
  • It simplifies the maven configuration by providing the starter template and helps to resolve the dependency conflicts. It automatically identifies the required dependencies and imports them into the application
  • It helps in removing the boilerplate code, extra annotations, and xml configurations
  • It provides powerful batch processing and manages the rest endpoints
  • It provides an efficient jpa-starter library to effectively connect the application with the relational databases
  • It offers a Microservice architecture and cloud configuration that manages all the application related configuration properties in a centralized manner

1.2 Lombok

  • Lombok is nothing but a small library that reduces the amount of boilerplate Java code from the project
  • Automatically generates the getters and setters for the object by using the Lombok annotations
  • Hooks in via the Annotation processor API
  • Raw source code is passed to Lombok for code generation before the Java Compiler continues. Thus, produces properly compiled Java code in conjunction with the Java Compiler
  • Under the target/classes folder you can view the compiled class files
  • Can be used with Maven, Gradle IDE, etc.

1.2.1 Lombok features

valLocal variables are declared as final
varMutable local variables
@Slf4JCreates an SLF4J logger
@CleanupWill call close() on the resource in the finally block
@GetterCreates getter methods for all properties
@SetterCreates setter for all non-final properties
  • Generates implementations of equals(Object other) and hashCode()
  • By default will use all non-static, non-transient properties
  • Can optionally exclude specific properties
  • Generates String of class name, and each field separated by commas
  • Optional parameter to include field names
  • Optional parameter to include a call to the super toString method
  • Generates no-args constructor
  • Will cause compiler error if there are final fields
  • Can optionally force, which will initialize final fields with 0/false/null var – mutable local variables
  • Generates a constructor for all fields that are final or marked @NonNull
  • The constructor will throw a NullPointerException if any @NonNull fields are null val – local variables are declared final
  • Generates a constructor for all properties of the class
  • Any @NotNull properties will have null checks
  • Generates typical boilerplate code for POJOs
  • Combines – @Getter, @Setter, @ToString, @EqualsAndHashCode, @RequiredArgsConstructor
  • No constructor is generated if constructors have been explicitly declared
  • Implements the Builder pattern for object creation
  • The immutable variant of @Data
  • All fields are made private and final by default

Let us go ahead with the tutorial implementation but before going any further I’m assuming that you’re aware of the Spring boot basics.

2. Cache with Spring Boot and Hazelcast

2.1 Tools Used for Spring boot application and Project Structure

We are using Eclipse Kepler SR2, JDK 8, and Maven. In case you’re confused about where you should create the corresponding files or folder, let us review the project structure of the spring boot application.

spring cache - project structure
Fig. 1: Project structure

Let us start building the application!

3. Hazelcast on Docker

To start with this tutorial, I am hoping you have the hazelcast up and running in your localhost environment. For easy setup, I have the hazelcast up and running on the Docker environment. You can execute the below script using the docker-compose command to get the hazelcast container running on Docker in minutes. If you’re doing it for the first time the docker image will be downloaded from the docker hub.


    container_name: hazelcast
    image: "hazelcast/hazelcast:4.0.1"
      - "5701:5701"
    container_name: management-center
      - hazelcast
      MC_ADMIN_PASSWORD: myPassword11
      MC_ADMIN_USER: admin
      MC_INIT_CMD: "./mc-conf.sh cluster add -H=/data -ma hazelcast:5701 -cn dev"
    image: "hazelcast/management-center:4.0.3"
      - "10080:8080"
version: "3"

If everything goes well the container would be started successfully as shown in Fig. 1. You can use the docker ps -a command to confirm that the container is started successfully. For further information on docker basics, you can navigate to this tutorial.

spring cache - hazelcast on docker
Fig. 2: Hazelcast container on Docker

4. Creating a Spring Boot application

Below are the steps involved in developing the application.

4.1 Maven Dependency

Here, we specify the dependency for the Spring boot (Web and JPA, Spring doc Open API (for swagger interface)), H2 database, Java Faker (to generate the dummy data), Lombok, and Hazelcast. The updated file will have the following code.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <relativePath/> <!-- lookup parent from repository -->


    <description>Hazelcast cache implementation in spring boot</description>




        <!-- https://mvnrepository.com/artifact/com.github.javafaker/javafaker -->
        <!-- https://mvnrepository.com/artifact/com.hazelcast/hazelcast -->




4.2 Application yml file

Create a new YML file at the location: SpringbootHazelcast/src/main/resources/ and add the following code where we will define –

  • The h2 database connection and hibernate details
  • The h2 console details will be accessible at the following URL – http://localhost:10091/h2-console in the browser
  • The Swagger UI path will be accessible at the following URL – http://localhost:10091/swagger-ui-custom.html in the browser

You’re free to change the application or the database details as per your wish.


    include-stacktrace: never
  port: 10091
    name: springboot-cache-hazelcast
    driverClassName: org.h2.Driver
    password: ''
    url: 'jdbc:h2:mem:testdb'
    username: sa
      enabled: true
      path: /h2-console
    database-platform: org.hibernate.dialect.H2Dialect
      ddl-auto: create-drop
        show_sql: true
    path: /swagger-ui-custom.html

4.3 Java Classes

Let us write the important java class(es) involved in this application. For brevity, we will skip the following classes –

  • Employee.java – Entity class that will be persisted in the database
  • EmployeeRepository.java – Repository interface that extends the JpaRepository interface to perform the SQL operations
  • EmployeeService.java – Service class that interact with the DAO layer methods
  • DefaultEmployeesLoader.java – Bootstrap class to populate dummy data to the h2 database once the application is started successfully
  • EntityNotFoundException.java – Exception class for throwing the not found exception when the entity is not present in the database. The exception class is annotated with the HTTP 404 error response code

4.3.1 Implementation/Main class

Add the following code to the main class to bootstrap the application from the main method. Always remember, the entry point of the spring boot application is the class containing @SpringBootApplication annotation and the static main method.


package com.springboot.cache.hazelcast;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//lombok annotation
//spring annotation
public class SpringbootHazelcastApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootHazelcastApplication.class, args);
        log.info("Springboot and hazelcast cache application started successfully");

4.3.2 Configuration class

Add the following code to the configuration class where we will define the Faker and Hazelcast cache configuration.


package com.springboot.cache.hazelcast.config;

import com.github.javafaker.Faker;
import com.hazelcast.config.Config;
import com.hazelcast.config.ManagementCenterConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.springboot.cache.hazelcast.model.Employee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Locale;
import java.util.Map;

//spring annotation
public class BeanConfig {

    public Faker faker() {
        return new Faker(new Locale("en-US"));

    //hazelcast cache configuration
    public Config config() {
        final Config cfg = new Config();
        return cfg;

    public Map<integer, employee=""> employeeMap(final HazelcastInstance instance) {
        return instance.getMap("employeeMap");

    public HazelcastInstance instance(final Config cfg) {
        return Hazelcast.newHazelcastInstance(cfg);

    private ManagementCenterConfig manCenterCfg() {
        return new ManagementCenterConfig()
                // port number should be same on which the hazelcast management center is running

4.3.3 Controller class

Add the following code to the controller class to specify the different endpoints. The controller methods are annotated with the HTTP GET mapping annotation. The methods will return the response but first checks the data from the hazelcast cache. If present in the cache it will return it from the cache otherwise from the database.


package com.springboot.cache.hazelcast.controller;

import com.springboot.cache.hazelcast.model.Employee;
import com.springboot.cache.hazelcast.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

//lombok annotation
//spring annotation
@RequestMapping(path = "/employee")
public class EmployeeController {

    private EmployeeService service;
    private Map<Integer, Employee> employeeMap;

    //get employee by id
    @GetMapping(path = "/get/{id}")
    @ResponseStatus(code = HttpStatus.OK)
    public Employee getEmployee(@PathVariable("id") int id) {
        //first check if employeeMap has the employee details for the given employee id
        //if yes then return it. else fetch ir from the database
        return Objects.nonNull(employeeMap.get(id)) ? employeeMap.get(id) : service.getEmployee(id);

    //get all employees
    @GetMapping(path = "/get")
    @ResponseStatus(code = HttpStatus.OK)
    public List<Employee> getEmployees() {
        //fetch all employees from the database
        final List<Employee> employees = service.getEmployees();
        if (CollectionUtils.isEmpty(employees)) {
            log.info("Returning empty list");
        } else {
            //add employee to the cache
            log.info("Adding employees to the cache");
            for (final Employee e : employees) {
                employeeMap.put(e.getId(), e);

        return employees;

5. Run the Application

To execute the application, right-click on the SpringbootHazelcastApplication.java class, Run As -> Java Application.

// Fig. 3: Run the Application

6. Project Demo

When the application is started, open the Postman tool to hit the application endpoints. You are free to choose any tool of your choice and for this tutorial, we will use the spring swagger interface (accessible at the following URL – http://localhost:10091/swagger-ui-custom.html).

Application endpoints

-- HTTP GET endpoints –

//Endpoint name – Get all employees
//URL :: http://localhost:10091/employee/get

//Endpoint name – Get employee by id
//URL :: http://localhost:10091/employee/get/101

That is all for this tutorial and I hope the article served you whatever you were looking for. Happy Learning and do not forget to share!

7. Summary

In this section, you learned:

  • Spring boot and Hazelcast cache introduction
  • Steps to implement hazelcast in a spring boot application

You can download the sample application as an Eclipse project in the Downloads section.

8. Download the Project

This was an example of how to implement Hazelcast cache in a spring boot application.

You can download the full source code of this example here: Cache with Spring Boot and Hazelcast


An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button