How to set up a cache fallback in SpringBoot when unable to connect to Redis?

Viewed 678

There is a SpringBoot (v2.2.7) application, where a Redis cache is configured

fragment of pom.xml

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

When the Redis service is up and available, caching works as expected: results of methods annotated with @Cacheable are being cached. Unfortunately, when Redis service is not available any call to a cacheable method leads to an exception RedisConnectionFailureException: Unable to connect to Redis.

I guess it is reasonable if the application could work (execute business logic) independently from the cache availability.

Possible solutions are:

  1. custom implementation (i.e. a wrapper handling errors around the Redis cache)
  2. standard configuration in Spring considering Redis cache service health (if there is such thing)

What is the proper way to set up a fallback cache in SpringBoot?

0 Answers
Related