In Spring Boot, @Cacheable caches a method's return value so repeated calls with the same arguments are answered from memory (or Redis) instead of running the method again. Boot makes this easy: add spring-boot-starter-cache, put @EnableCaching on a configuration class, pick a provider such as Caffeine, and configure expiry in application.yml.
The result is lower latency and far fewer database or HTTP calls for read-heavy data, with almost no change to your service code.
The @Cacheable annotation itself comes from Spring Framework. Spring Boot contributes the setup: CacheAutoConfiguration detects a supported cache library on the classpath and creates the CacheManager for you. Providers it can configure include Caffeine, Redis, Hazelcast, Infinispan, JCache implementations such as Ehcache 3, and a simple in-memory map used when nothing else is present.
You can name the provider explicitly with spring.cache.type and declare cache names up front with spring.cache.cache-names.
Spring Boot manages the Caffeine version, so you do not specify one.
The first call for an id hits the database; calls within ten minutes return the cached ProductDto.
When you run several instances, an in-process cache gives each its own copy. Switch to Redis so all instances share one cache.
This needs spring-boot-starter-data-redis. Values are serialized, so cached classes must be serializable with the configured serializer. Records and plain DTOs with a JSON serializer are a good fit.
For data that changes elsewhere, rely on a short time-to-live. For data you change yourself, evict or update on every write path.
With Spring Boot Actuator you can expose the caches endpoint to list and clear caches, and Micrometer publishes metrics such as hit and miss counts for supported providers. For Caffeine, call recordStats() in the spec (recordStats in spring.cache.caffeine.spec) to get hit-rate figures.
Restrict access to these endpoints, because caches allows evicting entries.
| Symptom | Likely cause |
|---|---|
| Method runs every time | @EnableCaching missing, or self-invocation, or a non-public method |
| Stale data | No eviction on writes and no time-to-live |
NotSerializableException with Redis | Cached type is not serializable |
| Memory grows without bound | Default simple cache with no size limit |
| Wrong value returned for similar methods | Two methods share a cache name and the same key |
Yes. Boot auto-configures the cache manager only when caching is enabled, so add @EnableCaching to a configuration class.
Caffeine for a single instance, Redis when several instances must share data, and the simple map only for tests and demos.
Configure it on the provider, for example expireAfterWrite=10m for Caffeine or time-to-live for Redis. The annotation has no expiry attribute.
The providers are thread-safe, but two threads can compute the same missing key at once unless you set sync = true.
In Spring Boot, @Cacheable plus spring-boot-starter-cache, @EnableCaching and a provider gives you production-ready method caching. Choose Caffeine or Redis, set size and expiry, evict on writes, and monitor hit rates through Actuator.