Quick revision
The short answer
A transaction-aware cache delays put, evict and clear calls made inside an active Spring transaction until the transaction has committed, and drops them if it rolls back, so the cache never holds values that were never committed. Outside a transaction, the calls happen immediately. It does not isolate reads, it does not delay evictIfPresent or invalidate, and it cannot stop other transactions from reading old data in the gap before the delayed eviction, or update caches in other instances.
What to remember
In this answer, we saw that a transaction-aware cache wraps a Cache and moves put, evict and clear into an afterCommit callback when a Spring transaction is active, dropping them on rollback and running them immediately outside a transaction. Reads, putIfAbsent, evictIfPresent and invalidate are not delayed. It does not isolate reads, cannot close the race between commit and the delayed eviction, and does nothing for caches in other instances, so TTLs and broadcasts are still needed.
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.
A transaction-aware cache delays put, evict and clear calls made inside an active Spring transaction until the transaction has committed, and drops them if it rolls back, so the cache never holds values that were never committed. Outside a transaction, the calls happen immediately. It does not isolate reads, it does not delay evictIfPresent or invalidate, and it cannot stop other transactions from reading old data in the gap before the delayed eviction, or update caches in other instances.
Why this question is asked
When we cache data that is written inside transactions, there is a simple question with an uncomfortable answer: what if the cache is updated and then the transaction rolls back? Readers would see data that never existed in the database, sometimes for the whole TTL. Spring offers a small, focused tool for this, and interviewers ask about it to see whether we know exactly what it does and, just as important, what it leaves open. In this answer, we will recap how Spring transactions and caches meet, see how TransactionAwareCacheDecorator works step by step, list which operations it delays and which it does not, and look at the gaps it leaves.
1A Quick Recap of the Problem
Spring's cache abstraction is a set of interfaces, CacheManager and Cache, plus annotations such as @Cacheable, @CachePut and @CacheEvict. A Cache is just a key-value store; it knows nothing about database transactions. When a @Transactional method writes to the database and also puts a value into the cache, the database change is only final at commit, but the cache change is final the moment it happens.
So if the commit fails or the method throws after the put, the database is rolled back while the cache keeps the new value. Every reader now gets data that is not in the database. A transaction-aware cache fixes exactly this ordering problem.
2How TransactionAwareCacheDecorator Works
TransactionAwareCacheDecorator is a wrapper around any Cache. A decorator is an object with the same interface as the one it wraps, which adds behaviour and passes the rest through. For each put, evict or clear, it first asks Spring whether transaction synchronization is active on this thread, which is true inside a Spring-managed transaction.
If it is, the decorator does not touch the cache yet. Instead it registers a TransactionSynchronization, a callback object that Spring calls at points in the transaction's life, and performs the real operation in its afterCommit method. If the transaction rolls back, afterCommit is never called, so the delayed operation simply disappears. If no transaction is active, the decorator calls the wrapped cache straight away.
5 messages in order. Play them, or step through them one at a time.
On rollback, afterCommit is never called and the real cache is never touched.
Notice the first get inside the transaction. The decorator does not keep a private view of pending changes, so the same transaction does not see its own put. That surprises people the first time.
3Which Operations Are Delayed and Which Are Not
Only put, evict and clear are delayed. Everything else goes straight to the wrapped cache.
| Operation | Inside a transaction | Why |
|---|---|---|
| put | after commit | do not publish uncommitted values |
| evict, clear | after commit | readers keep the old value until commit |
| get | immediate, shared cache | reads are not isolated |
putIfAbsent | immediate | it must return the existing value now |
evictIfPresent, invalidate | immediate | their contract is to act right away |
The reason for the immediate ones is their contract. putIfAbsent must tell the caller what was already there, which it cannot do later. evictIfPresent and invalidate were added to the Cache interface as operations that take effect immediately and report the outcome, so the decorator passes them through. If we call them inside a transaction, we get no protection.
4Enabling It for a Whole CacheManager
Wrapping each cache by hand would be tedious, so Spring offers it at the CacheManager level. Cache managers that extend AbstractTransactionSupportingCacheManager, such as JCacheCacheManager and RedisCacheManager, have a transaction-aware switch. For any other manager, such as CaffeineCacheManager, TransactionAwareCacheManagerProxy wraps the manager and decorates every cache it returns.
5What It Does Not Protect Against
The decorator fixes one thing: uncommitted values reaching the cache. It leaves three gaps. First, reads are not isolated; other threads see the shared cache, and our own transaction does not see its pending changes.
Second, there is still a race around the commit. Suppose transaction A updates an account and its eviction waits for afterCommit. Between the commit and the callback, or between our eviction and a slow reader, another request can read the old value from the cache, or load an old row it had read earlier and put it back. The window is small but real under load, which is why short TTLs remain a useful safety net.
Third, it only acts on the cache objects in this JVM. A local Caffeine cache in another instance, or a cache owned by another service, is never told. For those we need an invalidation broadcast or change data capture. And the afterCommit callback itself can fail, for example when Redis is unreachable, and by then the transaction is already committed, so the failure must be logged and repaired by TTL.
6Follow-up questions
How do you enable it for a whole CacheManager?
For Redis, call transactionAware() on the RedisCacheManager builder; for a JCacheCacheManager or other AbstractTransactionSupportingCacheManager subclass, call setTransactionAware(true). For any other manager, wrap it in a TransactionAwareCacheManagerProxy, which decorates every cache the manager returns.
Does it make the cache part of the transaction like a database resource?
No. There is no two-phase commit and no rollback of the cache. It only reorders our calls so they happen after a successful commit, which is a much weaker but much cheaper guarantee.
Does @Cacheable on a read benefit from it?
The put that @Cacheable makes after a miss is delayed if the read runs inside a transaction, so a value read in a transaction that later rolls back is not cached. The lookup itself is immediate and unaffected.
7Conclusion
In this answer, we saw that a transaction-aware cache wraps a Cache and moves put, evict and clear into an afterCommit callback when a Spring transaction is active, dropping them on rollback and running them immediately outside a transaction. Reads, putIfAbsent, evictIfPresent and invalidate are not delayed. It does not isolate reads, cannot close the race between commit and the delayed eviction, and does nothing for caches in other instances, so TTLs and broadcasts are still needed.
Open with
Making a cache transaction-aware means wrapping it in TransactionAwareCacheDecorator. When a Spring transaction is active, put, evict and clear are not applied straight away. The decorator registers a TransactionSynchronization and performs them in afterCommit, so if the transaction rolls back they are simply dropped and the cache never holds uncommitted data.
Then, if they want more
If there is no transaction, they run immediately. I can turn it on for a whole manager with RedisCacheManager.builder().transactionAware(), setTransactionAware(true) on a JCacheCacheManager, or by wrapping any manager in TransactionAwareCacheManagerProxy. What it does not do is just as important.
Reads are not isolated: get goes to the shared cache, and my own transaction does not see its pending put. putIfAbsent, evictIfPresent and invalidate act immediately because their contracts require it. It does not remove the race where another request reads or reloads the old value around the commit, and it does nothing for caches in other instances or services.
So I still keep TTLs as a safety net and use a broadcast or change data capture when other caches hold copies.
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 "What does making a Spring cache transaction-aware do? Explain...": https://scrivio-interview-library.netlify.app/databases/caching/db682d3026/