Grokking the Modern Frontend System Design Interview
Vote

0% completed

​

Frontend System Design vs Backend System Design

Two Ways to Design the Same Product

Where 3.7 Seconds Go

What the Backend Design Owns

What the Frontend Design Owns

Running on Computers You Do Not Choose

Shipping a New Version

Where the Boundary Sits

The Differences in One Table

Using This in an Interview

Key Takeaways

Practice Questions

Frontend System Design vs Backend System Design
Frontend System Design vs Backend System Design

Basket is the web app of an online grocery store, and its product list page asks the server for 30 products. The server finds them and answers in 80 milliseconds. On a mid-range phone with a slow mobile connection, the shopper still waits about 3.7 seconds before the first product appears.

The server's work is about 2 percent of that wait. The other 98 percent is spent opening a connection, downloading code, running that code in the browser, and drawing the page. Adding servers does not shorten any of it.

That 98 percent is the subject of frontend system design. This page explains what a frontend design owns that a backend design does not, and where the boundary between the two sits.

Two Ways to Design the Same Product

There are two ways to look at Basket: from the servers that store and serve its data (backend system design), or from the application in the shopper's browser that turns the data into a usable page (frontend system design).

Frontend system design is the design of the part of a product that runs in the browser: how the page is delivered, how it gets its data, how it responds to the user, and how it stays fast and safe.

The two designs meet at the API, the set of requests the browser is allowed to send and the responses the server promises to return. A backend design decides what happens behind that API. A frontend design decides what happens in front of it, and it usually treats the server as a system that already works.

Where 3.7 Seconds Go

The numbers in the opening use the slow mobile profile that Google's Lighthouse tool applies when it tests a page. A round trip is the time for one message to reach the server and for the reply to come back, and in this profile it is 150 ms. Data downloads at about 1.6 megabits per second, which is about 200 KB per second.

Here is what happens between the tap on a link and the first product on screen. The JavaScript size and the time to run it are reasonable values for a page of this kind, not measurements.

StepTimeWhy it takes this long
Open a connection450 msThree round trips: look up the address, connect, agree on encryption
Download the HTML250 msOne round trip plus 20 KB
Download the JavaScript2,150 msOne round trip plus 400 KB of compressed code
Parse and run the JavaScript400 msThe phone's processor turns the code into a working page
Ask the API for 30 products380 msOne round trip, 80 ms of server work, 30 KB of data
Draw the product list70 msThe browser builds the rows and paints them
Total3,700 ms
Where 3.7 seconds go on one page
Where 3.7 seconds go on one page

Look at the size of the JavaScript download: one file is more than half of the wait. Notice also that the request for products starts only after the code has downloaded and run, because in this design the code is what sends the request. Neither fact is visible from the server, and both are design decisions made in the browser.

A backend design can make the 80 ms shorter, but it cannot remove a 2-second download or move a request that waits on the code.

What the Backend Design Owns

A backend design answers questions about volume and about the stored data:

  • Capacity. How many servers handle the busiest hour, and how requests are spread across them.
  • The stored data. Which database holds the products, orders and carts, and how that data is split when one machine is not enough.
  • Availability. What keeps the store answering when a machine or a data center fails.
  • Agreement between copies. How quickly every copy of the data reflects a change, such as a price update.

Every item on that list concerns thousands of users at once. None of them concerns what a single shopper sees on a single screen.

What the Frontend Design Owns

The frontend design starts from that single shopper. It decides five things that a backend design does not decide for the screen.

  • The API the screen needs. The frontend design asks for the requests that suit the screen: one request or five, 30 products per page or all 4,000, and a live connection for order tracking or a check every 30 seconds.
  • Data and state. State is the data the page holds while it is open: the product list, the cart, the text typed into the search box. The design decides what is kept, for how long, and what the page shows when the shopper presses the back button or opens a second tab.
  • Delivery and rendering. Rendering means producing the page structure that the browser turns into pixels, either as HTML or as elements that JavaScript creates. If the browser renders the page from JavaScript, nothing useful appears until the code arrives and runs, which is the 2,550 ms of downloading and running code in the table, and the product request starts only after that. If the server sends finished HTML, the products can appear much earlier, and search engines can read them.
  • The main thread. The main thread is the single line of work on which the browser runs the page's JavaScript, works out the layout, and handles every click and key press. A long task is any piece of work that holds the main thread for more than 50 ms. A tap on "Add to cart" that arrives during a 300 ms task waits until the task ends, so the shopper sees nothing happen.
  • Qualities every user notices. The page must work with a keyboard and a screen reader (accessibility), in several languages and currencies (internationalization), and without letting another site's script read the shopper's data (security in the browser).
What each design owns
What each design owns

Look at where the network trip lands: on the API, which is the only part both designs touch. Everything in the left panel is decided inside the browser, which is why a frontend design is a different subject and not a smaller backend design.

Running on Computers You Do Not Choose

A backend team picks its machines. It decides the processor, the memory and the network, and it can add more of each.

A frontend runs on whatever the shopper owns. That is a phone whose processor is several times slower than a laptop's, a screen that is 360 pixels or 2,560 pixels wide, a browser version that is several months old, and extensions that change the page. Lighthouse's mobile profile makes the processor run four times slower for exactly this reason.

The design therefore states its target device and network in numbers, such as a mid-range phone on the slow profile, and it keeps the page usable on both ends of that range.

