Grokking the Modern Frontend System Design Interview
Vote

0% completed

​

Static Generation and Incremental Regeneration

Building Pages Ahead of Time

Stale Pages

Rebuilding One Page at a Time

Rebuilding When the Data Changes

Building Only Some Pages at First

The Four Choices Side by Side

Using This in an Interview

Key Takeaways

Practice Questions

Static Generation and Incremental Regeneration
Static Generation and Incremental Regeneration

Basket sells 4,000 products, and each one has its own page. Suppose rendering one product page takes 50 ms. Building every page ahead of time then takes 4,000 times 50 ms, which is 200 seconds on one processor core, or 25 seconds spread over eight cores. After that, every shopper receives a file that already exists, and no server renders anything for a visit.

Now suppose Basket's buyers change 200 prices an hour. Rebuilding all 4,000 pages after every change would mean 200 builds an hour, each 25 seconds long on eight cores. That is about 83 minutes of building every hour, which cannot be done, and a shopper would still see an old price while each build runs.

The answer is to build pages once, ahead of time, and then rebuild only the pages that changed. The sections below show how each part works and what it costs.

Building Pages Ahead of Time

Static site generation (SSG) renders pages once, at build time, and stores the HTML as files that are served unchanged to every visitor. The build runs the same components that server rendering would run, with the same data, but it does so before anyone asks for the page.

The files usually go to a CDN. A request that the CDN can answer from its copy is a cache hit, and it never reaches Basket's own servers. A request it cannot answer goes to the origin, the server that holds the original file.

The cost moves from request time to build time, so it grows with the number of pages, not with the number of visitors.

PagesOne core, 50 ms eachEight cores
500 best sellers25 sabout 3 s
4,000 products200 s25 s
40,000 products2,000 s, about 33 minutes250 s, about 4 minutes

Those times use the assumed 50 ms per page. A real build also fetches the data for each page, and that time is added to the 50 ms.

Stale Pages

A page is stale when its content is older than the data it shows. A product page built at 9:00 says 2.49 until the next build, even if the price became 2.99 at 9:05.

Two rules keep a stale page from doing harm. First, the price that the shopper pays always comes from the API when the cart is checked and the order is placed, never from the HTML. Second, a value that must be current on the page itself, such as stock left, can be fetched by the browser after the static page loads. The page then shows the shared parts at once and fills in the one live value a moment later.

The same rule covers personal content. A static page is the same file for every shopper, so it cannot hold the cart count or "Hello, Ana". Those parts are fetched in the browser after the page loads, and a personal value must never be written into a file that the CDN shares.

Rebuilding One Page at a Time

Incremental static regeneration (ISR) serves a prebuilt page from the cache and rebuilds that one page in the background when it is too old or when the app is told it changed. The name comes from Next.js; other frameworks offer the same idea under other names.

The time-based form gives each page a revalidation period. In the Next.js App Router, one line in a page file sets it:

// app/product/[id]/page.tsx export const revalidate = 60 // seconds

Here is what happens to one product page with a 60 second period:

  1. At 0 s the page is built and cached.
  2. At 30 s a shopper asks for it and gets the cached copy. The page is fresh.
  3. At 45 s the price changes in the product service. The cached page still shows the old price.
  4. At 70 s a shopper asks for it. The page is older than 60 s, so this shopper still gets the old copy, and the request starts a rebuild in the background.
  5. At 72 s the rebuild is finished. Every later shopper gets the new price.
One page with a 60 second revalidation period
One page with a 60 second revalidation period

Look at the stretch between 45 s and 72 s: the old price was on screen for 27 seconds, and the shopper at 70 s was the one who triggered the rebuild but did not get it. Notice that nothing happens at 60 s itself. A page is rebuilt only when someone asks for it after the period ends.

That last point sets the real limit on staleness. A busy page that is requested every second is never more than about 60 seconds plus one rebuild out of date. A quiet page that nobody opens for six hours keeps the six-hour-old copy, and the first shopper after the quiet period sees it once.

Rebuilding When the Data Changes

The on-demand form replaces the timer with a message. When a price changes, the product service tells the frontend's server which page changed, and the server marks that page as out of date. In Next.js the server code calls revalidatePath('/product/123'); other frameworks and CDNs offer a purge request with the same effect.

Rebuilding one page when its price changes
Rebuilding one page when its price changes

