Grokking the Modern Frontend System Design Interview
Vote

0% completed

​

Client Rendering and Server Rendering

Two Places to Build the Page

The Order of Requests

Measured on One Machine

What Each Form Costs

Single-Page and Multi-Page Apps

Using This in an Interview

Key Takeaways

Practice Questions

Client Rendering and Server Rendering
Client Rendering and Server Rendering

Basket's product list page shows the same 30 products to every shopper, and it can reach the browser in two forms. In the first form, the server sends 0.2 KB of HTML that contains an empty <div> and two script tags, and the browser builds the list itself. In the second form, the server sends 2.1 KB of HTML with all 30 products already written into it.

Both forms ask the same server for the same data, which takes 80 ms, and both download the same JavaScript. Yet a search engine that reads the first response finds no product names at all, and on a slow phone the shopper waits more than a second longer for the first row.

This page compares the two ways of building a page, measures them on one machine, and explains what each one costs.

Two Places to Build the Page

Rendering can happen in the browser or on the server, and the two choices have names.

Client-side rendering (CSR) sends an almost empty HTML page and a JavaScript bundle; the bundle fetches the data and builds the page in the browser. Here is the whole HTML of Basket's client-rendered list, from the lab sample:

<!doctype html> <html> <head><title>Basket</title></head> <body> <div id="root"></div> <script src="/library.js"></script> <script src="/app.js"></script> </body> </html>

Server-side rendering (SSR) runs the page's components on the server for each request and sends finished HTML; the browser shows it as soon as it arrives. The same JavaScript still downloads afterwards, because the buttons need code. Attaching that code to HTML that is already on the screen is called hydration.

The sample uses one app.js for both forms. It builds the list only when the server did not:

async function start() { const root = document.getElementById('root') if (!root.children.length) { const products = await (await fetch('/api/products?page=1')).json() root.innerHTML = `<ul>${products.map(row).join('')}</ul>` } root.addEventListener('click', (e) => { if (e.target.dataset.id) window.cart = e.target.dataset.id }) window.readyAt = performance.now() }

The Order of Requests

The two forms send the same requests in a different order, and the order decides when the first row can appear.

The order of requests in each form
The order of requests in each form

Look at the client row: the request for products cannot start until both scripts have downloaded and run, because the code is what sends it. Notice that in the server row the list is drawn before any JavaScript arrives, and the scripts only make the buttons work.

The server form has one cost at the start. The server cannot send the first byte of HTML until it has the data, so time to first byte (TTFB), the time from the request to the first byte of the response, includes the 80 ms of server work. The client form sends its shell at once, then pays for the data request later.

Measured on One Machine

The lab served both forms from one local server with Lighthouse's slow mobile profile (150 ms round trip, about 200 KB per second) and the CPU slowed four times. A stand-in library of 174 KB plays the part of a framework. Each form loaded five times in a fresh browser profile with no cache, in headless Chrome 150 on a desktop with an AMD Ryzen 7 5800X.

Output:

library.js: 174 KB, uncompressed client rendering: HTML 0.2 KB, product names in the HTML: 0 server rendering: HTML 2.1 KB, product names in the HTML: 30 client rendering: products on screen 1516 ms, buttons respond from 1462 ms (medians of 5) server rendering: products on screen 264 ms, buttons respond from 1296 ms (medians of 5)

"Products on screen" is the browser's first contentful paint, the moment it first draws any text or image. On both pages the first thing drawn is the product list, so the two measures are the same here. "Buttons respond from" is the moment start() finished attaching the click handler. In the client form, that happens in the same task that inserts the list, before the browser paints the list, so its time is slightly earlier than the paint.

Measured on one machine
Measured on one machine

Look at the server rendering row first. The products appeared about 1,250 ms sooner. Notice that its buttons did not respond until 1,296 ms, so for about one second the list was visible but could not be used.

A rough estimate shows why the client form is slow. The library alone is 174 KB, which at 200 KB per second is about 0.87 seconds. The HTML, the scripts and the product request each pay at least one 150 ms round trip, and they happen one after another, which adds about 0.45 seconds. That is about 1.3 seconds of network time alone. Running the library and the app code on the slowed CPU comes on top of it, between the script downloads and the product request. The estimate is arithmetic on the profile, not a breakdown measured by the run.

What Each Form Costs

Server work. A client-rendered shell is the same file for everyone, so it can be served from a CDN (content delivery network), a set of servers in many cities that keep copies of files close to users. It costs the application server nothing to build. A server-rendered page runs the components for every request. Suppose one render takes 20 ms of server CPU and Basket serves 300 page views per second at its busiest. That is 6 seconds of CPU every second, or six processor cores doing nothing but rendering.

