Sub topic 9
Choosing a cache provider
This chapter assumes you know Spring beans, Maven or Gradle dependencies, and application.properties. You have already enabled caching with @EnableCaching and used @Cacheable. Here you decide where the cached data actually lives.
What a provider is
The annotations such as @Cacheable do not store anything. They call a Cache, which a CacheManager hands out by name. A cache provider is the library that implements the storage behind those interfaces. Your service code stays the same when you change providers. Only the dependencies and configuration change.
How Boot picks a provider
Spring Boot auto-configures a CacheManager when it finds @EnableCaching. It checks the classpath for supported providers in a fixed order, and it uses the first one it can configure. If it finds none, it falls back to a simple ConcurrentHashMap cache.
The common choices are:
| Provider | Where data lives | Expiry and size limits | Typical use |
|---|---|---|---|
| Simple (default) | In the JVM heap | None | Tests and prototypes |
| Caffeine | In the JVM heap | Yes | One instance, or data that may differ slightly per instance |
| Redis | In a separate server | Expiry yes, size set on the server | Several instances that must share entries |
Setting the provider explicitly
Relying on classpath detection is fragile, because adding a library can change which provider wins. Set the type yourself:
spring.cache.type=caffeine
spring.cache.cache-names=products,productSearchIf the named provider is not on the classpath, the application fails at startup. That failure is useful, because you find out immediately instead of running on the wrong cache.
Decision criteria
Ask these questions in order.
- Do you run more than one instance? With a local cache, each instance holds its own copy. After
@CacheEvicton instance A, instance B still serves the old value until it expires. If that is unacceptable, use Redis. - How fast must a hit be? Caffeine reads from memory in the same process. Redis adds a network round trip and serialization.
- How large is the data? Caffeine shares the JVM heap with your application, so a large cache raises garbage collection pressure. Redis keeps the data outside the heap.
- Must the cache survive a restart? A local cache starts empty each time. Redis can keep entries across application restarts.
- Can you operate another server? Redis needs hosting, monitoring, and failure handling. Caffeine needs only a dependency.
Common mistakes
- Leaving the default in production. The simple provider never expires entries and has no size limit. Memory use grows until the process fails.
- Choosing a local cache for data that must be consistent. Evictions on one node do not reach the others.
- Assuming Redis values behave like Java objects. Values are serialized, so they must be serializable and must keep working across deployments.
- Forgetting dependencies. Caffeine needs
com.github.ben-manes.caffeine:caffeine. Redis needsspring-boot-starter-data-redis. Both needspring-boot-starter-cache.
The next chapters configure each provider in detail.
Exercise
Your application runs on three instances behind a load balancer. A product price changes, and the update method calls @CacheEvict. Users keep seeing the old price on some requests. You use Caffeine. What causes this, and what is one fix?
Show answer
Caffeine is a local cache, so each instance has its own copy. The eviction only clears the entry on the instance that handled the update. The other two instances keep serving the old value until it expires. One fix is to switch to Redis so all instances share one cache. Another is to set a short expireAfterWrite so stale entries age out quickly, if brief staleness is acceptable.