Look at how little is rebuilt: one page per change, not 4,000. Notice that the message names a page, so the product service must know which pages show a product, such as the product page and any category page that lists it.

The arithmetic from the opening now works. 200 price changes an hour means about 200 page rebuilds an hour, at 50 ms each, which is 10 seconds of rendering per hour. Many teams use both forms together: a rebuild on every change, plus a long timer, such as one hour, in case a message is lost.

Building Only Some Pages at First

A build does not have to include every page. Basket can build its 500 best sellers ahead of time, in about 25 seconds on one core, and build the other 3,500 the first time someone asks for each one. The first shopper on a rare product waits for one server render, and every shopper after that gets the cached copy.

The Four Choices Side by Side

Static generationISR, time-basedISR, on demandServer rendering
When the page is builtAt buildAt build, then after the periodAt build, then after a changeOn every request
Server work per visitNoneNone on a cache hitNone on a cache hitOne render
How stale it can beUntil the next buildThe period, longer on quiet pagesSeconds after the messageNot stale
Personal contentFetched in the browserFetched in the browserFetched in the browserCan be in the HTML
Good fit on BasketHelp and policy pagesCategory pagesProduct pagesSearch results, checkout

Using This in an Interview

Name the pages and the freshness rule together: "Product pages are static and rebuilt on demand when the price changes; the price the shopper pays is checked again by the API at checkout."

Two follow-up questions are likely:

  • "How long can a shopper see an old price, and what happens if they add it to the cart?"
  • "What does a build cost when the catalog grows from 4,000 products to 400,000?"

Key Takeaways

  • Static site generation renders pages at build time, so a visit costs no server rendering and a CDN can serve the page close to the shopper.
  • Build time grows with the number of pages: at an assumed 50 ms per page, 4,000 pages take 200 seconds on one core.
  • A static page can be stale, so the price the shopper pays comes from the API at checkout, and live or personal values are fetched in the browser.
  • Incremental static regeneration rebuilds one page at a time, either after a period, when someone next asks for it, or on demand when the data changes.
  • With a time-based period, a quiet page can stay stale for much longer than the period, because it is rebuilt only when requested.

Static and regenerated pages still send the whole page in one response. The next lesson, Streaming and Server Components, sends a server-rendered page in pieces, as each piece becomes ready.

Practice Questions

Try each question first, then open the answer.

1. Basket grows to 40,000 products and rendering a page takes 50 ms. How long does a full build take on eight cores, and what would you build ahead of time instead?

<details> <summary>Show answer</summary>

About 250 seconds, a little over four minutes. 40,000 times 50 ms is 2,000 seconds of work, and eight cores divide it to 250 seconds. A better plan builds the best sellers ahead of time and renders every other page the first time someone asks for it, then keeps it in the cache.

</details>

2. A product page has a revalidation period of 300 seconds. Nobody opens it for two hours, and the price changed one hour ago. What does the next shopper see?

<details> <summary>Show answer</summary>

The old price. The page is rebuilt only when someone asks for it after the period ends. That shopper's request gets the cached copy and starts the rebuild, so the shopper after them sees the new price. A rebuild message from the product service would have avoided the wait.

</details>

3. A teammate wants to show "Hello, Ana, you have 3 items in your cart" at the top of the static product page. What goes wrong, and what is the repair?

<details> <summary>Show answer</summary>

The CDN would serve Ana's greeting and cart to every shopper who opens that page. A static page is one file shared by everyone. The repair is to keep the shared parts in the static HTML and fetch the greeting and the cart count in the browser after the page loads.

</details>

4. Why does a shopper never pay a stale price, even when the product page shows one?

<details> <summary>Show answer</summary>

The cart and checkout take the price from the API, not from the HTML. The page's price is for display only. When the shopper places the order, the server uses its current price, and the checkout screen shows any change before the shopper pays.

</details>

5. With on-demand rebuilding, the product service sends one message per price change. Name one risk in that design and one way to cover it.

<details> <summary>Show answer</summary>

A lost message leaves a page stale until something else rebuilds it. Adding a long time-based period, such as one hour, puts a limit on how long a missed change can last. A second risk is a missing page in the message, such as a category page that also lists the product.

</details>

Reading Progress

0%


Vote for new content

On This Page

Building Pages Ahead of Time

Stale Pages

Rebuilding One Page at a Time

Rebuilding When the Data Changes

Building Only Some Pages at First

The Four Choices Side by Side

Using This in an Interview

Key Takeaways

Practice Questions