Hazelcast - SirajChaudhary/comprehensive-example-on-microservices-using-spring-boot-with-spring-cloud GitHub Wiki
Caching in Spring Boot Microservices
- 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 named 'Hazelcast' (step5 to step7)
- Spring supported cache providers are JCache, Generic, EhCache 2.x, Hazelcast, Infinispan, Couchbase, Redis, Caffeine, Simple. We can use any of these as second layer of cache.
Step1: Add spring boot starter cache dependency in pom.xml
<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.
Points:
-
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 by referring this URL,
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.
NOW LETS IMPLEMENT HAZLECAST CACHE (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 in pom.xml
<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.
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 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.
You can see more properties in the following sample file to configure hazelcast.yaml
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