System Design Patterns: From Fundamentals to Real Systems
Vote

0% completed

​

The Five Questions

Numbers Before Boxes

The Checkout Before Any Pattern

The Five Questions in Order

Question 1: How Does Data Flow?

Question 2: Where Is the Data Stored?

Question 3: What Must Be Fast?

Question 4: What Happens When a Part Fails?

Question 5: How Does It Grow?

The First Design on the Whiteboard

What the Questions Do Not Tell You

Check Yourself

The Five Questions of System Design: Data Movement, Storage, Speed, Failure, and Growth, Applied to a URL Shortener (Video)
The Five Questions
The Five Questions

An interviewer says, "Design the checkout for an online store," and hands you a marker. The whiteboard is empty, and you have about 45 minutes. What do you draw first?

Most engineers draw a box labeled "Checkout" and add boxes around it. After ten minutes the board is full, but nobody can say which part matters most or which part will break first.

A better start follows a fixed order, the same procedure for every design. First collect a few numbers. Then ask five questions about the design, always in the same order: flow, storage, speed, failure, and growth. This lesson explains the numbers and the five questions. Each question finds a real problem in the store's checkout and points to the part of the course that solves it.

Numbers Before Boxes

The first habit from Thinking in Patterns is Numbers before boxes. A checkout for 3 orders per second and a checkout for 3,000 look nothing alike, so the right boxes depend on which one you must build. Before you draw, ask the interviewer for five figures, called the requirement numbers.

NumberWhat to askThe store's answer
UsersHow many people use it?A mailing list of 5 million subscribers
<span style="white-space:nowrap">Requests</span> per secondHow many requests arrive each second, normally and at the busiest moment?300 checkout requests per second. A flash sale is planned at 5,000 orders per second for two minutes.
Data sizeHow much data is kept, and for how long?Every order is kept. The next lesson calculates the size of 90 days of order events.
Read-to-write ratioHow many reads arrive for each write?8,000 reads and 300 writes per second, about 27 to 1
Cost of downtimeWhat does one minute of failure cost?About 12,000 orders, or $480,000

The 300 checkout requests per second include the store's 200 orders per second, and the other 100 are cart updates and coupon checks. A ratio of 27 reads to each write (8,000 ÷ 300) says that most of the work is reading, so the design must make reads cheap. The flash sale will send an email to all 5 million subscribers at once, which is why so many orders are expected in two minutes.

The cost of downtime comes from one multiplication. The store takes 200 orders per second, which is 12,000 orders per minute. Each order is about 40, the price of Maya's order 1042. So one minute without checkout costs about 480,000 in sales. That figure tells you how much effort the failure question deserves.

In an interview, asking for these numbers takes two or three minutes. If the interviewer answers "You decide," state a number aloud, like "Let us assume 200 orders per second," and write it in a corner of the board. The interviewer can correct a number you said, but cannot correct one you kept in your head.

The Checkout Before Any Pattern

With the numbers on the board, draw the simplest design that could work. The buyer taps Place order, and checkout calls four services one after another: inventory reserves the item (30 ms), payment charges the card (150 ms), the orders database saves the order (20 ms), and fulfillment schedules shipping (50 ms). The buyer waits for all four, which is 250 ms.

Each of those four services is a dependency of checkout: a part that checkout needs in order to finish its work. The orders database is also the store's only database. Product pages read from it too, so it receives all 8,000 reads and 300 writes per second. One database machine handles about 5,000 queries per second.

This first sketch is not the answer. It is what the five questions check.

The Five Questions in Order

The five questions are a fixed list that you ask about any design, in this order:

  1. How does data flow between the parts?
  2. Where is the data stored, and how many copies exist?
  3. What must be fast, and how is it made fast?
  4. What happens when a part fails?
  5. How does it grow?
Numbers first, then five questions in order
Numbers first, then five questions in order

Read the diagram from the top. The requirement numbers come first, and every question below them uses those numbers. Each row names one question and the modules of this course that teach the patterns for it.

The order matters. You need to know how data flows before you can ask where it is stored. You need to know where data is stored before you can make reading it fast. Failure comes after the design has all its parts, because each part needs its own answer. Growth comes last, because more traffic changes the answers to the first four questions.

Question 1: How Does Data Flow?

Draw an arrow for every message that moves between two parts. Then ask one follow-up question for each arrow: does the caller need the answer before it can continue?

