Quick revision
The short answer
After the price change commits, the instance that handled it publishes a price-changed event, through an outbox table or an after-commit listener, to Kafka, Redis pub/sub or a similar channel. Every instance subscribes and evicts (or refreshes) that key in its local Caffeine cache. Because delivery can be lost or delayed, each entry also keeps a TTL as an upper bound. The honest promise is eventual consistency with a stated bound: usually under a second, never more than the TTL. For prices that must never be stale, such as the one charged at checkout, we read from the database or a shared cache instead.
What to remember
In this answer, we designed invalidation for 20 local Caffeine caches. The instance that changes a price publishes an event after commit, through an outbox or an after-commit listener, to a channel every instance subscribes to, and each instance evicts the key. Because pub/sub can lose messages and consumers can lag, a TTL on every entry bounds staleness. The promise is eventual consistency with numbers attached, usually under a second and never more than the TTL, and for prices that must never be stale we read from the database or a shared 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.
After the price change commits, the instance that handled it publishes a price-changed event, through an outbox table or an after-commit listener, to Kafka, Redis pub/sub or a similar channel. Every instance subscribes and evicts (or refreshes) that key in its local Caffeine cache. Because delivery can be lost or delayed, each entry also keeps a TTL as an upper bound. The honest promise is eventual consistency with a stated bound: usually under a second, never more than the TTL. For prices that must never be stale, such as the one charged at checkout, we read from the database or a shared cache instead.
Why this question is asked
Local caches are wonderful for speed: a price lookup in Caffeine takes well under a microsecond. But with 20 instances, there are 20 copies of every price, and an update through the admin API reaches only one of them. Without a design for spreading that change, 19 instances keep showing the old price until their entries expire. Interviewers ask this to see whether we can design the invalidation path end to end and, just as important, state the consistency we can really promise. In this answer, we will recap the problem, publish the change reliably, apply it on every instance, add the TTL safety net, state the promise in numbers, and decide where local caches should not be used.
1A Quick Recap of the Problem
Each instance's Caffeine cache is private to its JVM. When the admin API on instance 7 updates the price of product 42 and evicts its own entry, nothing tells instances 1 to 6 and 8 to 20. Each of them still holds the old price and will serve it until the entry's TTL runs out. With a 10 minute TTL and a price change, customers hitting different instances can see two different prices for up to 10 minutes. We need a way to broadcast "price 42 changed" to every instance, and we need it to be sent only when the change really happened.
2Publishing the Change Only After Commit
The event must not be sent before the database commit. If it were, another instance could evict, immediately reload the old price from the database, and cache it again. There are two reliable ways to publish after commit. The simplest is a @TransactionalEventListener with phase AFTER_COMMIT that sends the message. Its weakness is that if the process dies between commit and send, the event is lost. The more robust way is the transactional outbox: the update writes the new price and a row in an outbox table in the same transaction, and a relay process publishes outbox rows to the channel. Nothing committed is ever lost, at the cost of a little extra machinery.
3Applying the Change on Every Instance
Every instance subscribes to the channel. With Kafka, each instance must receive every message, so each uses its own consumer group, for example one named after the instance id, rather than sharing one group, which would deliver each message to only one instance. With Redis pub/sub, every subscriber receives each message by design. The handler evicts the key, or refreshes it if we want the next read to be a hit.
5 messages in order. Play them, or step through them one at a time.
The TTL on every entry still bounds staleness if a message never arrives.
Notice that we evict rather than put the price carried in the message. Eviction is idempotent and safe if messages arrive out of order: two evictions in any order still lead to a fresh read. Putting values from messages would require ordering or version checks.
4The TTL Safety Net
Events can be lost or late. Redis pub/sub is fire-and-forget: an instance that is disconnected, restarting or slow at the moment of publishing simply never receives the message. Kafka keeps messages, but a new instance usually starts from the latest offset, and a consumer can lag. So events alone cannot be the only guarantee. Every entry keeps an expireAfterWrite TTL, for example five minutes, which bounds the staleness whatever happens to the events. The events give us freshness in the normal case, and the TTL gives us a worst case.
5The Consistency We Can Promise
This design gives eventual consistency, and we should state it with numbers rather than words. In normal operation the window is the commit-to-publish delay plus delivery plus the next read, typically 100 to 500 milliseconds. In the worst case, a lost event, it is the TTL. During that window, two instances may answer the same question differently.
| Situation | Staleness on other instances |
|---|---|
| Normal: event delivered | usually under 1 second |
| Consumer lagging | until the consumer catches up |
| Event lost (pub/sub, instance restarting) | at most the TTL, for example 5 minutes |
| Checkout price | none: read from the database |
6Where a Local Cache Is the Wrong Tool
Some values must never be stale. The price we charge at checkout, a discount that has been withdrawn, a price that is legally binding: for these we read from the database, or from a shared cache like Redis where one eviction is seen by all instances, and accept the extra millisecond. A good split is to use the local cache for display prices on listing pages, where a sub-second delay is harmless, and read through to the source for the price used in the order.
7Follow-up questions
How would you handle an instance that was restarting when the event was sent?
A restarting instance starts with an empty local cache, so it cannot hold a stale price from before the restart; it loads fresh values on first read. The risk is an instance that was alive but disconnected from the channel when the event was sent. With Kafka, it resumes from its committed offset and replays what it missed, as long as the instance-specific group keeps its offsets. With Redis pub/sub, the message is gone, so the TTL is the protection; on reconnect, we can also clear the whole local cache, which is cheap and removes any doubt.
Why not use Redis as the only cache instead?
We could, and for prices it is a reasonable choice: one copy, one eviction, no broadcast. The cost is a network round trip per read, about a millisecond, and dependence on Redis being up. Local caching with events is worth it when read rates are very high or latency budgets are very tight; otherwise a shared cache is simpler.
Can we broadcast the new price instead of an eviction?
Yes, if messages carry a version or timestamp and the handler only applies a value newer than what it holds. Without that, an older message arriving late could overwrite a newer price. Eviction avoids the ordering problem at the cost of one extra database read per instance per change.
8Conclusion
In this answer, we designed invalidation for 20 local Caffeine caches. The instance that changes a price publishes an event after commit, through an outbox or an after-commit listener, to a channel every instance subscribes to, and each instance evicts the key. Because pub/sub can lose messages and consumers can lag, a TTL on every entry bounds staleness. The promise is eventual consistency with numbers attached, usually under a second and never more than the TTL, and for prices that must never be stale we read from the database or a shared cache.
Open with
When the admin API changes a price on one instance, I would publish a price-changed event only after the database commit, ideally through a transactional outbox so a crash between commit and publish cannot lose it, or more simply from an after-commit transactional event listener.
Then, if they want more
The channel could be Kafka or Redis pub/sub. Every instance subscribes; with Kafka that means a consumer group per instance so each one receives every message. The handler evicts the product's key from the local Caffeine cache, so the next read loads the new price.
I prefer evicting over putting the new price from the message, because eviction is idempotent and does not care about message order. Then I would be honest about delivery: pub/sub is fire-and-forget, a disconnected instance misses messages, and Kafka consumers can lag, so every entry also keeps an expireAfterWrite TTL, say five minutes, as the upper bound.
The promise I would state is eventual consistency with numbers: normally under a second, at most the TTL if an event is lost. That is fine for display prices on listing pages.
For anything that must never be stale, like the price actually charged at checkout, I would read from the database or a shared cache rather than a local copy.
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 "A pricing service runs 20 instances, each with a local Caffeine cache of prices. Prices...": https://scrivio-interview-library.netlify.app/databases/caching/5b5cdd321e/