Shipping a New Version

A new server version replaces the old one for every request within minutes. A new frontend version reaches every new page load within minutes too, with no app store review, which makes the web easier to release than a mobile app.

There is one difference that a backend design never faces. A shopper who opened Basket an hour before the release still has the old JavaScript running in that tab, and the tab may stay open for days. When that old page asks for a code file that the new release no longer serves, the request fails and the button the shopper pressed does nothing.

A frontend design handles this in two ways. It keeps the previous release's files available for a period after each release, and it keeps the API accepting requests in the shape the previous version sends.

Where the Boundary Sits

A frontend design question arrives in one of three sizes. Scope means how much of the system the question asks you to design.

ScopeWhat you designExample
One componentIts inputs, events, states and keyboard behaviorA search box with suggestions
One application, API givenRendering, state, data fetching, performanceBasket's product list and cart
Application plus its APIAll of the above, plus the requests and response shapesBasket's live order tracking

The third scope sometimes includes a backend for frontend, a small server owned by the frontend team that combines several backend services into the responses one kind of client, such as the web app, needs. Designing the databases and services behind it is still outside the frontend design.

The Differences in One Table

Backend system designFrontend system design
The main questionMany users at onceOne user on one screen
Where the time goesServer work and database queriesConnections, downloads, running code, drawing
The hardwareChosen by the teamOwned by the user, often slow
The limit that mattersServers, storage, network capacityOne main thread, the download size, about 16 ms to draw each frame
A failed requestA log line and a retrySomething the user sees on the screen
A new releaseOld version gone in minutesOld version can keep running in open tabs
Typical partsLoad balancers, caches, databases, queuesComponents, state, the data layer, the rendering method

Using This in an Interview

State the boundary in your first minute: "I will treat the product service as given, spend most of the time on rendering, data flow and the main thread, and propose the API the screen needs." That sentence tells the interviewer where your design starts.

Two follow-up questions are likely early on:

  • "What does the shopper see while the products are loading, and what if the request fails?"
  • "Which part of the 3.7 seconds would you remove first, and how?"

Key Takeaways

  • Backend system design is about many users and the stored data. Frontend system design is about one user, one screen, and the code that runs in the browser.
  • On a slow phone most of the wait happens in the browser, through connections, downloads, code and drawing, so faster servers rarely fix a slow page.
  • The frontend owns delivery and rendering, the main thread, data and state, the API the screen needs, and the qualities every user notices: accessibility, internationalization and browser security.
  • A frontend runs on hardware the team does not choose, so the design names a target device and network in numbers.
  • A web release reaches new page loads in minutes, but old tabs keep running old code, so the previous release's files and API shapes must keep working for a time.

A frontend design is therefore the other half of the same product, built around one person, one browser, and one main thread. The next lesson, The Parts of a Web Client, opens Basket's frontend and names the parts that do this work.

Practice Questions

Try each question first, then open the answer.

1. A page downloads 600 KB of compressed JavaScript on the slow profile used in this lesson (200 KB per second, 150 ms round trip). About how long does that one download take, and could a faster API fix it?

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

About 3.15 seconds, and a faster API cannot fix it. The data takes 600 / 200 = 3 seconds, plus one round trip of 150 ms. The time is spent moving code to the browser, so the fix is in the frontend design: send less code, or show the products before all of the code has arrived.

</details>

2. A shopper taps "Add to cart" while the page is running a 400 ms task that sorts the product list. What does the shopper see, and why?

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

Nothing happens for up to 400 ms. The browser handles taps on the main thread, and the main thread is busy with the sort. The tap is handled only when the task ends. Any task over 50 ms counts as a long task, and this one is eight times that limit.

</details>

3. Basket releases a new version at noon. A shopper opened the store at 11 in the morning and, at 1 in the afternoon, opens the cart, which loads a separate code file. The release deleted the old files. What happens, and what should the design include?

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

The request for the old cart code file fails, so the cart does not open. The tab still runs the 11 o'clock version, which asks for a file name the new release removed. The design should keep the previous release's files available for a period after each release. A second, separate rule covers the other half of the problem: the API should keep accepting requests from the previous version.

</details>

4. You are asked to design a search box with suggestions. A teammate starts by choosing the database and the number of search servers. Which scope is the question in, and what should the design cover instead?

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

It is a one-component question, so the servers are outside it. The design should cover the component's inputs, events and states (empty, loading, results, no results, error). It should also say how often the box sends requests while the user types, how it ignores replies that arrive out of order, and how it works with a keyboard and a screen reader.

</details>

5. A backend engineer says, "The page is slow, so let's add a cache in front of the product database." The API already answers in 80 ms out of a 3.7 second wait. How much can that cache save at most, and what is a better first target?

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

At most 80 ms, about 2 percent of the wait. The larger costs are the 2,150 ms JavaScript download and the fact that the product request waits for the code to run. Sending less JavaScript, or sending the first products inside the HTML, saves far more time than any server cache.

</details>

Reading Progress

0%


Vote for new content

On This Page

Two Ways to Design the Same Product

Where 3.7 Seconds Go

What the Backend Design Owns

What the Frontend Design Owns

Running on Computers You Do Not Choose

Shipping a New Version

Where the Boundary Sits

The Differences in One Table

Using This in an Interview

Key Takeaways

Practice Questions