For the store, inventory, payment, and the orders database all pass that test. An order for an item that is out of stock must stop, an unpaid order cannot be confirmed, and a confirmed order must be saved. Fulfillment fails the test. The buyer waits 50 ms of the 250 ms for a shipping plan they never see, and checkout fails if fulfillment is down.

The fulfillment arrow is a real finding. Module 2 teaches the communication patterns that help you choose the right kind of call for each arrow, starting with the Request-Response lesson and the Message Queue lesson.

Question 2: Where Is the Data Stored?

For each kind of data, name the place that keeps it and count the copies. The store keeps products and orders on one database machine, so the count is one copy of everything.

One copy creates two problems. If that machine's disk fails, every order the store has taken can be lost. If the machine is busy, product pages and checkout wait for it together. Module 3 teaches how to keep more copies and how to split data across machines. Module 7 teaches what happens when copies disagree.

Question 3: What Must Be Fast?

This question uses two words that are easy to confuse. Latency is how long one request takes, measured in milliseconds. Throughput is how many requests the system completes each second.

Latency and throughput are different numbers
Latency and throughput are different numbers

The top panel shows latency: one buyer waits 250 ms, and payment takes 150 ms of it. The bottom panel shows throughput: the chain completes at most 400 orders per second, twice the normal 200, but far below the flash sale's 5,000. A design can have good latency and poor throughput, so ask about both.

The limit is 400 and not higher because heavy load slows the payment and database calls. Each order then holds one of checkout's 200 threads for about 500 ms, so the threads finish at most 200 ÷ 0.5 = 400 orders per second.

Next, name what must be fast. The buyer's checkout response must feel instant, and the product page is read on every visit. The product page is the clear finding here, because it sends 8,000 reads per second to a database that handles about 5,000. Keeping a copy of product data in memory is the subject of Module 4. Module 8 covers the front of the system, where requests enter and where content can be served close to the buyer.

Question 4: What Happens When a Part Fails?

The third habit from Thinking in Patterns is Question four wins interviews. Interviewers ask many of their follow-up questions about failure. Any design looks correct while every part works. A failure shows whether you understand the design.

Answer the question with a failure walk: go through every dependency, one at a time, and state three things. Say how it fails, what the user sees, and what the design does about it. Here is the failure walk for the store's four dependencies.

<span style="white-space:nowrap">Dependency</span>How it failsWhat the buyer seesPlanned response
InventoryStops <span style="white-space:nowrap">answering</span>Every order failsWait a short, fixed time, then show "please try again"
PaymentSlows from 150 ms to 2 sA long spinner, then checkout stops for every buyerGive up after a short time, and retry only with an idempotency key, so Maya's $40 is charged once
Orders databaseThe machine stopsNo order can be saved, and product pages fail tooA second copy that can take over (question 2)
<span style="white-space:nowrap">Fulfillment</span>Stops answeringAn error, although the card was chargedTake fulfillment off the buyer's wait (question 1)

Two rows in the last column send you back to earlier questions, which is common. The payment row also shows how patterns interact, as Thinking in Patterns explained. A retry is safe only with an idempotency key.

The cost of downtime gives each row a price. If the orders database is down for ten minutes, the store loses about 120,000 orders. Module 5 teaches the patterns that stop one failure from affecting the whole system, and Module 9 teaches how to notice a failure and change a running system safely.

Question 5: How Does It Grow?

Ask what happens when the numbers become larger, both suddenly and over time. The sudden case for the store is the planned flash sale. At 10:00, the email reaches 5 million subscribers, and 5,000 orders per second are expected for two minutes, which is 600,000 orders. The chain completes 400 per second, so the planned load is 12.5 times what checkout can finish.

The slow case is ordinary growth. With each larger number, you ask questions 1 to 4 again, and the answers change. Module 6 teaches how to add machines and split the load across them, and Module 10 teaches how to process large amounts of data that arrive all the time.

The First Design on the Whiteboard

After five questions, the sketch shows five findings, each in a specific place.

Five questions, five findings
Five questions, five findings

Each badge matches one question. Badge 4 sits on checkout, because every dependency needs a planned response, and badge 5 sits beside the buyer, where the flash sale arrives.

The board now shows where the design needs patterns, although none has been chosen yet. In an interview, say the result aloud:

