Caching strategies in Spring Boot come down to three decisions: which pattern keeps the cache consistent with your data (cache-aside, write-through or write-around), how entries expire or are evicted, and which provider stores them. Spring's cache abstraction implements cache-aside with @Cacheable, and refresh or invalidation with @CachePut and @CacheEvict.
The right strategy depends on how often data changes and how much staleness you can accept. A product catalogue that changes hourly tolerates a 10-minute TTL; an account balance does not.
@Cacheable@CachePut and @CacheEvict@CacheableIn cache-aside, the application checks the cache first, loads from the database on a miss, and stores the result. This is what @Cacheable does.
It is the default choice for read-heavy data. Its weakness is staleness: if another process changes the row, the cached copy remains until it expires or is evicted.
@CachePut and @CacheEvictFor write paths you have two options.
@CachePut suits entities you return right after saving. @CacheEvict is safer when the cached shape differs from the saved entity, because the next read rebuilds the value from the source of truth. Use allEntries = true to clear a whole cache after a bulk import.
| Strategy | Spring mechanism | Best for | Risk |
|---|---|---|---|
| Cache-aside | @Cacheable | Read-heavy data | Stale reads until expiry |
| Write-through | @CachePut on writes | Entities read soon after write | Extra write cost |
| Invalidate on write | @CacheEvict | Derived or aggregated values | One miss after each change |
| Time-based expiry | Provider TTL | Data with tolerable staleness | Periodic reload spikes |
The Spring abstraction does not define expiry; the provider does. With Caffeine:
expireAfterWrite bounds staleness, expireAfterAccess drops rarely used entries, and maximumSize evicts the least valuable entries when full. With Redis you set spring.cache.redis.time-to-live and rely on Redis maxmemory-policy for eviction. Always set both a size or memory bound and a TTL, otherwise a cache can grow until the JVM or Redis runs out of memory.
When a popular key expires, many threads may miss at the same time and all hit the database. Setting sync = true makes Spring let only one thread compute the value per key while others wait.
sync = true works with a single cache and cannot be combined with unless. For distributed setups it only locks within one JVM, so a stampede across instances still needs short TTL jitter or request coalescing.
| Provider | Scope | Typical use |
|---|---|---|
| Caffeine | Per JVM | Hot, small data on a single instance or as an L1 cache |
| Redis | Shared across instances | Sessions, shared lookups, consistent invalidation |
Simple (ConcurrentMapCacheManager) | Per JVM, no expiry | Tests and prototypes only |
A common production layout uses Caffeine as a short-lived local cache in front of Redis, with a short local TTL so instances converge quickly. Spring can compose managers with CompositeCacheManager, but you must decide which cache each name belongs to.
Expose metrics with Spring Boot Actuator and Micrometer and watch the hit ratio, for example the cache.gets meter tagged with result=hit and result=miss. A hit ratio far below what you expect usually means keys include a volatile argument, TTLs are too short, or the cache is too small.
@CachePut and @Cacheable?@Cacheable skips the method on a hit, while @CachePut always runs it and overwrites the entry with the result.
Prefer immutable DTOs or records. Caching mutable JPA entities risks stale lazy proxies and callers modifying shared objects.
Match it to how much staleness the business accepts, not to a default. Minutes suit catalogue data; seconds or no caching suit financial values.
null results?You can, but usually you should not. Use unless = "#result == null" so a missing row is not cached for the full TTL.
Use @Cacheable for cache-aside reads, @CachePut or @CacheEvict on writes, and always bound caches by size and time. Add sync = true for expensive hot keys, pick Caffeine for local and Redis for shared caching, and measure the hit ratio before tuning.