Quick revision
The short answer
When a page is rendered we record, for each product on it, SADD deps:product:<id> <pageKey>, with the set's TTL at least as long as the page's. When a product changes we read its set, UNLINK the page keys and delete the set in one pipeline, and re-render the most popular pages right away. We bound the fan-out by capping invalidation per change or by caching per-product fragments, guard against a page rendered with old data being stored after the invalidation, and first ask whether price and stock should be fetched live on top of a cached page skeleton.
What to remember
In this answer, we saw that page caching needs a reverse index from product to pages, recorded with SADD at render time and given a TTL at least as long as the pages. A change reads the set with SSCAN, unlinks the pages and deletes the set in a pipeline, re-rendering the busiest pages at once. We bound fan-out with caps or per-product fragments, close the render-versus-invalidate race with stored versions or a delayed second delete, and often remove most of the work by serving price and stock live on top of a cached skeleton.
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.
When a page is rendered we record, for each product on it, SADD deps:product:<id> <pageKey>, with the set's TTL at least as long as the page's. When a product changes we read its set, UNLINK the page keys and delete the set in one pipeline, and re-render the most popular pages right away. We bound the fan-out by capping invalidation per change or by caching per-product fragments, guard against a page rendered with old data being stored after the invalidation, and first ask whether price and stock should be fetched live on top of a cached page skeleton.
Why this question is asked
Rendered pages are expensive to build and cheap to serve, so caching them pays off, but a page depends on many products and a product appears on many pages. That many-to-many relationship is what makes invalidation hard. Interviewers ask this to see whether we can design the dependency index, keep it from growing forever, and notice the race that leaves stale pages behind. In this answer, we will size the problem, record dependencies at render time, invalidate on change, bound the fan-out, close the race, and finally question whether the design needs to be this precise.
1Sizing the Problem
Assume 200,000 cached listing pages, counting sort orders and page numbers, each showing up to 50 products, and a popular product appearing on 300 pages. If 100 price or stock changes happen per second at peak, each change touches 300 pages on average, so we must invalidate about 30,000 page keys per second. That is well within what Redis can delete, but it means the invalidation path is a real workload, not an afterthought, and a single bestseller on 5,000 pages produces a burst of 5,000 misses every time its stock changes.
Freshness within a few seconds rules out relying on TTLs alone, since a TTL short enough for that would re-render every page constantly. So we need to know, for any product, which pages show it.
2Recording Dependencies at Render Time
The renderer already knows which products it put on the page. After rendering, it stores the page and adds the page key to one set per product, all in one pipeline.
The set's TTL must be at least as long as the page's, plus a margin. If the set expired first, a later product change would find no set and leave a cached page untouched. Refreshing the set's TTL on every add keeps it alive while pages keep being rendered, and lets it disappear once no live page refers to the product. Pages that expire on their own leave their key behind in the set, which is harmless: deleting a key that no longer exists costs nothing, and the set's own TTL cleans the leftovers up.
3Invalidating When a Product Changes
Product changes arrive as events, for example on a Kafka topic or from change data capture. For each change, the invalidator reads the product's set, deletes the pages, and deletes the set itself. For sets with thousands of members it reads with SSCAN in chunks rather than SMEMBERS, so no single command returns a huge reply. In Redis Cluster the set and the pages live in different hash slots, so a Lua script cannot touch them all; the invalidator does it in a pipeline instead.
Deleting popular pages creates a burst of misses exactly when they are most requested. So for the top pages by traffic, the invalidator re-renders them immediately and overwrites the key rather than only deleting it, and the rest are simply removed and rebuilt on demand.
7 messages in order. Play them, or step through them one at a time.
4Bounding the Fan-Out
A bestseller on 5,000 pages that changes stock every few seconds would keep thousands of pages permanently invalidated. Two techniques bound that. The simple one is a cap: invalidate at most, say, 500 pages per change immediately, chosen by traffic, and give pages a TTL short enough that the rest catch up within the tolerance. The structural one is composition: cache each product card as its own fragment, product:card:<id>, and build the page from a cached skeleton plus 50 fragments fetched with one MGET. A product change then invalidates one fragment, and the dependency index disappears for everything that lives inside the card.
Composition costs some work on each request, assembling 50 fragments, but that is a cheap string operation compared with rendering, and it changes the invalidation cost from hundreds of pages to one key.
5The Race That Leaves Stale Pages
There is a subtle problem. A renderer reads product 5001 at version 5 and starts building a page. Meanwhile the price changes to version 6, and the invalidator deletes all pages that show 5001. But our page has not been stored yet, so there is nothing to delete. A moment later the renderer stores its page, built with version 5, and it stays stale until its TTL.
Two fixes work. Store the versions used in the page alongside it, for example a small hash of product id to version, and have the invalidator record the latest version per product; on read, or before storing, the page is rejected if any product it used is older than the latest known version. A cheaper approximation is a delayed second invalidation a few seconds after the first, which catches pages stored by renders already in progress. And for the fields where staleness really matters, the safest fix is the next section.
6Do Price and Stock Need to Be in the Cached Page?
Before building all this, ask which fields need seconds-level freshness. Names, images and descriptions change rarely; price and stock change constantly. If the cached page holds only the slow fields, and price and stock are filled in per request from a fast source, such as HMGET on a Redis hash of current prices or a batch call to the price service for the 50 ids, the page cache only needs invalidating on rare catalogue edits. The hard dependency tracking shrinks to almost nothing, and the race above no longer matters for the fields customers care about.
| Approach | Invalidation work per change | Freshness |
|---|---|---|
| Whole page + dependency sets | Hundreds of pages | Seconds, with the race to handle |
| Capped invalidation + short TTL | At most N pages | Seconds for top pages, TTL for the rest |
| Per-product fragments | One fragment | Seconds |
| Cached skeleton + live price and stock | Rare, on catalogue edits | Live for price and stock |
7Follow-up questions
How would this change if pages were cached at a CDN instead?
We would let the CDN keep the dependency index. The origin tags each page response with the products it shows, for example Surrogate-Key or Cache-Tag headers listing product-5001 and the other 49, and on a change we send one purge request for product-5001. CDN purges take a few seconds to propagate and providers rate-limit purge APIs, so a product that changes very often should not be purged each time; that is another reason to load price and stock from an API in the browser rather than bake them into the cached HTML.
Why not store the dependencies in the page instead of per product?
Because invalidation starts from the product. When product 5001 changes, we need its pages, and finding them from a per-page list would mean scanning every page. The per-product set is the reverse index that makes the lookup direct.
What if the invalidator falls behind?
Pages stay stale for as long as it lags, bounded by their TTL. We alert on the time from the change event to the deletes, and keep page TTLs short enough, say 10 minutes, that a stuck invalidator cannot cause hours of stale pages.
8Conclusion
In this answer, we saw that page caching needs a reverse index from product to pages, recorded with SADD at render time and given a TTL at least as long as the pages. A change reads the set with SSCAN, unlinks the pages and deletes the set in a pipeline, re-rendering the busiest pages at once. We bound fan-out with caps or per-product fragments, close the render-versus-invalidate race with stored versions or a delayed second delete, and often remove most of the work by serving price and stock live on top of a cached skeleton.
Open with
At render time I know which products are on the page, so after storing the page with a TTL I add the page key to a set per product, SADD deps:product:id pageKey, and refresh that set's TTL to slightly more than the page TTL, so the index never expires before the pages it points to.
Then, if they want more
When a product changes, an invalidator reads the set with SSCAN, UNLINKs the pages and deletes the set in a pipeline, because in Redis Cluster these keys are in different slots. For the most visited pages it re-renders immediately, so we do not get a burst of misses.
I would bound the fan-out, since a bestseller may be on thousands of pages: either cap immediate invalidation and let short TTLs handle the rest, or cache per-product fragments so a change invalidates one key.
I would also handle the race where a page rendered with old data is stored just after the invalidation, either by storing the product versions used with the page or with a delayed second delete. But first I would ask whether price and stock need to be inside the cached page at all.
If the page skeleton is cached and those two fields are fetched live, the dependency tracking is only needed for rare catalogue edits.
About this answer
- Last updated
- 13 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 dependency tracking and invalidation for these pages, including the data...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/b2530f710a/