Quick revision
The short answer
We add spring-boot-starter-cache and spring-boot-starter-data-redis, put @EnableCaching on a configuration class, set spring.cache.type=redis and spring.cache.redis.time-to-live=10m, and annotate the lookup with @Cacheable("products"). That gives cache-aside behaviour: on each call Spring checks Redis first, returns the cached value on a hit, and on a miss runs the method and stores its result for ten minutes. Changes go through @CacheEvict or @CachePut, and because caching works through a proxy, a call from inside the same class skips the cache.
What to remember
In this answer, we configured Spring Cache with Redis using the cache and Redis starters, @EnableCaching, spring.cache.type=redis and a 10 minute TTL, with a customizer for per-cache TTLs and JSON values. We followed the cache-aside flow through the proxy: check Redis, return on a hit, run the method and store the result on a miss. We kept data fresh with @CacheEvict, preferred over @CachePut because of ordering races, and saw that self-invocation bypasses the proxy and therefore the cache.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We add spring-boot-starter-cache and spring-boot-starter-data-redis, put @EnableCaching on a configuration class, set spring.cache.type=redis and spring.cache.redis.time-to-live=10m, and annotate the lookup with @Cacheable("products"). That gives cache-aside behaviour: on each call Spring checks Redis first, returns the cached value on a hit, and on a miss runs the method and stores its result for ten minutes. Changes go through @CacheEvict or @CachePut, and because caching works through a proxy, a call from inside the same class skips the cache.
Why this question is asked
Caching a product lookup is often the first performance fix a service gets, and it is easy to do almost right. The common mistakes are stale data after an update, a cache that never works because of self-invocation, and values that cannot be read because of the serializer. Interviewers ask this to check that we can configure it and that we understand the cache-aside pattern well enough to reason about staleness. In this answer, we will recap what cache-aside means, add the dependencies and configuration, annotate the methods, follow a hit and a miss step by step, handle updates, and look at the proxy rule.
1A Quick Recap of Cache-Aside
Cache-aside, also called lazy loading, is a pattern where the application, not the database, manages the cache. To read, it first looks in the cache. If the value is there, a hit, it uses it. If not, a miss, it reads from the database, puts the result in the cache, and returns it. The cache only ever holds data someone asked for, and each entry has a time to live, a TTL, after which Redis deletes it so the next read fetches fresh data.
Spring's cache abstraction implements this pattern for us through annotations. We describe which methods are cacheable, and Spring wraps them in a proxy, a generated object that sits in front of our bean and runs the cache logic before and after the real method.
2Dependencies and Configuration
We need the cache abstraction, the Redis client, and caching switched on.
spring.cache.type=redis makes the choice explicit; Spring Boot would also pick Redis when it is the available provider. The time-to-live property applies to every cache. The customizer bean shows how to give each cache its own TTL and how to store values as JSON; by default the Redis cache manager stores values with JDK serialization, so cached classes must implement Serializable and appear as binary in redis-cli. Check your Spring Data Redis version for the JSON serializer class, since Jackson 3 support renamed some of them.
3Annotating the Lookup and Following a Call
Now we mark the method. The cache name is products, and by default the key is built from the method arguments, here the product id.
9 messages in order. Play them, or step through them one at a time.
On a hit, the proxy returns after the GET and the service and database are never called.
Notice that the second call never reaches our method body; the proxy returns the cached value directly. By default Spring also caches null results, so a lookup for a missing product does not hammer the database; that can be switched off with disableCachingNullValues(), or skipped per method with unless = "#result == null". A method that throws, as orElseThrow does here, stores nothing.
4Keeping the Cache Correct When Products Change
A cached product stays in Redis for up to ten minutes. If the price changes and we do nothing, readers see the old price until the entry expires. So the methods that change products tell the cache.
Eviction is usually the safer choice. With @CachePut, two concurrent updates can finish in a different order in Redis than in the database and leave the older value cached. Eviction only removes the entry, and the next read loads the current row. Notice also that the TTL remains our safety net: even if an update path forgets to evict, the stale value lives at most ten minutes.
5The Proxy Rule: Self-Invocation Skips the Cache
The annotations only work when a call goes through the proxy. A call from one method of ProductService to another method of the same class uses this, the real object, so the proxy and the cache are skipped.
The fix is to call the cached method from another bean, or move it into a small dedicated class. The same rule applies to @Transactional and @Async, and so do the other proxy rules: the method must be public in the usual configuration, and calls during construction are not intercepted.
6Follow-up questions
What is the sync attribute on @Cacheable for?
With sync = true, concurrent misses for the same key in one application instance wait for the first caller to load the value instead of all hitting the database. It does not coordinate across instances, so ten pods can still load the same key once each, which is usually acceptable.
Is cache-aside consistent with the database?
Not strictly. Between the database write and the eviction there is a brief window where readers see the old value, and a reader that loaded just before the update can put the old value back after the eviction. TTLs bound how long such a stale entry lives, which is why they should match how stale the business can tolerate.
How do you test that caching works?
In an integration test with Redis in Testcontainers, call the method twice and verify with a spy or a counter that the repository was called once, then call the evicting method and verify the next read hits the repository again.
7Conclusion
In this answer, we configured Spring Cache with Redis using the cache and Redis starters, @EnableCaching, spring.cache.type=redis and a 10 minute TTL, with a customizer for per-cache TTLs and JSON values. We followed the cache-aside flow through the proxy: check Redis, return on a hit, run the method and store the result on a miss. We kept data fresh with @CacheEvict, preferred over @CachePut because of ordering races, and saw that self-invocation bypasses the proxy and therefore the cache.
Open with
I would add spring-boot-starter-cache and spring-boot-starter-data-redis, put @EnableCaching on a configuration class, and set spring.cache.type to redis and spring.cache.redis.time-to-live to 10m. If different caches need different TTLs, or I want JSON values instead of the default JDK serialization, I add a RedisCacheManagerBuilderCustomizer with a RedisCacheConfiguration per cache name.
Then, if they want more
Then I put @Cacheable with the name products on the lookup method. The behaviour is cache-aside: the call goes through a Spring proxy, which builds the key from the arguments, like products::42, and does a GET in Redis. On a hit it returns the cached product without running my method.
On a miss it runs the method, which reads the database, stores the result with the ten minute TTL, and returns it. Nulls are cached by default, which protects the database from repeated lookups of missing ids, but exceptions are not.
For changes, I put @CacheEvict on the update method so the next read loads fresh data; I prefer evict over @CachePut because concurrent puts can leave an older value in Redis. And the TTL is the safety net if an update path forgets.
The classic trap is self-invocation: if another method in the same class calls find through this, the proxy is skipped and nothing is cached, so the cached method has to be called from another bean.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Configure Spring Cache with Redis so that product lookups are cached for 10 minutes, and...": https://scrivio-interview-library.netlify.app/databases/spring-data-nosql/266fc823a4/