DEV Community

Cover image for One Sale Invalidated 1,300 Cache Entries in Our Next.js Shop
Artur Pokładek
Artur Pokładek

Posted on AI-assisted

One Sale Invalidated 1,300 Cache Entries in Our Next.js Shop

On November 16 Vercel sent us an email about the account that hosts the shop from my previous post. Less than ten days into the billing cycle, 15 of the 20 dollars of included credit were gone.

At that pace the cycle would end at 45 to 50 dollars. The cost export put the shop alone at $14.56 for November 7 to 16, and the CDN traffic in it cost nothing. More than half was the cache being written, and one cause sat in our handler for a Shopify stock webhook, which marked about 1,300 cache entries stale on every sale.

The shop is Lady Blue, a small headless Shopify store for ceramics, running on Next.js 16 and Vercel Pro under our studio's own account. On November 21 it cost 25 cents for the day. This is what moved the number, with the parts that are still wrong at the end.

Where the money went

The cost export in the Usage tab, filtered to the shop's project, November 7 to 16:

Item Cost
ISR Writes (1.67 million units) $8.66 (59%)
Deployment Storage $2.25
Fast Origin Transfer $1.45
Fluid Active CPU $1.23
Everything else $0.97

274 thousand CDN requests in those ten days cost zero. Everything below comes from the Usage panel, Observability and 2,000 requests pulled with vercel logs.

Vercel Consumption Breakdown for the shop's project, daily bars from November 7 to 22, with the blue ISR Writes segment dominating through November 16 and much shorter bars after it

The blue segment is ISR Writes, and the sliver on the far right is November 22, a partial day. The rest of this post is about the drop after the 16th.

One sale, 1,300 stale cache entries

The inventory_levels/update webhook called revalidateTag("katalog"), and that one tag sat on about 1,300 cache entries. A sale changes a stock level, so each sale marked all of them stale.

Shopify also sends products/update when stock changes. Our handler for that one invalidated the home page and all collections as well, and it did that twice.

Stock levels sync every 5 minutes, so this ran on every sale and on every sync. In one hour the logs showed 416 invalidations by tag.

An hourly timer I set myself

Every Storefront API call also had revalidate = 3600, and so did the dictionaries, the currency in the layout and the decoration pages. That one hour was my own fix from October 21. Before it the cache lived 2 to 5 minutes, and on the Hobby plan we had used 98.8% of the ISR writes limit.

I raised it to an hour with one constant for the whole catalogue and assumed the webhooks would keep things fresh. I never checked how many pages a single webhook invalidated. I had treated the symptom, which was time, while every sale still marked the whole shop stale, and the longer cache barely helped.

With the hour in place, the first visit or menu prefetch after it expired got a STALE page and started a regeneration in the background. In the logs, 411 of 809 responses for product cards were STALE. In 12 hours that meant about 2,200 regenerations and 125 thousand write units, and one product card was written about 30 times.

Pages that looked the same, bytes that didn't

Vercel's ISR docs say that when revalidation runs and the content hasn't changed, "no ISR write units are incurred." To me our pages looked unchanged, with neither random keys nor dates in the HTML. On November 18, with the first fixes already live, I tested that.

A script polled seven pages every minute. When one came back STALE, it saved that copy, fetched the regenerated page 20 seconds later and compared the two byte by byte. In 40 minutes of read-only requests it collected 16 pairs.

Two of them, the club page and a payment help page, came back identical, with the same ETag, as the docs describe. The other 14 differed, and none of the causes was new Date() or Math.random().

Twelve pairs were the first regeneration after a deploy. The visible HTML was identical in all of them, and the RSC payload differed from the build output by 3 or 4 bytes of internal Next.js markers. Every deploy therefore starts a wave of writes, one for each page somebody visits.

The last two were product cards with no deploy in between. On one, the chunks of the RSC stream arrived in a different order, and on the other the related products section listed the same products in a different order.

The payloads held a third source that needs no diff. Product lists carried quantityAvailable for every tile, 210 times on the mug list alone, and the stock sync keeps changing those numbers, which is a real change in content.

