SpringBoot Hazelcast cache with PostgreSQL backend

Performance is a big problem in software development. And Caching is one solution to speed up system. Hazelcast is an open source in-memory data grid. So in the tutorial, JavaSampleApproach will show you how to use SpringBoot Hazelcast cache with PostGreSQL backend.

Related posts:
How to work with Spring Cache | Spring Boot
SpringBoot Caffeine cache with PostgreSQL backend
Couchbase – How to create Spring Cache Couchbase application with SpringBoot

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.7.RELEASE
– Hazelcast

II. SpringBoot Hazelcast

In the tutorial, we will create a SpringBoot project as below design:

Springboot Hazelcast Cache - Architecture

Spring Boot will auto-configure a HazelcastInstance when having Hazelcast on the classpath. And Spring Boot only configures HazelcastInstance if a Hazelcast configuration is found.

We use dependency hazelcast-spring:

hazelcast-spring had included below dependency:

We can configure Hazelcast via com.hazelcast.config.Config bean. Or use an hazelcast.xml configuration file.
In the application.properties, we specify hazelcast.xml configuration via:

An example of hazelcast.xml:

III. Practice

We create a SpringBoot project as below structure:

SpringBoot Hazelcast Cache - project structure

Step to do:
– Create a SpringBoot project
– Create data model
– Create JPA repository
– Implement cache service
– Initial data with CommandLineRunner
– Run and check results

1. Create a SpringBoot project

We use SpringToolSuite to create a SpringBoot project with below dependencies:

2. Create data model

Create a Customer data model:

Note: need implements Serializable for data model to cache with Hazelcast.

3. Create JPA repository

– Create a CustomerRepository repository:

– Open application.properties, add data source config:

4. Implement cache service
4.1 Configure Hazelcast

Create a file /src/main/resources/config/hazelcast.xml:

Open application.properties, specify hazelcast.xml location:

4.2 Implement Customer cache

For details about {@Cacheable, @CachePut, @CacheEvict}, we can refer at How to work with Spring Cache | Spring Boot.

4.3 Implement Customer services

5. Initial data with CommandLineRunner

Note: @EnableCaching is used to enable Hazelcast.

6. Run and check results

– Run the SpringBoot project -> console’s Log:

– Check PostGreSQL customer table:

SpringBoot Hazelcast Cache - customer table

– Make request 1: http://localhost:8080/api/cachable?id=1

-> Results:
Service process slowly and,

Springboot Hazelcast Cache - request 1

Server has displayed a text on console:

– Make request 2: http://localhost:8080/api/cachable?id=1
Now the response is faster because Customer with id = 1 has been cached before, the application just get data from cache storage.

Springboot Hazelcast Cache - request 2

– Make request 3: http://localhost:8080/api/cacheput?id=1&firstname=Peter
Message is returned on Browser:
Done
Now customer with id=1 is modified: firstname=’Peter’, NOT ‘Jack’.

– Make request 4: http://localhost:8080/api/cachable?id=1
Response is faster BUT the result is difference from first request:

Springboot Hazelcast Cache - request 4

– Make request 5 – Make a cache-evict request: http://localhost:8080/api/cacheevict?id=1
Browser displays:
Done
Now customer with id=1 was evicted from cache storage.

– Make request 6: http://localhost:8080/api/cachable?id=1
Now the behavior of the browser is the same as the first request because customer with id=1 was evicted to cache, and the method under @Cachable is executed.
Service process slowly and result:

Springboot Hazelcast Cache - request 6

IV Sourcecode.

SpringBootHazelcast


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*