"Checkout takes 300 requests per second, and a flash sale is planned at 5,000 orders per second against a chain that completes 400. The five questions found five places to work on. Fulfillment should not be on the buyer's wait, and the database is one copy. Product reads exceed what the database handles, and every dependency needs a failure plan. The flash sale needs a way to accept more than the chain can finish, so I will start there."

That summary takes under a minute. When you later choose a pattern for a finding, follow the second habit, Every pattern pays its price aloud, and name its cost in the same sentence.

What the Questions Do Not Tell You

The five questions tell you where to look, but they do not tell you how large a problem is. Is 8,000 reads per second a little too many or far too many? How many of checkout's 200 threads do 300 requests per second use? How many minutes per month is a chain of four services down? Each answer needs a short calculation, and the Back-of-the-Envelope Math lesson teaches the four calculations this course uses.

Check Yourself

Q1. A link shortener turns a long web address into a short code, and anyone who opens the short code is sent to the long address. The requirement numbers are 1,000 redirects per second and 10 new links per second. Every link must work for years, because a link that stops working shows an error on every page that uses it. The first sketch is one server and one database. Ask the five questions, and write one finding for each.

<details> <summary>Show answer</summary>
  1. Flow: the user needs the redirect before the page can open, so it waits. Counting each click for statistics can happen later.
  2. Storage: one database holds every link, so a failed disk loses every link ever made. The design needs more than one copy.
  3. Speed: redirects are 100 reads for each write (1,000 ÷ 10), and every one waits for the database. A copy of popular links in memory would make most redirects fast.
  4. Failure: if the database stops, every redirect fails. The planned response is a second copy that can take over.
  5. Growth: if one link is shared on a very popular site, its traffic can rise far above the average. The design must spread those reads across more machines.
</details>

Q2. Why does the storage question come before the speed question? Use the store's product page in your answer.

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

Making a read fast usually means keeping another copy of the data closer to the reader. That copy is a storage decision, so you first need to know where the data is stored and how many copies exist. At the store, question 2 shows that product data is stored on one database machine. Only then can question 3 ask how to serve 8,000 reads per second from a machine that handles 5,000.

</details>

Q3 (incident). A bad deploy stops fulfillment for 15 minutes. During that time, every buyer who places an order sees an error. However, the payment provider's report shows a charge for each of those orders, and many buyers tried again and were charged twice. Which questions would have found this before it happened, and what does the failure walk say?

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

Question 1 finds it first: the buyer does not need a shipping plan before checkout answers, so fulfillment should not be on the buyer's wait. Question 4 finds it again, because the failure walk row for fulfillment says "an error, although the card was charged." Fulfillment is the last call, so payment had already charged the card and the orders database had saved the order.

At 12,000 orders per minute, 15 minutes affects about 180,000 orders, or about $7.2 million in sales that showed an error. The planned response is to take fulfillment off the buyer's wait, so checkout answers after the order is saved and fulfillment does its work later. The second charges also show why every retry of a payment needs an idempotency key.

</details>

💡 In the interview: On a blank whiteboard, ask for the requirement numbers first, then go through the five questions aloud in order. A strong start sounds like this: "Checkout takes 300 requests per second and depends on four services in a row. Question one: fulfillment does not need to finish before the buyer gets an answer, so I take it off the buyer's wait." Expect a follow-up like "What happens when payment slows down?" Answer with a row of the failure walk: how it fails, what the buyer sees, and the planned response.

Key takeaway: Start every design with the requirement numbers: users, requests per second, data size, read-to-write ratio, and cost of downtime. Then ask the five questions in order: how data flows, where it is stored and how many copies exist, what must be fast, what happens when a part fails, and how it grows. For the store, they found five problems before any pattern was chosen, from fulfillment on the buyer's wait to a flash sale 12.5 times larger than the chain can finish. The questions show where to look, and arithmetic shows how large each problem is.

Reading Progress

0%


Vote for new content

On This Page

Numbers Before Boxes

The Checkout Before Any Pattern

The Five Questions in Order

Question 1: How Does Data Flow?

Question 2: Where Is the Data Stored?

Question 3: What Must Be Fast?

Question 4: What Happens When a Part Fails?

Question 5: How Does It Grow?

The First Design on the Whiteboard

What the Questions Do Not Tell You

Check Yourself