I have no way to see whether Vercel billed those specific writes, only that the bytes and ETags changed.

Refresh only what really changed

The time-based revalidate went from one hour to 24 hours and stays as a safety net. Webhooks do the real refreshing, and a cron job invalidates tags once a night with the max profile, which serves the old page while the new one renders. The cart and the checkout read price and stock from Shopify live, so an older list cannot sell anything at the wrong price.

For the webhooks we added a content hash: a sha256, computed from the Admin API, of everything the shop shows about a product, with availability as a plain yes or no. The stock count stays out of it, and so do metafields that change on their own, like the nightly Omnibus price record. The hash is stored in a product metafield.

  • A stock change refreshes only that product's card.
  • Lists, the home page and collections refresh only when the hash changes, for example when a product goes from sold out to in stock.
  • Saving the metafield triggers another products/update. That one sees the same hash and does nothing, so there is no loop.
  • If reading the hash fails, the handler falls back to the old broad refresh.

The searchParams that switched off the cache

Category pages read searchParams for the filters. The Next.js docs say that this "will opt the page into dynamic rendering at request time."

Every visit to a category was therefore rendered on the server, and so was every prefetch from the menu links that sit on every page. In 12 hours the category lists ran about 2,900 times with zero cache hits, and they used more CPU than anything else.

Now page.tsx doesn't touch searchParams and the page is static. A URL with filter parameters gets rewritten by the proxy to a /filtrowane variant ("filtered" in Polish), which is the same code rendered dynamically.

Page weight, billed again on every write

Vercel measures ISR writes in 8 KB units, and its guide to reducing ISR writes puts it this way: "Larger changed output needs more units each time Vercel stores it."

Most of what we cut was data the page did not need. Every page carried all 21 translation namespaces, about 66 KB, and now gets only its own, as the next-intl docs suggest. The search box in the header received the full dictionary of 68 decorations, about 43 KB, and it gets 4 fields now.

Product tiles used to receive the whole product, about 4.2 KB with the description twice and the reviews. A dedicated tile type and a mapper now decide what crosses to the client, and the compiler refuses a tile that asks for more.

The pages look the same as before, measured on production:

Page Before After
Collections (/kolekcje) 1,303 KB 634 KB
Category 765 KB 407 KB
Search 610 KB 321 KB
Product card 330 KB 232 KB

Cutting translations per page is a good way to ship a button that shows a translation key instead of a label. A static test now checks that every useTranslations call has its texts, and 235 Chrome screenshots (3 languages, 3 widths) were compared pixel by pixel before and after.

40 stored builds, one in use

With the writes down, Deployment Storage became the biggest line, at 28 cents a day on November 17. Vercel bills it at $0.10 per GB-month, and the cost export showed 84 GB of stored builds for this one project.

The Resources tab of a deployment explained most of it.

Vercel Resources tab of one deployment: Functions 1162, with route after route listed at 1.77 MB

1,162 functions, nearly all of them 1.77 MB, because every product, decoration and collection page was prerendered at build time in three languages. The build log said "Generating static pages (1145/1145)". On paper that is about 2 GB per deployment, though the storage meter later worked out to roughly 1 GB per build, for a reason I haven't found.

Previews added to the pile: every push to develop built a preview, including commits that changed only documentation, and a merge built the same commit twice, once for main and once for develop, in the same minute.

On November 18 I shortened retention to one day for previews and failed builds and one week for production, down from 30 days at the start. Storage fell to 40 GB and 13 cents a day, and then stopped falling.

Vercel's retention exceptions keep the last 20 ready production deployments and the last 20 ready previews whatever the policy says. The API listed exactly that for our project: 20 production, 20 preview, and 2 more that never became ready.

So on November 21 I deleted all of them except the one serving the shop. Any old version can be rebuilt from git, but there is no one-click rollback now, and that was a deliberate trade. The storage line for that day came out at 0.7 GB and less than a cent.

After

