Quick revision
The short answer
We classify the data by how often it changes and how much staleness the business can tolerate, then pick the cache and expiry per kind. Product details change a few times a day, so they get a long TTL (for example 30 minutes) in a local Caffeine or shared Redis cache with eviction on update; prices change every few minutes, so they get a short TTL or event-driven eviction in a shared Redis cache; stock changes constantly, so we usually do not cache it at all, or only for a few seconds on display pages. Each cache gets its own TTL, and we keep only the caches that measurably improve latency or protect the database.
What to remember
In this answer, we designed caching by starting from the data rather than from the tool. Product details tolerate long TTLs and fit a local Caffeine or shared Redis cache with eviction on update. Prices need a short TTL and cross-instance eviction, which a shared Redis cache provides. Stock is usually not cached, or only for seconds on display pages. We configured a separate TTL and size for each cache, and we decided to keep a cache only when its hit ratio, latency and database load show a real benefit.
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.
We classify the data by how often it changes and how much staleness the business can tolerate, then pick the cache and expiry per kind. Product details change a few times a day, so they get a long TTL (for example 30 minutes) in a local Caffeine or shared Redis cache with eviction on update; prices change every few minutes, so they get a short TTL or event-driven eviction in a shared Redis cache; stock changes constantly, so we usually do not cache it at all, or only for a few seconds on display pages. Each cache gets its own TTL, and we keep only the caches that measurably improve latency or protect the database.
Why this question is asked
Adding caching to a service is easy; adding the right caching is a design decision with real costs. A cache is a second copy of the data, and every copy can be wrong. With 12 instances behind a load balancer, a careless choice means a customer sees one price on the product page and another at checkout, depending on which instance answered. Interviewers ask this question to see whether we reason from the data and its tolerance for staleness instead of reaching for one global setting. In this answer, we will recap the two kinds of cache we can use, classify the three kinds of data, choose a provider and expiry for each, configure per-cache TTLs in Spring, and finish with how to prove the cache earns its place.
1A Quick Recap: Local and Shared Caches
Spring's cache abstraction lets us put the cache in one of two places. A local cache such as Caffeine lives inside each JVM. A read is a hash map lookup, well under a microsecond, with no network or serialization. But with 12 instances there are 12 separate copies, each filled by its own database loads and each expiring on its own clock. When a value changes, the other 11 instances keep their old copy until their TTL (time to live, how long an entry may stay) runs out, unless we tell them.
A shared cache such as Redis lives outside the service. All 12 instances read and write the same copy, so one eviction is seen by everyone. The cost is a network round trip, typically around half a millisecond to a millisecond inside a data centre, plus serializing and deserializing the value on every read. We can also combine the two: a short-lived local cache in front of Redis, which we call a two-level cache.
2Classifying the Data by Change Rate and Staleness
The question to ask for each kind of data is not just how often it changes, but what happens if a user sees an old value. That gives a maximum staleness we can promise, and the TTL follows from it.
| Data | Changes | Cost of a stale read | Choice |
|---|---|---|---|
| Product details | a few times a day | low: an old description for minutes | Caffeine 30 min + evict event, or Redis 1 h |
| Prices | every few minutes | medium: wrong price shown, fixed at checkout | Redis 60 s + evict on change |
| Stock levels | constantly | high: selling what we do not have | no cache, or Caffeine 2 s for display only |
3Choosing a Provider and Expiry for Each
Product details are the easy case. They are read on every page view and change rarely, so the hit ratio will be very high. A local Caffeine cache with a 30 minute TTL is the fastest option. The catch is that after an edit the other instances may show the old text for up to 30 minutes, so if the business cares we publish a product-changed event and each instance evicts its entry. If we prefer not to build that, Redis with a one hour TTL and eviction on update gives one copy for everyone.
Prices need tighter consistency. If 12 local caches each held prices for two minutes, two requests a second apart could show different prices. A shared Redis cache with a 60 second TTL, plus @CacheEvict when a price is updated, means all instances see a change at once and the TTL is only a safety net. The checkout should still read the price from the source of truth, so a stale display price can never become a stale charge.
Stock levels change on every order. Caching them for minutes would show items as available after they sold out. Usually we do not cache stock at all. If the database cannot take the read load of listing pages, a two second local cache for display only is reasonable, while reservation at checkout always goes to the database.
4Configuring a Different TTL per Cache
A single global TTL forces the most volatile data to set the expiry for everything, or the least volatile data to make everything stale. Spring Data Redis lets us configure each cache separately.
Notice that each Caffeine cache also gets a maximumSize, because a local cache without a bound grows until it hurts the heap, and recordStats(), because without statistics we cannot see whether the cache is helping.
5Measuring Whether the Cache Earns Its Place
Every cache costs memory, complexity and a new class of stale-data bugs, so it has to pay for itself. Spring Boot's actuator binds Micrometer metrics for the caches it knows about, such as cache.gets tagged with result=hit or result=miss, cache.puts and cache.evictions. From these we compute the hit ratio per cache.
We look at three numbers together. The hit ratio tells us how often the cache answers; a product cache should be above 90 percent, while a price cache with a 60 second TTL might sit at 70 percent and still help. The latency of the endpoint and the load on the database tell us whether the hits matter. And the rate of staleness complaints or support tickets tells us whether the TTL is too long. A cache with a 20 percent hit ratio that saves 2 milliseconds on a cheap query is cost without benefit, and we should remove it.
6Follow-up questions
How would you prove the cache actually reduced database load?
We measure before and after in the same conditions. We record the database's query rate and CPU for the queries the cache replaces, for example from the repository's timer metrics or the database's statement statistics, together with the cache hit ratio. Then we enable the cache on part of the fleet or behind a feature flag and compare. If the products query rate drops from 3,000 to 200 per second while the hit ratio reads 93 percent, the numbers agree and we have our proof. If the hit ratio is high but the database load does not move, the cache is in front of the wrong queries.
Why not use one Redis cache with a single five minute TTL for everything?
Because five minutes is too stale for prices and pointless for stock, while it throws away product details far more often than needed, so the hit ratio on the cheapest data suffers. A single TTL hides a decision that should be made per kind of data. Per-cache configuration costs a few lines and makes the staleness promise explicit.
When is a two-level cache worth it?
When a shared cache is not fast enough or one key is extremely hot, for example the home page product list read thousands of times per second on each instance. A local cache with a TTL of a few seconds in front of Redis absorbs most reads, and Redis keeps the instances roughly aligned. The price is two places to invalidate, so we keep the local TTL short.
7Conclusion
In this answer, we designed caching by starting from the data rather than from the tool. Product details tolerate long TTLs and fit a local Caffeine or shared Redis cache with eviction on update. Prices need a short TTL and cross-instance eviction, which a shared Redis cache provides. Stock is usually not cached, or only for seconds on display pages. We configured a separate TTL and size for each cache, and we decided to keep a cache only when its hit ratio, latency and database load show a real benefit.
Open with
I would start by classifying the data by how often it changes and how much staleness the business can tolerate, because the TTL is really a promise about staleness.
Then, if they want more
Product details change a few times a day, so a long TTL is fine: either a local Caffeine cache with maybe 30 minutes and a product-changed event to evict across the 12 instances, or Redis with an hour and eviction on update.
Prices change every few minutes, and with 12 local caches two requests could show different prices, so I would use a shared Redis cache with about a 60 second TTL and @CacheEvict when a price changes, and checkout would still read the real price.
Stock changes constantly, so I would not cache it, or at most for a couple of seconds on listing pages, never for reservations. In Spring I would configure TTLs per cache, using RedisCacheManagerBuilderCustomizer with withCacheConfiguration, or registerCustomCache on the CaffeineCacheManager with a maximum size, rather than one global TTL.
Then I would measure: hit ratio from the Micrometer cache metrics, endpoint latency, database query rate, and staleness complaints. If a cache does not reduce latency or protect the database, I would remove it, because it is a copy of data that can be wrong.
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 "You are adding caching to a product service that runs 12 instances behind a load...": https://scrivio-interview-library.netlify.app/databases/caching/d38246c7af/