Skip to content

Hazelcast

SirajChaudhary edited this page Sep 20, 2022 · 20 revisions

how to setup Hazelcast

Hazelcast Setup

Note: Lets divide cache implementation for spring-boot microservices in two phases. In the first phase we will implement first layer of cache using spring provided caching with spring-boot-starter-cache dependency (step1 to step4) and then on top of it in phase two we will implement a third party cache technology (spring supported cache providers are JCache, Generic, EhCache 2.x, Hazelcast, Infinispan, Couchbase, Redis, Caffeine, Simple)

Step1: Add spring boot starter cache dependency

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

Step2: Enable caching by putting following annotation on main class
@EnableCaching

Step3: Use following different cache annotations as per needs on service layer
@Cacheable: It is used on the method level to let spring know that the response of the method are cacheable.
@CachePut: It updates the cache. The updateXXX method will always be executed and updates the cache.
@CacheEvict: It is used when we need to evict (remove) the cache previously loaded of data. it will clear the cache.
@Caching: Regroups multiple cache operations to be applied on a method.
@CacheConfig: Shares some common cache-related settings at class-level.

Note:
-We can define multiple cache on a method
@Cacheable({"myCache1", "myCache2"})
-We can define cache on a condition
@Cacheable(cacheNames="myCache", condition="#name.length() < 32")
-We can set many other cache properties
https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#cache

Step4: You can add following line into the properties file to show the query on console which hit the DB. If the data will be retrieved from cache rather DB then the query won't be displayed on console.
spring.jpa.show-sql=true

Note: Spring by default uses ConcurrentHashMap as cache. We will replace it to Hazelcast cache.

LETS IMPLEMENT HAZLECAST CACHE NOW. (STEP5 TO STEP7)

-We have to use Hazelcast as Cache Manager.

Step5: You need to mark request(VO), response(DTO) objects with Serializable interface

Step6: Add Hazelcast dependency

<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast-all</artifactId>
    <version>4.0.2</version>
</dependency>

Step7: Create hazelcast.yaml on classpath and add hazelcast configuration into it.
Note: You can create hazelcast.yaml or hazelcast.xml or @Bean for hazelcast configuration

hazelcast:
  network:
    join:
      multicast:
        enabled: true
  instance-name: hazelcast-instance
  map:
    default:
      # Note: Maximum number of seconds for each entry to stay in the Cache.
      time-to-live-seconds: 60
      # Maximum number of seconds each entry can stay in the Near Cache as untouched (not read).
      max-idle-seconds: 60
      eviction:
        # You need to select cache evict policy such as None, LRU, LFU, Random 
        eviction-policy: LRU
        # Maximum size policy for eviction of the Cache.
        max-size-policy: PER_NODE
        # Maximum size (entry count) of the Cache.
        size: 5000

-You can see more properties in the following sample file to configure hazelcast.yaml
https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/resources/hazelcast-full-example.yaml

Note: You need to set cache evict policy in hazelcast.yaml such as,
Eviction policy has following values:

  • NONE (by default): No eviction.
  • LRU: Least recently used entries will be removed.
  • LFU: Least frequently used entries will be removed.
  • RANDOM: Randomly selected entries will be removed.

Reference:
https://hazelcast.com/blog/spring-boot/
https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/test/resources/hazelcast-fullconfig-without-network.yaml
https://spring.io/guides/gs/caching/
https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/boot-features-caching.html
https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/boot-features-caching.html#boot-features-caching-provider-hazelcast
https://docs.spring.io/spring-framework/docs/current/reference/html/integration.html#cache
https://www.youtube.com/watch?v=cN8-4_Eka9A

Clone this wiki locally