Advanced System Design Fundamentals
Vote

0% completed

​

Back-of-the-Envelope, Revisited

Part 1: The Test Before the Arithmetic

Part 2: Numbers to Memorize

Part 3: Four Estimates That Change a Design

Part 4: Throughput, Storage, and Bandwidth

Part 5: Working Set and Fan-Out

Part 6: A Range, Not a Number

Part 7: Using This in an Interview

Common Mistakes

Key Takeaways

The physicist Enrico Fermi is often credited with questions like "How many piano tuners work in Chicago?" He answered them in about a minute, with round numbers and no reference books. He multiplied a few rough figures, such as the number of households, the share that own a piano, and how often a piano is tuned.

System design uses the same method for a different purpose. Fermi wanted a number close enough to be useful. A designer wants a decision, such as whether the data fits on one machine. This lesson shows which estimates are worth the minutes, how to do them quickly, and when to use a range.

Part 1: The Test Before the Arithmetic

This lesson uses one question throughout.

"Design the message service for a workplace chat app. About 2 million people use it each day."

Messages go to channels. A channel is a shared conversation, with anywhere from two members to an entire company.

A back-of-the-envelope estimate is a rough calculation done in a minute or two, with round numbers and no tools. Its purpose is to find the constraint. A constraint is a limit that forces a particular design, such as data that cannot fit on one machine.

Before computing, ask one question. Would a very different answer lead to a different design? If a result ten times larger or smaller would leave the design unchanged, the arithmetic adds nothing, because the design is the same either way. Spend the minutes elsewhere.

The test does not ask whether the inputs are accurate. A rough number that changes a decision is worth more than an exact number that changes nothing.

The rest of the lesson builds on this test. Part 2 gives the numbers to memorize, Part 3 names the four estimates that often pass the test, and Parts 4 to 6 work them out for the chat app.

Part 2: Numbers to Memorize

Most estimates in the chat app start from a count per day, and many of them need a rate per second. A few constants make that step fast.

A day has 86,400 seconds, which you can round to 100,000. A month has about 2.5 million seconds, and a year has about 30 million. The rounding on the day is off by about 16 percent. That is far less than the uncertainty in any guess about users, so the round number is safe to use.

These constants often shrink impressive numbers. A million requests a day is 10 a second, or about 12 with the exact figure. One modest server handles that easily.

The table below gives rough times to reach data, which vary with hardware.

Where the data isRough time to reach it
Memory on the same machineAbout 100 nanoseconds
A fast local disk (SSD)About 100 microseconds
Another machine in the same data centerAbout half a millisecond, there and back
A machine across an oceanAbout 100 milliseconds, there and back

Two units in the table need a definition:

  • A nanosecond is a billionth of a second.
  • A microsecond is a millionth of a second.

Reading from a fast disk is therefore about a thousand times slower than reading from memory. Most of the last figure is set by the speed of light in fiber, so no amount of engineering removes that part.

Part 3: Four Estimates That Change a Design

The test from Part 1 rules out most possible estimates. Four quantities often pass it, because a large answer to any of them forces a different structure. The diagram below shows the question each one answers.

The four estimates that most often change a design, the question each one answers, and what a large answer forces
The four estimates that most often change a design, the question each one answers, and what a large answer forces

Notice the third column of the diagram: each large answer forces a change in structure, not just more machines of the same kind.

  • Peak write rate decides whether one database machine can take every write.
  • The working set against memory decides whether reads come from memory or from disk. The working set is the part of the data that is read often.
  • Fan-out is the number of places one event has to reach. It decides whether one write becomes thousands.
  • Total storage after five years tells you roughly when the design has to change.

The number of API servers fails the test. Whether it is 4 or 40, the same servers sit behind a load balancer, and the design does not change. Anything you would scale by adding a machine is not worth estimating carefully. It locates a cost, not a constraint.

Part 4: Throughput, Storage, and Bandwidth

Now apply Part 3 to the chat app, starting with the write rate and storage. All numbers below are made-up examples.

Step 1: Messages per day. At about 20 messages per person, that is 40 million a day.

Step 2: Peak write rate. Throughput is the amount of work done per second. Dividing 40 million by 100,000 seconds gives an average of 400 messages a second. Daily traffic often peaks at two to four times its average, over a few busy hours. Taking three times gives about 1,200 writes a second at peak. If one modest database machine handles about 5,000 small writes a second, one machine is enough.

Step 3: Storage. A stored message takes about 500 bytes, including IDs, timestamps, and index entries. That is 20 gigabytes a day, or about 7 terabytes a year. After five years, it is about 35 terabytes, before copies and backups. If one database machine is comfortable up to about 10 terabytes, that limit arrives in about 500 days, or 16 months.

Storage, not the write rate, therefore forces a split. The design can start on one machine, with a plan to split messages by channel or move old ones to cheaper storage.