Search engines and link previews. A crawler that reads only the HTML sees the server-rendered page in full and the client-rendered page as an empty <div>. Google's crawler can run JavaScript, but it does so in a later step. Many of the tools that build link previews for chat and social apps read only the HTML.

Later navigations. After the first load, a client-rendered app already has its code, so moving to another page needs only a data request.

Single-Page and Multi-Page Apps

A multi-page app (MPA) loads a new HTML document from the server for each navigation. A single-page app (SPA) loads one document once; after that, its JavaScript changes the URL with the History API, fetches only data, and redraws part of the page.

The two ideas are separate from where the first page is rendered. Many modern frameworks, such as Next.js, render the first page on the server and then behave as a single-page app for every later navigation. The shopper gets fast first content and fast later moves, at the price of the server rendering cost and the hydration wait.

Client-side renderingServer-side rendering
First HTMLEmpty shell, the same for everyoneFinished page, built per request
First contentAfter the code downloads, runs and fetches dataWhen the HTML arrives
Buttons respondWhen the list is drawnAfter the code downloads and hydrates
Time to first byteFast, a static fileIncludes the server's data work
Server costAlmost none, served from a CDNOne render per request
Search and link previewsContent missing from the HTMLContent in the HTML
Best fitPages behind a login with no search trafficPublic pages that must appear fast and be found

Using This in an Interview

State the choice per page and the reason: "The product list is public and must appear fast, so I render it on the server and hydrate it. The account pages sit behind a login, so the browser can render them."

Two follow-up questions are likely:

  • "What does the shopper see, and what happens to a tap, between the first paint and hydration?"
  • "How much server capacity does rendering every product page per request need?"

Key Takeaways

  • Client-side rendering sends an empty shell and builds the page in the browser; server-side rendering sends finished HTML built for each request.
  • In the client form, the data request waits for the code to download and run. In the lab, server rendering showed the products at 264 ms against 1,516 ms for client rendering.
  • Server rendering does not make the page usable sooner. Its buttons responded at 1,296 ms, about one second after the products appeared, because the same code still had to arrive and hydrate.
  • Server rendering costs server CPU on every request and delays the first byte by the data work; a client shell is a static file.
  • Crawlers and link-preview tools that read only HTML see nothing in a client-rendered page. Single-page and multi-page describe navigation, which is a separate choice from where the first page is built.

Server rendering runs the same work for every visitor, even when every visitor gets the same HTML. The next lesson, Static Generation and Incremental Regeneration, builds those pages once, ahead of time.

Practice Questions

Try each question first, then open the answer.

1. A client-rendered page downloads a 400 KB bundle on the slow profile (200 KB per second, 150 ms round trip), then asks an API that takes 100 ms of server work. Not counting the time to run the code, roughly how late is the first product?

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

At least about 2.55 seconds. The HTML costs one round trip (0.15 s), the bundle costs one round trip plus 400 / 200 = 2 seconds (2.15 s), and the API costs one round trip plus 100 ms (0.25 s). Running the code and drawing the list come on top of that.

</details>

2. In the lab, the server-rendered list appeared at 264 ms but its buttons responded only from 1,296 ms. Why did server rendering not make the buttons work sooner?

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

The buttons need the same JavaScript, and server rendering does not make it smaller. The HTML arrives early, but the 174 KB library and the app code still download and run before the click handler is attached. Server rendering moves the first content earlier; it does not move the moment the page responds.

</details>

3. Basket's account page shows a shopper's past orders and is reachable only after login. Which form fits it, and why?

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

Client-side rendering fits. Search engines never see the page, so the empty shell costs nothing in search traffic. The data is different for every shopper, so there is nothing to share between requests, and rendering in the browser saves a server render for each visit.

</details>

4. A teammate says, "We use Next.js, so we are a single-page app, which means client-side rendering." What is wrong with that sentence?

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

Single-page app describes navigation, not where the first page is rendered. Next.js usually renders the first page on the server and then handles later navigations in the browser like a single-page app. An app can be server rendered on the first load and a single-page app afterwards.

</details>

5. Suppose a server render takes 20 ms of CPU and the busiest hour brings 900 product page views per second. How many CPU cores does rendering alone keep busy?

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

About 18 cores. 900 renders per second times 20 ms is 18,000 ms of CPU each second, which is 18 seconds of work every second. That is the cost that serving one static file to everyone avoids.

</details>

Reading Progress

0%


Vote for new content

On This Page

Two Places to Build the Page

The Order of Requests

Measured on One Machine

What Each Form Costs

Single-Page and Multi-Page Apps

Using This in an Interview

Key Takeaways

Practice Questions