Quick revision
The short answer
I would use three layers, each for what it does best. The CDN serves anonymous, cacheable responses using Cache-Control headers and purges on change; a distributed cache such as Redis holds computed product views shared by all instances; and a short-lived local cache keeps the hottest items in memory to save network calls. Changes invalidate all layers through events published after the transaction commits, with TTLs as a safety net, and we measure hit ratios per layer and remove any layer that does not earn its place.
What to remember
In this answer, we designed caching for a catalogue read 50,000 times per second and updated a few times per second. The CDN serves anonymous responses with Cache-Control headers and purge tags, Redis holds computed product views shared by every instance, and a short-lived Caffeine cache absorbs the hottest items. Changes invalidate every layer through events published after commit, with TTLs as a safety net, and per-layer hit ratios tell us which layers to keep.
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 use three layers, each for what it does best. The CDN serves anonymous, cacheable responses using Cache-Control headers and purges on change; a distributed cache such as Redis holds computed product views shared by all instances; and a short-lived local cache keeps the hottest items in memory to save network calls. Changes invalidate all layers through events published after the transaction commits, with TTLs as a safety net, and we measure hit ratios per layer and remove any layer that does not earn its place.
Why this question is asked
A catalogue read 50,000 times per second and updated a few times per second is close to the ideal caching case: reads outnumber writes by about ten thousand to one. The design challenge is not whether to cache but where, and how to make sure a price change reaches every layer quickly. Interviewers ask this design question to see whether we can reason about layers, invalidation and measurement together. In this answer, we will look at the traffic, design each of the three layers, connect them with invalidation after commit, and finish with how to measure whether each layer helps.
1Understanding the Traffic
50,000 reads per second is a lot for a database, but most of these reads are the same: a few thousand popular products receive most of the traffic, and most requests come from anonymous visitors who all see the same page. A few updates per second, such as price changes and stock status, touch a tiny fraction of products.
Those two facts drive the design. Identical anonymous responses can be cached at the edge, close to users, and never reach our services. Computed views can be shared. And because updates are rare, we can afford to invalidate precisely on each change instead of relying only on short expiry.
- CDN edgeanonymous product pages and API responses, about 85% of reads end here
- Spring Boot service, local Caffeinehottest items, 10 to 30 s TTL
- Rediscomputed product views, shared by all pods
- PostgreSQLsource of truth, only misses and writes
4 layers, from the top down. Step through them one at a time.
Each layer only sees what the layer above it missed.
2Layer 1: The CDN for Anonymous Responses
A CDN, content delivery network, caches HTTP responses at edge servers near users. For it to cache our responses, we send Cache-Control headers that say the response is public and how long it may be kept. A common pattern is a short browser lifetime and a longer CDN lifetime, plus stale-while-revalidate so the CDN can serve a slightly old copy while it fetches a fresh one.
Notice the purge tag. When a product changes, we ask the CDN to purge everything tagged product-42, so the edge does not serve the old price for ten minutes. Each CDN has its own header and API for this, so check yours. Responses that depend on the user, such as personalised prices or a logged-in cart, must not be cached publicly; they either bypass the CDN or are split so the shared part is cacheable and the personal part is loaded separately.
3Layer 2: Redis for Computed Product Views
Requests that reach our services, CDN misses, logged-in users and internal callers, still need product data. Building a product view may take several queries: product, prices, images, attributes and availability. We cache the computed view in Redis, keyed by product id and a version, so all instances share one copy and the work is done once per change rather than once per pod.
A long TTL, for example an hour, is fine here because invalidation does the real work; the TTL is only a safety net in case an invalidation is lost.
4Layer 3: A Short Local Cache for the Hottest Items
Even Redis has a cost at this volume: thousands of network calls per second per pod, plus deserialization. A small Caffeine cache in each instance, a few thousand entries with a 10 to 30 second TTL, absorbs the hottest products. Its size limit keeps heap use small and its short TTL bounds how stale it can get if an invalidation message is missed.
We add this layer only if measurements show Redis calls are a real cost. If the CDN absorbs most traffic, the local layer may not be worth its added staleness.
5Invalidation Through Events After Commit
When a product changes, every layer must learn about it. The service that writes the change publishes a ProductChanged event, but only after the database transaction commits. If we published before commit and the transaction rolled back, caches would drop a valid value, or worse, a consumer could reload the old value before the new one is visible and cache it again.
In Spring, a @TransactionalEventListener with phase AFTER_COMMIT runs after a successful commit. For reliability across services, the transactional outbox pattern, writing the event to a table in the same transaction and publishing it from there, guarantees the event is not lost if the process dies.
5 messages in order. Play them, or step through them one at a time.
Invalidate after commit; TTLs in every layer catch any message that is lost.
6Measure Hit Ratios per Layer
Each layer adds cost and a place for stale data, so each must justify itself. We measure the hit ratio per layer: the CDN reports its own, Caffeine exposes statistics through Micrometer when recordStats is enabled, and Redis cache hits and misses can be measured in the cache manager or from Redis itself. We also measure the time from a change to its visibility at the edge.
If the local layer's hit ratio is 20 percent and Redis latency is low, we remove it and accept a few more Redis calls. If the CDN hit ratio is low because of query parameters that make each URL unique, we normalise URLs instead of adding more layers.
| Layer | Requests seen | Hit ratio | Keep? |
|---|---|---|---|
| CDN | 50,000/s | 86% | yes |
| Local Caffeine | 7,000/s | 65% | yes, saves 4,500 Redis calls/s |
| Redis | 2,500/s | 97% | yes |
| PostgreSQL | about 75/s | - | comfortable |
7Follow-up questions
How quickly does a price change become visible?
With invalidation after commit, usually within a second or two: the event is delivered, Redis and local copies are evicted and the CDN purge completes. If a message is lost, the TTLs bound the delay, for example 30 seconds locally and ten minutes at the CDN, which is why prices that must be exact at checkout are always read fresh from the source of truth there.
Why not just use short TTLs everywhere and skip invalidation?
Because the TTL then has to be very short to keep prices fresh, which lowers hit ratios at every layer and raises load on the database. With only a few updates per second, precise invalidation is cheap and lets us use long TTLs for high hit ratios.
What protects the database if the caches are cold?
Coalesced loads per key, limits on concurrent loads, and the CDN's stale-while-revalidate and stale-if-error behaviour, so the edge can keep serving old copies while the origin recovers. Warming the hottest products after a Redis restart helps too.
8Conclusion
In this answer, we designed caching for a catalogue read 50,000 times per second and updated a few times per second. The CDN serves anonymous responses with Cache-Control headers and purge tags, Redis holds computed product views shared by every instance, and a short-lived Caffeine cache absorbs the hottest items. Changes invalidate every layer through events published after commit, with TTLs as a safety net, and per-layer hit ratios tell us which layers to keep.
Open with
With about ten thousand reads per write and mostly anonymous traffic, I would use three layers. At the edge, a CDN caches anonymous product responses: the service sends Cache-Control public with a short max-age for browsers, a longer s-maxage for the CDN and stale-while-revalidate, plus an ETag and a surrogate key tag per product so we can purge it.
Then, if they want more
Personalised parts stay out of the shared cache. Behind it, Redis holds the computed product view, which takes several queries to build, shared across all pods, with a long TTL as a safety net.
In each pod, a small Caffeine cache with a short TTL, ten to thirty seconds, holds the hottest products to save Redis calls, but only if measurements show it helps.
For freshness, the service that changes a product publishes an event after the transaction commits, with a TransactionalEventListener or an outbox for reliability, and consumers evict the Redis key, broadcast a local eviction to every pod and purge the CDN tag. Publishing after commit avoids caching a value that was rolled back.
Finally I would measure hit ratios per layer and the time for a change to reach the edge, and remove any layer that does not pay for itself. Checkout still reads prices fresh from the source of truth.
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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 "Design the caching layers for a product catalogue read 50,000 times per second and...": https://scrivio-interview-library.netlify.app/spring-boot/scalability/c0c2ff354c/