Search code examples
javaspring-bootkubernetesopenshiftspring-cache

How to evict cache in multiple pods? I have two Openshift pods and I want to send a HTTP request for evict cache in both pods


I have two microservices pods in Openshift, I want to clean cache both microservices. I have a endpoint that clear-evict cache, the request only enter in one pod. How to avoid restart the pods for clear-cache? I use 2.1.10 springboot and I have a cron configurated every two hours but I want to clean cache in HTTP Request.

I use @Cacheable from org.springframework.cache.annotation in the request and org.springframework.cache.CacheManager.clear for evict/clean the cache.


Solution

  • Just like other services in the Spring Framework, the caching service is an abstraction, not an implementation and requires the use of an implementation to store the cache data. This abstraction is materialized by the org.springframework.cache.Cache and org.springframework.cache.CacheManager interfaces.

    You have not spoken about which is your Spring cache implementation, so I suppose that you are using the default implementation.

    By default, Spring chooses java.util.concurrent.ConcurrentMap. This implementation stores your data in-memory and will disappear it when you will close your JVM. For your concrete enviroment, with multiples pods, you need a cluster-aware cache implementation. You can't rely on cache implementations based on in-memory storage.

    So, you should check out implementations like Spring Redis Cache, which is a cluster-aware cache implementation, and configure it in all your pods.