Quick revision
The short answer
Depending on the order of the two interceptors, the cache eviction can run when the method returns but before the transaction commits. In that gap a concurrent reader misses the cache, reads the old committed row from the database and puts it back, and when the commit lands the cache holds the old order until the TTL expires. The fix is to make cache operations transaction-aware, with TransactionAwareCacheManagerProxy or transactionAware() on the RedisCacheManager builder, so evictions run after commit; an after-commit event listener and short TTLs are alternatives and safety nets.
What to remember
In this answer, we saw how @Transactional and @CacheEvict on one method can evict the entry before the transaction commits. A concurrent reader then misses, reads the old committed row and stores it again, and that stale entry survives until the TTL. Making the cache transaction-aware, with transactionAware() on the RedisCacheManager builder or a TransactionAwareCacheManagerProxy, defers the eviction until after commit, and an AFTER_COMMIT event listener achieves the same thing explicitly. Even then a small race remains, so short TTLs, a delayed second eviction or version checks are the safety net for data with this risk.
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.
Depending on the order of the two interceptors, the cache eviction can run when the method returns but before the transaction commits. In that gap a concurrent reader misses the cache, reads the old committed row from the database and puts it back, and when the commit lands the cache holds the old order until the TTL expires. The fix is to make cache operations transaction-aware, with TransactionAwareCacheManagerProxy or transactionAware() on the RedisCacheManager builder, so evictions run after commit; an after-commit event listener and short TTLs are alternatives and safety nets.
Why this question is asked
This bug is a classic because each piece is correct on its own. The update is transactional, the eviction is there, and in a single-user test everything works. Only under concurrent load does a stale order appear, and then it stays for the whole TTL, which makes it look like eviction is broken. Interviewers ask it to see whether we can reason about two concurrent timelines and about when a commit really happens. In this answer, we will recap how the two annotations wrap the method, walk through the race step by step, fix it with transaction-aware caching, look at an event-based alternative, and see why even after-commit eviction leaves a smaller gap.
1A Quick Recap: Two Interceptors Around One Method
Both @Transactional and @CacheEvict are applied by proxies: a wrapper object Spring generates around our bean, which runs extra logic before and after our method. When a method has both annotations, the same proxy runs two interceptors, the transaction interceptor and the cache interceptor, one inside the other.
The order matters. If the transaction interceptor is outside, the sequence is: begin transaction, then the cache interceptor runs our method and evicts the entry, then the transaction commits. If the cache interceptor is outside, the transaction commits first and the eviction happens afterwards. Both advisors have the lowest precedence by default, so unless we set an order explicitly, we should not assume which one wins. And even when committing first, the database commit is the moment other transactions can see the new row, so anything that happens before it sees the old one.
2Walking Through the Race
Take the ordering where eviction runs inside the transaction. Writer and reader run at the same time on different threads, perhaps on different instances.
9 messages in order. Play them, or step through them one at a time.
The eviction worked; the reader simply refilled the cache from data that was about to change.
Notice the key step: the reader's SELECT runs after the eviction but before the commit. Under the default READ COMMITTED isolation, it cannot see the uncommitted update, so it reads PAID and stores it. When the writer finally commits, nobody evicts again. Every later reader hits the cache and sees PAID until the TTL expires or the order is updated again. With a one hour TTL, that is an hour of stale data from a race that lasted a few milliseconds. The busier the order, the more likely a reader falls into that gap.
3Fix 1: Make the Cache Transaction-Aware
Spring has a decorator for exactly this case. TransactionAwareCacheDecorator wraps a Cache, and when a Spring-managed transaction is active it delays put and evict until the transaction has committed, using a transaction synchronization callback. If the transaction rolls back, the deferred operations are dropped. TransactionAwareCacheManagerProxy applies the decorator to every cache a manager returns, and RedisCacheManager can do it through its builder.
Now the order of the interceptors no longer matters. Even if eviction is requested inside the transaction, it is executed only after the new row is visible, so a reader that misses afterwards loads SHIPPED. Note one detail: eviction requested with beforeInvocation = true uses an immediate evict, so leave that attribute at its default when we want after-commit behaviour. Check your version for which Cache methods the decorator defers.
4Fix 2: Evict From an After-Commit Event Listener
If we prefer to keep cache handling out of the service method, we publish an event inside the transaction and evict from a listener that only runs once the commit has succeeded.
This has the same effect as the transaction-aware cache, and it makes the invalidation explicit and easy to extend, for example to also publish the change to other instances. Whichever fix we choose, keeping the TTL short for data that has this risk limits the damage of any race we did not think of.
5Follow-up questions
Does after-commit eviction fully remove the race?
No, it makes the window much smaller but does not close it. A reader can miss the cache and read the old row just before the commit, then be delayed, by a garbage collection pause for example, while the writer commits and evicts. The reader then puts the old value after the eviction, and it stays until the TTL. Common mitigations are a short TTL as an upper bound, a second delayed eviction a short time after the first, or storing a version with each value and refusing to put an older version over a newer one. For data that must never be stale, read from the database.
Why not use @CachePut with the new order instead of evicting?
Putting the new value has its own race: two concurrent updates can complete in one order and write the cache in the other, leaving the older value cached. And inside a transaction, a put before commit could publish a value that is later rolled back. Eviction is the safer default because the worst case is an extra database read, not a wrong value. If we put, it should be after commit, ideally with a version check.
Can we fix it by ordering the advisors so the cache interceptor is outside?
It helps, because the commit then happens before the eviction, and we can set it with the order attribute of @EnableCaching and @EnableTransactionManagement. But it relies on configuration that is easy to change by accident, and it does not help when the transaction was started by an outer caller, because then our method returns long before the real commit. The transaction-aware cache is tied to the actual commit, so it is the more robust fix.
6Conclusion
In this answer, we saw how @Transactional and @CacheEvict on one method can evict the entry before the transaction commits. A concurrent reader then misses, reads the old committed row and stores it again, and that stale entry survives until the TTL. Making the cache transaction-aware, with transactionAware() on the RedisCacheManager builder or a TransactionAwareCacheManagerProxy, defers the eviction until after commit, and an AFTER_COMMIT event listener achieves the same thing explicitly. Even then a small race remains, so short TTLs, a delayed second eviction or version checks are the safety net for data with this risk.
Open with
The race is between the eviction and the commit. Both annotations are applied by interceptors on the same proxy, and depending on their order, the cache eviction can run when the method returns but before the transaction commits.
Then, if they want more
In that gap a reader misses the cache, queries the database, and under read committed it still sees the old committed row, so it puts the old order back into the cache. Then the writer commits, nobody evicts again, and every reader gets the old order until the TTL expires.
Even with the opposite ordering, if an outer caller owns the transaction, the commit happens later than our method's return. The fix is to tie cache operations to the commit. Spring has TransactionAwareCacheDecorator, applied through TransactionAwareCacheManagerProxy or the transactionAware() option on the RedisCacheManager builder, which defers puts and evicts until after a successful commit and drops them on rollback.
An alternative is to publish an event and evict in a @TransactionalEventListener with phase AFTER_COMMIT. I would also say honestly that after-commit eviction does not close the race completely: a reader that read just before the commit can still put the old value after the eviction.
So for this data I would keep the TTL short, and for critical cases add a delayed second eviction or version checks.
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 Framework reference, Cache abstraction
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 "An order service method annotated @Transactional and @CacheEvict(cacheNames = "orders"...": https://scrivio-interview-library.netlify.app/databases/caching/ac5bc420db/