Spring Boot offers powerful caching capabilities through auto-configuration and a flexible abstraction layer. When you use caching annotations like @Cacheable
, @CachePut
, or @CacheEvict
without explicitly defining a CacheManager
or CacheResolver
bean, Spring Boot attempts to auto-detect an appropriate caching provider for you.
🛠️ How Spring Boot Chooses a Cache Provider
If you do not define a bean of type CacheManager
or a CacheResolver
named cacheResolver
, Spring Boot uses the following detection order to automatically choose a cache implementation:
đź“‹ Spring Boot Cache Provider Detection Order
- Generic Cache Provider
- This is a fallback mechanism when none of the other providers are available.
- It’s rarely used unless you're providing a custom implementation.
- JCache (JSR-107)
- Supports APIs that abstract various caching providers like:
- EhCache 3
- Hazelcast
- Infinispan
- To use this, add a JSR-107 compatible provider to your dependencies.
- Spring Boot configures the cache automatically via
javax.cache.spi.CachingProvider
.
- Hazelcast
- An in-memory data grid offering distributed caching.
- Requires Hazelcast instance on the classpath or configured programmatically.
- Infinispan
- A distributed in-memory key/value data store and cache.
- Automatically detected if on the classpath.
- Couchbase
- A NoSQL database that can be used as a cache provider.
- Integrates with Spring Cache through the Spring Data Couchbase module.
- Redis
- A popular in-memory key-value store.
- Detected if
spring-boot-starter-data-redis
is present. - Supports TTL, pub/sub, and distributed caching.
- Caffeine
- A high-performance Java caching library.
- Local cache with near-optimal hit rates and performance.
- Detected via
spring-boot-starter-cache
and Caffeine dependencies.
- Cache2k
- A zero-dependency caching library for high-throughput scenarios.
- Automatically picked up if on the classpath.
- Simple (ConcurrentMap-based) Cache
- The default fallback if no other provider is available.
- Uses
ConcurrentHashMap
under the hood. - Suitable for development or lightweight use cases.
⚙️ Configuration Tip
If you want explicit control, define your own CacheManager
bean:
java
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("books", "users");
}
Or use @EnableCaching
alongside CachingConfigurer
to customize the resolver and cache manager logic.
âś… Summary
Spring Boot gives you intelligent caching defaults with minimal setup. The auto-detection order helps it integrate smoothly with a wide range of caching technologies—from simple in-memory caches to robust distributed systems like Redis and Hazelcast.
By understanding the detection order, you can control and optimize how your application handles caching in production, staging, or dev environments.