Quick revision
The short answer
My strategy starts by deciding, for each type of data, how stale it may be, because caching without an explicit staleness budget is what causes stale-data incidents. The default is a standard bounded local cache library with metrics, and a distributed cache only where cross-instance consistency or data size really requires it, after counting its full cost: another system to run, network latency, new failure modes and invalidation logic. Before adding any cache, we prefer fixing the slow source, since every cache adds a consistency problem for as long as the system lives, and we review the cache inventory and hit rates regularly and remove caches that do not pay for themselves.
What to remember
In this answer, we set a long-term caching strategy that starts with an explicit staleness budget for each type of data, because caching without one causes stale-data incidents. The default is a standard bounded local cache library with metrics, and distributed caches are used only where cross-instance consistency or size requires them, after accounting for the extra system, network latency, failure modes and invalidation logic. We prefer fixing slow sources over adding caches, since every cache adds a lasting consistency problem, and we review the cache inventory and hit rates periodically, removing caches that do not pay for themselves.
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.
My strategy starts by deciding, for each type of data, how stale it may be, because caching without an explicit staleness budget is what causes stale-data incidents. The default is a standard bounded local cache library with metrics, and a distributed cache only where cross-instance consistency or data size really requires it, after counting its full cost: another system to run, network latency, new failure modes and invalidation logic. Before adding any cache, we prefer fixing the slow source, since every cache adds a consistency problem for as long as the system lives, and we review the cache inventory and hit rates regularly and remove caches that do not pay for themselves.
Background to this question
Caches are easy to add and hard to remove. Each one solved a real latency problem on the day it was written, but years later an organisation can have dozens of them, each with its own idea of freshness, and incidents where a customer sees an old price or a revoked permission still works. At this level the question is not how to configure a cache but how to decide where caching belongs at all and how to weigh its cost. In this answer, we will recap what a cache really trades, set the staleness budget as the first decision, define the default and the exceptions, put numbers on the cost of a distributed cache, explain why fixing sources often wins, and finish with how to keep the inventory healthy.
1A Quick Recap: What a Cache Trades
A cache keeps a copy of data closer to where it is used, so we pay less latency or load on the source. The copy is the whole trade: from the moment it is made, it can differ from the source. Every cache therefore answers, explicitly or by accident, the question of how old a value may be when we serve it, and what happens when the source changes.
Memory incidents come from caches without bounds. Stale-data incidents come from caches without a deliberate answer to the freshness question. Both are design gaps, not library bugs, which is why a strategy has to start from decisions rather than tools.
2First Decision: A Staleness Budget per Data Type
For each kind of data we cache, the owning team writes down how stale it may be and what must happen on change. A product description may be an hour old. A price may be a minute old in browse pages but must be exact at checkout. A permission revocation must take effect within seconds. An account balance should not be cached at all for decisions. These budgets come from the business, not from engineering convenience.
The budget then drives the design: the expiry time, whether an event-driven invalidation is needed, and whether a given path may use the cache at all. Most of the stale-data incidents in a history like ours turn out to be a cache whose expiry was picked arbitrarily, or a path such as checkout that read a cache meant for browsing.
| Data | Budget | Design that follows |
|---|---|---|
| Product descriptions | 1 hour | local cache, expireAfterWrite 1h |
| Prices on browse pages | 1 minute | local cache, 1m expiry, refresh |
| Price at checkout | 0 | read the source, no cache |
| Permission changes | under 10 seconds | short expiry plus invalidation events |
| Account balance for decisions | 0 | never cached |
3The Default and the Exceptions
The default for every service is a standard bounded local cache library, such as Caffeine through a shared starter, with size bounds, expiry taken from the staleness budget, and metrics on every cache. Local caches are fast, around a hundred nanoseconds per hit, add no new infrastructure and fail in simple ways: if the process restarts, the cache is just empty.
A distributed cache, such as Redis, is the exception. It is justified when instances must agree on a value, when the working set does not fit in each instance's heap, or when recomputing a value is so expensive that sharing it across instances clearly pays. Each such use is a written decision with the reason, because the cost lasts as long as the system.
4Counting the Full Cost of a Distributed Cache
A distributed cache looks cheap because the client call is one line. The real cost has four parts. It is another system to run: a cluster to size, patch, monitor, back up and scale, plus on-call knowledge. Every lookup is a network call, typically a fraction of a millisecond to a millisecond inside a data centre, thousands of times slower than a local hit, and it adds a tail-latency risk on every request. It has new failure modes: when the cache is slow or down, every service that relies on it must fall back without overwhelming the source, and a cold cache after an outage can cause a thundering herd. And invalidation becomes our code: someone must delete or update keys when the source changes, and every path that forgets is a stale-data incident.
So before approving one, I would ask for numbers. Suppose a cache serves 5,000 lookups per second with a 90 percent hit rate and saves 20 milliseconds per hit on a slow query. That saves the source 4,500 queries per second, which can justify a cluster. A cache with a 30 percent hit rate in front of a 3 millisecond query saves little and still carries every cost above.
- Can the source be made fast enough?index, query, read replicaYesFix the source, no cacheNoConsider a cache
- Staleness budget above zero?YesContinueNoDo not cache this path
- Need cross-instance consistency or more than heap?YesDistributed cache, written decisionNoStandard local cache
- Review hit rate and cost each quarterremove caches that do not pay
4 steps in order. Play them, or step through them one at a time.
5Prefer Fixing Slow Sources
Many caches exist because a query was slow, an index was missing, or a service did N+1 calls. A cache hides the problem for the cached path but leaves it in place for misses, cold starts and every other caller, and it adds a consistency problem that lasts as long as the cache does. Fixing the source, with an index, a better query, a read replica or a batch endpoint, removes the latency for everyone and adds no staleness. So the strategy asks teams to show that the source cannot reasonably be made fast enough before a cache is added, and platform and database teams help with that work.
6Keeping the Inventory Healthy
Finally, caches need life-cycle management like any other dependency. The shared starter registers every cache with its name, owner, budget and metrics, which gives us an inventory for free. Each quarter we review it: caches with low hit rates, say under 50 percent for a local cache or below the agreed threshold for a distributed one, caches whose source has since become fast, and caches whose owners have moved on. The usual outcome is that some caches are removed, which simplifies the system and lowers the number of places where data can be stale. I would also track stale-data and cache-related memory incidents as an organisational metric, so we can see whether the strategy is working.
7Follow-up questions
How do we handle data with a zero staleness budget that is still read heavily?
We make the source fast for that read pattern rather than caching: a read replica with low lag, a materialised view kept up to date by the database, or a purpose-built read model updated from change events. These are consistent by design or within a known, very small lag, which is different from a cache with arbitrary expiry.
Is a cache invalidation event stream a good default?
It is a good addition where the budget is short, but not a replacement for expiry. Events can be lost or delayed, so every cache still needs an expiry as a backstop. Treat events as a way to make data fresher sooner, and expiry as the guarantee.
How do you weigh the engineering cost of the strategy itself?
The starter and the inventory are a few weeks of platform work, and the review is an hour per quarter per area. Against that we set the incidents: two OOMs and recurring stale-data issues each cost hours of several people's time and customer trust. Showing that comparison with the last year's incident data is usually what gets the strategy funded.
8Conclusion
In this answer, we set a long-term caching strategy that starts with an explicit staleness budget for each type of data, because caching without one causes stale-data incidents. The default is a standard bounded local cache library with metrics, and distributed caches are used only where cross-instance consistency or size requires them, after accounting for the extra system, network latency, failure modes and invalidation logic. We prefer fixing slow sources over adding caches, since every cache adds a lasting consistency problem, and we review the cache inventory and hit rates periodically, removing caches that do not pay for themselves.
Open with
I would base the strategy on one idea: every cache is a copy of data that can be wrong, so it has to earn its place. The first decision is a staleness budget for each type of data, set with the business.
Then, if they want more
Product descriptions might be fine an hour old, browse prices a minute, but a checkout price or an account balance used for decisions should never come from a cache. Most stale-data incidents come from caches whose expiry was picked arbitrarily or that were read on a path that needed fresh data.
Second, the default is a standard bounded local cache, Caffeine through a shared starter, with size bounds, expiry from the budget and metrics.
Distributed caches are the exception, used only where instances must agree or the data does not fit in heap, and approved with numbers, because their full cost includes another cluster to run, a network hop on every lookup, failure modes like thundering herds after an outage, and invalidation logic that every write path must remember.
Third, before adding a cache, teams should show that the slow source cannot be fixed with an index, a better query, a replica or a batch API, because a cache hides the problem and adds a consistency issue that lasts as long as the system.
Finally, the starter gives us an inventory of every cache with its owner and hit rate, and each quarter we review it and remove caches that do not pay for themselves. I would track cache-related incidents to show whether it is working.
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
- java.util.HashMap, API documentation, The Collections Framework, overview
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 "Over several years your services accumulated in-process map-based caches, a few...": https://scrivio-interview-library.netlify.app/java/hashmap-internals/4b5cdf6adf/