Step 4: Bandwidth. Bandwidth is the amount of data sent over the network per second. Text sent to about 20 members per message is about 12 megabytes a second, which changes nothing. If one message in 20 carries a 1-megabyte image, peak image traffic is about 1.2 gigabytes a second. Passing that through the chat servers would size them for files, not messages.

For this reason, images go to object storage and are delivered by a CDN:

  • Object storage is a service that keeps files and charges per gigabyte.
  • A content delivery network (CDN) is a set of servers in many places that keeps copies of files near users.

Part 5: Working Set and Fan-Out

Part 4 settled writes and storage. The other two estimates from Part 3, the working set and fan-out, each decide something different about the chat app.

The working set. If people mostly read the last week, the working set is 40 million messages a day, times 7 days, times 500 bytes. That is about 140 gigabytes. One cache machine might hold about 64 gigabytes in memory, so the working set does not fit.

If the working set fits, the cache is one machine. If not, the cache becomes a group of machines that must split the data, survive failures, and rebalance. That is a distributed system of its own, which is a different project from adding a cache.

Fan-out. One simple design writes a copy of each message into every member's inbox. The diagram below compares it with storing each message once, in its channel.

One message to a large channel becomes thousands of writes with a copy per member, and one write with a copy per channel
One message to a large channel becomes thousands of writes with a copy per member, and one write with a copy per channel

With 20 members on average, inbox copies turn 1,200 messages a second into 24,000 writes a second. That is far past one database machine, which Step 2 assumed handles about 5,000 small writes a second. One announcement in a 50,000-member company channel becomes 50,000 writes at once.

Notice the bottom line of each side of the diagram. Storing each message once per channel keeps the write rate at 1,200 a second. Online members receive the message over their open connections.

This estimate did not choose a bigger machine. It chose where messages are stored.

Part 6: A Range, Not a Number

Every estimate so far started from one guess: 20 messages per person. Because it was a guess, a senior estimate gives a range. A confidence interval is a low and a high value that you are fairly sure contain the true number.

Suppose each person sends between 5 and 60 messages a day. The diagram below carries that range through two estimates from Parts 4 and 5, each against the limit that decides it.

Two estimates given as ranges, one that sits wholly below its limit and one that crosses it
Two estimates given as ranges, one that sits wholly below its limit and one that crosses it

Peak writes run from about 300 to 3,600 a second, wholly below the limit of 5,000. Every plausible input leads to the same decision, so one machine is settled for writes.

The working set runs from about 35 to 420 gigabytes, which crosses the 64-gigabyte line. The estimate has found the one input worth measuring: messages per person per day.

Notice the difference between the two bars. A range below its limit settles the decision, and a range that crosses its limit names what to measure next.

Two responses to the crossing range are weak. Taking the midpoint in silence hides that the decision is still open. Designing for the top of the range pays now for a scale that may never come. The strong response names the input to measure, and says what each side of the line would mean.

Part 7: Using This in an Interview

An interview leaves time for fewer estimates than the chat app used. On the 45-minute clock, the numbers get minutes 7 to 10. That allows two or three estimates, not seven, so choose the ones you suspect are near a limit.

"Two million people at about 20 messages each is roughly 1,200 writes a second at peak. One database handles that. The working set could be 35 to 420 gigabytes, so I'd measure messages per person first. Until then, I'll assume about 140 gigabytes and use three cache machines. Under 64 gigabytes, one machine is enough."

Every number in that answer ends in a decision. That shows judgment, one of Module 1's four signals.

Expect two follow-up questions. "What if traffic were ten times higher?" Rerun only the estimates that pass the test: peak writes become 12,000 a second, so writes now force a split. "Why didn't you estimate the servers?" Their number changes the bill, not the design.

Common Mistakes

Mistake 1: Estimating everything. Seven numbers in three minutes leave no time to use them.

Mistake 2: Computing a number and then ignoring it. If a result crosses a limit, change the design out loud, because the number has no value unless it changes a decision.

Mistake 3: Giving false precision. "1,389 writes a second" claims an accuracy the inputs do not have. Round, and give a range when the decision is close.

Key Takeaways

  • An estimate exists to make a decision. Before you compute, ask whether a very different answer would lead to a different design.
  • Four estimates change designs most often. They are peak write rate, the working set against memory, fan-out, and total storage after five years.
  • Anything you would scale by adding a machine is a cost, not a constraint. Its answer changes the bill, not the design.
  • Give a range when an input is a guess. If the range crosses the limit that decides the design, name the input you would measure.
  • In a system design interview, spend minutes 7 to 10 on two or three estimates, and say the decision each one makes.

The next lesson, Capacity Planning, turns a peak estimate into a number of machines, with room for failures and growth.

Reading Progress

0%


Vote for new content

On This Page

Part 1: The Test Before the Arithmetic

Part 2: Numbers to Memorize

Part 3: Four Estimates That Change a Design

Part 4: Throughput, Storage, and Bandwidth

Part 5: Working Set and Fan-Out

Part 6: A Range, Not a Number

Part 7: Using This in an Interview

Common Mistakes

Key Takeaways