Spring Cache Abstraction with Redis (@Cacheable, @CacheEvict)
Fast recall
Quick revision
Core answer
Practical knowledge of wiring Spring caching to Redis, including configuration, key generation, and cache eviction strategies.
Forgetting to add @EnableCaching on the configuration class, or not configuring a custom KeyGenerator, leading to key collisions between methods.
Explain the proxy-based limitation (self-invocation doesn't hit the cache), custom key expressions with SpEL, and how to configure per-cache TTLs.
Interview-ready guide
Interview answer
Spring's cache abstraction lets you add Redis caching to any Spring bean method with three annotations — @Cacheable, @CachePut, and @CacheEvict — without coupling the business logic to the caching implementation.
`@Cacheable` — cache on first call
- Place
@Cacheable(value = "products", key = "#id")on a method — on the first call, the method executes and the return value is stored in Redis under the keyproducts::id; subsequent calls with the same argument return the cached value without executing the method - The cache key defaults to the method parameters; use SpEL expressions to compose custom keys:
key = "#user.id + ':' + #status"
`@CacheEvict` — invalidate on mutation
- Place
@CacheEvict(value = "products", key = "#id")on a service method that updates or deletes a product — when that method executes, Spring removes the corresponding Redis key so the next read fetches fresh data allEntries = trueevicts every key in the named cache — useful after a bulk import that invalidates everything
`@CachePut` — update the cache on write
@CachePutalways executes the method AND updates the cache — unlike@Cacheablewhich skips the method on a hit; use it on create or update methods to keep the cache warm
Configuration in Spring Boot
- Add
spring-boot-starter-data-redisand configurespring.redis.host; add@EnableCachingto the application; Spring Boot auto-configures aRedisCacheManagerusing Jackson for serialization - Configure TTL per cache:
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(10))— without TTL, cached values live until evicted, which can cause stale data to persist indefinitely
Spring Cache Abstraction with Redis (@Cacheable, @CacheEvict)
Direct Answer
Spring's cache abstraction lets you declaratively add Redis caching to any Spring bean method using @Cacheable to cache results, @CachePut to update the cache, and @CacheEvict to invalidate entries — all without mixing caching logic into business code. You configure a RedisCacheManager bean to connect Spring's abstraction to Redis, set TTLs per cache, and choose a serializer. The result is clean service code where caching is a cross-cutting concern managed by AOP proxies.
Common Mistakes & Pitfalls
Forgetting to add @EnableCaching on the configuration class, or not configuring a custom KeyGenerator, leading to key collisions between methods.
Check your understanding
Answer these without looking back at the article.
- Why does self-invocation bypass Spring's cache proxy, and how do you work around it?
- How would you implement cache warming on application startup?
- What serializer would you recommend for Redis caching and why?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.