The webhook, revalidate and searchParams changes went live on November 16, the page weight cuts on the 17th, retention on the 18th and the build cleanup on the 21st. Daily numbers for the shop's project alone, from the cost export:

Day ISR Writes Deployment Storage Total for the day CDN requests
Nov 15, Sunday (before) 185,619 units, $0.97 $0.27 $1.56 18,042
Nov 17 34,171 units, $0.18 $0.28 $0.66 36,708
Nov 19 (no deploys) 41,305 units, $0.22 $0.13 $0.54 32,609
Nov 20 (no deploys) 44,323 units, $0.23 $0.13 $0.55 28,367
Nov 21, Saturday (builds deleted) 19,805 units, $0.10 $0.002 $0.25 21,215

Stacked bars of the shop's daily Vercel cost, November 7 to 21, split into ISR Writes, Deployment Storage and everything else: 0.92 to 2.32 dollars a day before the fixes of November 16, 0.54 to 0.76 after them, 0.25 on November 21 once the old builds were deleted

November 17 had twice the requests of November 15 and less than a fifth of the ISR writes. On November 19 and 20, with nothing deployed, the shop cost 54 and 55 cents a day, and storage was almost a quarter of that.

November 21 came in at 25 cents, 30 below the day before. Storage accounts for 13 of those cents, and that part is the cleanup. ISR writes account for another 13: they halved on a Saturday with fewer requests, and I can't yet tell how much of that is traffic, so I'm not counting it as a fix.

On the morning of November 21 the account's cycle stood at $18.34 of the included 20 dollars. It closes on December 7, and I'll leave the monthly total until then.

What is still wrong

ISR writes are the top line again, between 20 and 44 thousand units a day with no deploys, and the diff test shows where to look. Product tiles display only sold out or available, and the lists still carry the exact stock count for every tile. The related products sort has no stable tie-breaker.

The nightly cron still marks the whole catalogue stale, about 1,145 pages, even though the content hash now handles product changes. Every build still prerenders products and decorations in three languages, and every push to develop still builds a preview, so storage will creep back up unless the builds get smaller or fewer. None of that is done yet.

The content hash and the /filtrowane rewrite work for us, and I wouldn't call either best practice. With the hash, the webhook handler makes an Admin API call, plus a metafield write whenever a product really changes. The rewrite puts a proxy in front of every request, where client-side filtering with useSearchParams would be the more idiomatic route.

I also can't say how any of this behaves on a store with thousands of products. These numbers come from one small shop and two weeks of usage.

The project now has a spend limit of 50 dollars that pauses it, set before the next bill could set it for us.

The shop was built and is maintained by Hex16, our small web studio in Poland (the site is in Polish), and this bill was ours to fix.

If you run Next.js on Vercel, open the Usage tab, filter it to one project and look at the top line for the current cycle. Ours was ISR Writes at 59%. What is yours?

Top comments (2)

Collapse
 
userxnom profile image
Abdullah Naseer •

Really satisfying write-up — the part that stuck with me is that you verified the page-weight cuts with 235 Chrome screenshots compared pixel by pixel, so the pages weren't just cheaper but provably identical. The nightly cron is the part I'd chase next: 1,145 pages marked stale every night even though the content hasn't changed is the same damage the webhook used to do, just on a schedule. Could the cron use the same content hash before marking pages stale, or does it revalidate for a different reason?

Collapse
 
friga profile image
Artur Pokładek •

Good catch, and you're right: it's the same blast radius, just once a night instead of on every sale. The cron is a nightly safety net and invalidates three tags: catalogue, dictionaries and redirects. Redirects are only read on a product 404, so that one is cheap. The other two both cover every page, because the dictionaries (decorations, shapes) are read by the layout too, so dropping only the catalogue tag would change nothing.

Honest answer though: our cache already expires every 24 hours, so a page a crawler visits gets rebuilt about once a day anyway. The nightly invalidation at most moves that rebuild earlier. Hash-checking it would only pay off together with a much longer cache lifetime, and at our size that's a few dollars a month. I'll check that against the bill when the cycle closes.