- @EnableCaching To enable the caching api of spring boot.
- @Cacheable Read from cache or compute and store.
- @CachePut Always execute method, update cache.
- @CacheEvict Remove cache entry.
- @Caching Combine multiple cache operations together.
- ConcurrentMapCacheManager: Backed by ConcurrentHashMap.
- JVM memory only, good for local build.
- Caffeine / EhCache, still in process but muc smarter.
spring:
cache:
type: caffeine@Bean
public CaffeineCacheManager cacheManager() {
CaffeineCacheManager manager = new CaffeineCacheManager("users");
manager.setCaffeine(
Caffeine.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.maximumSize(10_000)
);
return manager;
}- Provides ttl, eviction policies.
- Redis is external, shared, and persistent.
- Microservices, multiple app instances.
Prevents cascading failures when a dependency keeps failing.
States:
- CLOSED → calls allowed
- OPEN → calls blocked, fallback triggered
- HALF_OPEN → limited test calls
Handles temporary failures (timeouts, network glitches).
Stops resource starvation by isolating calls.
Protects services from traffic spikes or abuse.