Add a new configuration option "timeOffset" in Redis to introduce a random time offset within the range [0, timeOffset) added to the specified timeToLive. This is aimed at effectively preventing cache avalanche issues at a specific time point in scenarios involving bulk cache refreshing.

Comment From: wilkinsona

Thanks for the suggestion but I wonder if this change is too broad. As I understand it, cache avalanche is only a concern when adding many entries at the same time such that they would all expire at the same time. The change proposed here will affect all entries that are added to Redis, not just those that are added in bulk.

What API calls do you typically use to perform bulk inserts? I wonder if this would be better handled in a way that the randomised TTL only applies to those calls.

Comment From: liudaac

Thanks for the suggestion but I wonder if this change is too broad. As I understand it, cache avalanche is only a concern when adding many entries at the same time such that they would all expire at the same time. The change proposed here will affect all entries that are added to Redis, not just those that are added in bulk.

What API calls do you typically use to perform bulk inserts? I wonder if this would be better handled in a way that the randomised TTL only applies to those calls.

As you said, this change is indeed quite broad, but in actual usage, this configuration is generally not used in online services because its configuration dimensions are not refined to the CacheName level. In my view, it seems more like an example, providing developers with a direction for customized development.

My initial idea is to first add the configuration item "timeOffset" in Redis, and then change the type of "redis" in CacheProperties from Redis to a new type like (Redis, Map). This way, custom configurations can be applied to different CacheNames and also has a default configuration to fit dynamic caches. I did similar work in my open-source project a year ago.

Do you think this is necessary? If approved, I will work on this and submit a new pull request.

Comment From: wilkinsona

In my view, it seems more like an example, providing developers with a direction for customized development.

What you have proposed isn't an example. It will affect everyone using a Redis-based cache in a Spring Boot application.

change the type of "redis" in CacheProperties from Redis to a new type like (Redis, Map)

I don't think we can do that. It would be a breaking change that would affect everyone using a Redis-based cache yet it would only be of benefit to those performing bulk inserts.

I think it would be better to implement this when needed through your own RedisCacheManagerBuilderCustomizer bean rather than Spring Boot providing built-in support. The customizer can configure the TTL function as needed, both for the default cache configuration and for specific caches.

Thanks anyway for the proposal.