Quick revision
The short answer
I would start by mapping every cache: what it stores, who owns it, its TTL, how it is invalidated and what consistency it promises. Then I would move towards fewer, explicit cache layers at service boundaries instead of hidden ORM-level caches, define a staleness budget per data type with the business, require an invalidation path for every write source including batch jobs and direct SQL, and measure hit ratio and age of served data so caches that do not pay for themselves are removed.
What to remember
In this answer, we built a long-term caching strategy for a system with Hibernate, Redis and CDN caches whose staleness nobody could explain. We mapped every cache with its owner, TTL, invalidation and write sources, preferred fewer explicit layers at service boundaries over hidden ORM caches, set staleness budgets per data type with the business, required an invalidation path for every write source including batch jobs and direct SQL, and used hit ratio and served-data age to keep, alert on or remove each 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.
I would start by mapping every cache: what it stores, who owns it, its TTL, how it is invalidated and what consistency it promises. Then I would move towards fewer, explicit cache layers at service boundaries instead of hidden ORM-level caches, define a staleness budget per data type with the business, require an invalidation path for every write source including batch jobs and direct SQL, and measure hit ratio and age of served data so caches that do not pay for themselves are removed.
Why this question is asked
Layered caches are how many large systems stay fast, and also how they become impossible to reason about. A customer sees an old address; the Hibernate cache, a Redis key, a CDN edge and a browser cache could each be responsible, and each was added by a different team for a good reason years ago. Interviewers ask this question of very senior engineers because the fix is not a technology but a strategy and a governance model that lasts. In this answer, we will look at why stale data becomes untraceable, build the cache map, decide which layers should exist, introduce staleness budgets and invalidation rules, and finish with the metrics and the review process that keep it healthy.
1Why Nobody Can Explain the Stale Data
Each cache layer makes a local promise. The Hibernate second-level cache keeps entities consistent within one instance for writes through Hibernate. Redis holds whatever the service put there, until a TTL or an explicit eviction. The CDN holds responses for as long as their Cache-Control headers say, unless someone purges them. None of these layers knows about the others.
So the age of what a user sees is the sum of the layers in the path. If Redis may be 10 minutes stale and the CDN caches the API response built from it for 5 minutes, the user can see data up to 15 minutes old, plus whatever the second-level cache adds on a cache miss in Redis. When a batch job writes directly to the database, no layer is told at all. Without a map, every incident starts from zero.
- BrowserCache-Control from the API, often forgotten
- CDN edgeTTL from headers, purge API
- API gateway or service response cachesometimes added per endpoint
- Redis (Spring Cache)service-level, TTL and explicit eviction
- Hibernate second-level cacheper instance, entity state by id
- Databasethe source of truth
6 layers, from the top down. Step through them one at a time.
The staleness a user can see is the sum of the layers on the path.
2Map Every Cache
The first deliverable is an inventory, kept as a living document or, better, as configuration that tools can read. For every cache we record what it stores, the owning team, the key format, the TTL, how it is invalidated, which write sources exist for the underlying data, and what consistency it promises. We find caches by searching code for @Cacheable, @Cache, RedisTemplate and Cache-Control headers, and by reading CDN and gateway configuration.
Building this map usually surfaces the answer to old incidents on its own, for example a Redis cache whose owning team left, or a CDN rule caching an API path that was meant to be private.
| Cache | Stores, owner | TTL and invalidation | Write sources not covered |
|---|---|---|---|
| L2 cache, catalog service | Product entities, catalog team | local, 30 min, Hibernate only | nightly price import (direct SQL) |
Redis productView | Product page DTO, catalog team | 10 min, evict on update | price import, admin bulk edit |
| CDN /api/products/* | API JSON, platform team | 5 min from headers, no purge | every write |
| Redis session | Login sessions, identity team | 30 min sliding | none, single writer |
3Prefer Fewer, Explicit Layers at Service Boundaries
With the map in hand, we decide which layers should exist. My preference is fewer and more explicit. A cache at the service boundary, such as Spring Cache over Redis for a shaped response, has its key, TTL and eviction in visible code, owned by the team that owns the data. A CDN cache is explicit too, controlled by headers the service sets on purpose. The Hibernate second-level cache is the opposite: it is hidden behind entity annotations, local to each instance and blind to writes outside Hibernate.
So the strategy is to keep ORM-level caching only for read-mostly reference data, move other entity caching to a service-level cache, and make sure each data type is cached in at most one or two layers. Each removed layer removes one place where staleness can hide.
4Staleness Budgets and Invalidation Paths
The question how fresh must this be is a business question, so we ask it there. For each data type we agree a staleness budget: product descriptions may be 15 minutes old, prices 1 minute, stock levels shown on a product page 30 seconds, an account balance zero, meaning never cached. The sum of TTLs along the path must fit inside the budget; if it does not, a layer goes or its TTL shrinks.
Then every write source needs an invalidation path. Writes through the owning service evict their keys. Batch jobs and direct SQL updates are the usual gaps, so we require them either to go through the service's API or to publish an event, for example through change data capture on the table, that the cache owners consume to evict. A write source without an invalidation path is a design defect, reviewed like any other.
Notice the total line. Writing down the worst case path per data type is what turns we have caches into we know how stale this can be.
5Metrics, Reviews and Removing Caches
Every cache reports hit ratio, size, evictions and, ideally, the age of data it serves. Age is the metric most teams lack and the one that matters for stale data: when a response is built, we can record how long ago the cached value was loaded and publish a histogram. If the 99th percentile age exceeds the budget, an alert fires before customers notice.
Caches are reviewed regularly, for example quarterly, against their numbers. A cache with a 30 percent hit ratio, or one that saves a few milliseconds on an endpoint nobody calls, does not pay for its staleness risk and its operational cost, and it is removed. New caches go through a light review: an entry in the map, a staleness budget, an invalidation path for each write source and metrics. That review is the governance; it is small enough that teams follow it.
- Entry in the cache mapstores, owner, key, TTL
- Staleness budget agreed with the business?YesCheck the layers fit the budgetNoAgree one first
- Every write source has an invalidation path?YesAdd metrics: hit ratio and ageNoClose the gap before approval
- Hit ratio and savings worth the risk?YesKeep and review quarterlyNoRemove the cache
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
How do you find the source of a stale value today, before the strategy is in place?
Follow the read path from the outside in. Check response headers such as Age and Cache-Control for the CDN, then the Redis key and its TTL, then whether the entity is in a second-level cache region, and finally the database row and its last update. The first layer whose value differs from the layer below is where the staleness lives.
Is change data capture worth it just for cache invalidation?
Often yes, when several write sources exist, because it catches every committed change at the database, including direct SQL and batch jobs, and publishes it once. Tools like Debezium read the database log, so writers do not need to cooperate. It does add infrastructure, so for a single-writer service an event from the service is enough.
Who should own the cache map?
Each cache is owned by the team that owns the data, and its entry lives in that team's repository. A platform or architecture group owns the format, the aggregation into one view and the quarterly review.
7Conclusion
In this answer, we built a long-term caching strategy for a system with Hibernate, Redis and CDN caches whose staleness nobody could explain. We mapped every cache with its owner, TTL, invalidation and write sources, preferred fewer explicit layers at service boundaries over hidden ORM caches, set staleness budgets per data type with the business, required an invalidation path for every write source including batch jobs and direct SQL, and used hit ratio and served-data age to keep, alert on or remove each cache.
Open with
The root problem is that each cache layer makes a local promise and nobody owns the end-to-end picture, so the staleness a user sees is the sum of all the layers on the path, and some writes reach none of them.
Then, if they want more
First I would map every cache: what it stores, the owning team, key, TTL, how it is invalidated, which write sources exist for the data, and what consistency it promises. That map alone usually explains old incidents. Then the strategy: fewer and more explicit layers.
I prefer caching at service boundaries, for example Spring Cache on Redis, where the key, TTL and eviction are in visible code owned by the data team, and CDN caching controlled by headers on purpose. I would keep the Hibernate second-level cache only for read-mostly reference data, because it is hidden, local per instance and blind to writes outside Hibernate.
For each data type I would agree a staleness budget with the business, prices a minute, descriptions fifteen minutes, balances never cached, and check that the TTLs along the path fit. Every write source, including batch jobs and direct SQL, must have an invalidation path, through the service or change data capture events.
Finally, metrics: hit ratio and the age of served data, with alerts against the budget, and a quarterly review that removes caches that do not pay for themselves.
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 JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Your company relies on several cache layers (Hibernate second-level cache, Redis, CDN)...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/03078d0512/