Systemsfor Aravinda
0 / 78 core
Week 1 · Fundamentals

Back-of-the-envelope estimation

2.5 hours · core path

Read first: Latency numbers that matter

In one sitting

QPS is daily actions divided by 86,400, and the 100,000-second shortcut is the same idea a bit low. Storage is object count times size times replicas times retention. Peak is a factor you must say out loud, and Little's law turns that rate and a latency into how many requests are in flight.

A diagram without a rate is a poster. Latency numbers that matter tells you what one round trip costs. This page tells you how many of those round trips you are about to attempt, and how many bytes they leave on disk. You do the division out loud, you name every guess as a guess, and you stop when you know which resource breaks first.

Core concepts

You are after three quantities: requests per second, bytes stored, and bytes per second on the wire. Everything else in an interview estimate is a factor in one of those products. If a factor is a guess, you say so before you multiply.

Requests per second. Count the actions in a day, then divide by the length of a day. A day is 86,400 seconds. The interview shortcut is about 10^5 seconds, one hundred thousand. Use the shortcut to get the order of magnitude, then divide by 86,400 when the decision is close.

A million requests a day is not a million per second. It is 1,000,000 / 86,400, about 11.6 per second. The shortcut says 1,000,000 / 100,000 = 10. Same order, a bit low. The true rate is 100,000 / 86,400 = 1.16 times the shortcut, about 16 percent higher. The other direction uses the other base: the shortcut is 86,400 / 100,000 = 0.864 of the exact rate, about 14 percent low. Same gap. Do not call both of them 16 percent. That gap is the whole difference between 86,400 and a round 100,000. It will not change a design that was off by a factor of ten. It will change a design that was hoping to fit on one box with a 20 percent margin.

The teaching scale is the same division with more users. One hundred million daily active users, twenty actions each, is two billion actions a day. Divided by 10^5 that is about 20,000 actions per second average. Divided by 86,400 it is about 23,148. Say which divisor you used.

Peak is an assumption. Average is what the day contains. Peak is the hour people actually show up. A peak of 2x to 5x the average is a planning range, not a measurement. You pick a number inside it and you say you picked it. A batch job that runs at noon can be 20x for ten minutes and idle otherwise; that is a different assumption, and you would say that too. Sizing the fleet to the average means the site falls over at the busy hour while the daily total still matches your spreadsheet.

Storage is a product. Stored bytes equal objects times size times replicas times retention. Drop one factor and the estimate is silently wrong by that factor. Three replicas kept for 30 days is 90 times one day of ingest: 3 * 30 = 90. A year of retention is about twelve times a 30-day window, because 365 / 30 is about 12. The teaching shortcuts for time are a year at about 3 * 10^7 seconds and a 30-day month at about 2.6 * 10^6 seconds. 365 * 86,400 = 31,536,000, so the year shortcut is about 5 percent short of a calendar year. Fine for a sketch. Use the calendar product when you are buying disks.

Replicas are not a performance tuning. They are the copy count Replication uses so one dead disk is not the only copy. Three is a common interview assumption. Say it. One replica is a different product: cheaper, and a single failure loses the object.

Bandwidth. Bytes per day divided by 86,400 is bytes per second. Times 8 is bits per second. Write bandwidth and read bandwidth are different numbers. If each object is viewed many times, reads dominate. Replication multiplies the bytes written inside the cluster: the client sends the object once, and the cluster writes it once per replica. A cache in front of the origin cuts the bytes the origin sends out. Cost-aware design is that statement without a dollar figure. Do not invent a price. Do not invent a company’s traffic. Your number is an assumption until a metric replaces it.

Dean’s teaching row is a useful unit for the small end of the same idea. Sending 1 KB over a 1 Gbps link takes about 10 microseconds. That is serialization of a header, not the pipe you fill with photos. When the estimate comes out at several times 1 Gbps, you have left the “one teaching link” world. Quote the multiple and stop. You do not need a vendor NIC for that conclusion.

Little’s law, once. Average concurrency equals arrival rate times average time in the system. If 1,000 requests per second each take 50 milliseconds, 1,000 * 0.05 = 50 requests are in flight. If latency doubles and the arrival rate holds, concurrency doubles. That is the whole law. It is how a slow dependency becomes a thread pile, and it is why a timeout belongs in the estimate: the timeout is the longest time you are willing to let one call contribute. QPS alone does not tell you how many threads you need. QPS times latency does.

A worked example

Photo sharing, sketched so the three quantities disagree with each other on purpose. Every line below that is not a division is an assumption.

  • 10 million daily active users. An interview sketch, not a measured product.
  • Each user uploads 2 photos a day and views 40.
  • Mean object size is 2 MB, and a megabyte here is 10^6 bytes. A terabyte is 10^12 bytes. The tail of “photos” that are short videos will be worse; this mean is for capacity, and you would measure the large end before buying disks.
  • 3 replicas.
  • Keep the original for a year. Also compute 30 days, because that is the factor of 90 people remember.
  • Peak is 4 times the average. That sits inside the 2x to 5x band. A photo app bunches up in the evening; 4x is a guess you would replace with a week of traffic.
  • First pass: every view is served from the origin. Second pass: 90 percent of views hit a cache close to the user. The 90 percent is an assumption you have not measured.
uploads per day           10,000,000 * 2 = 20,000,000
average upload QPS        20,000,000 / 86,400 = 231.5
shortcut                  20,000,000 / 100,000 = 200
views per day             10,000,000 * 40 = 400,000,000
average view QPS          400,000,000 / 86,400 = 4,629.6
shortcut                  400,000,000 / 100,000 = 4,000
peak factor               4
peak upload QPS           4 * 231.5 = 926
peak view QPS             4 * 4,629.6 = 18,518

one day of originals      20,000,000 * 2 MB = 40 TB
three replicas            120 TB per day of disk
30 days                   120 * 30 = 3,600 TB
check                     3 * 30 = 90 days of ingest, 40 TB * 90 = 3,600 TB
365 days                  120 * 365 = 43,800 TB

ingest on the wire        40 TB / 86,400 s = 463 MB/s = 3.7 Gbps
peak ingest               4 * 3.7 = 14.8 Gbps
views, no cache           20 times ingest, because 40 views / 2 uploads
average view bandwidth    74 Gbps
peak view bandwidth       296 Gbps
after a 90 percent hit    origin views are 7.4 Gbps average, 4 * 7.4 = 29.6 Gbps

The shortcut said 200 uploads per second and the day length says 231. You would not pick a different architecture over that gap. You would pick a different architecture over 74 Gbps of origin reads. In units of Dean’s 1 Gbps teaching link, the uncached view path is on the order of 74 such links average and about 300 at the assumed peak. One origin machine does not have that path. That is the bottleneck, and it is why the bytes want to live near the viewer. DNS and CDNs is the mechanism. Object storage is where the 43,800 TB of originals actually sit: immutable blobs, not a filesystem you rewrite in place.

Cross-check the month shortcut so you trust the division. A 30-day month is about 2.6 million seconds. 231.5 * 2,600,000 is about 602 million uploads. 20,000,000 * 30 is 600 million. Same count, two routes. If those two ever disagree, you divided wrong.

Now Little’s law on the origin, after the cache assumption. Sunny-day origin view rate is a tenth of 4,629.6, about 463 per second. Assume an origin view occupies a worker for 20 milliseconds: longer than a 0.5 millisecond same-datacenter round trip, shorter than a 10 millisecond disk seek, so you are assuming the bytes come from memory on that machine. Concurrency is 463 * 0.020 = 9. A thread pool of a few dozen is bored.

Break the assumptions one at a time, because they multiply.

  • Lose the cache. Origin view rate goes back to 4,630 per second. At 20 milliseconds that is about 93 in flight.
  • Apply the 4x peak on top. About 370 in flight.
  • The disks are busy, so latency doubles to 40 milliseconds. Little’s law says concurrency doubles again if the rate holds. About 740 in flight.

From 9 to about 740 is the 10x from the lost cache, times 4x from the peak guess, times 2x from the slower response. A pool sized for the sunny day rejects the rest or queues without bound. The bound is a Load shedding decision. The estimate’s job was to show that the sunny-day number was not the capacity number.

flowchart LR
  users[Daily users]
  actions[Actions per day]
  qps[Average rate]
  peak[Peak rate]
  disk[Stored bytes]
  wire[Bandwidth]
  users --> actions
  actions --> qps
  qps --> peak
  actions --> disk
  actions --> wire

Same users, chat instead of photos. Keep the 10 million people. Assume 50 messages sent per user per day, 200 bytes each, and that each message is read by 10 people. Those three are assumptions.

messages per day     10,000,000 * 50 = 500,000,000
average send QPS     500,000,000 / 86,400 = 5,787
shortcut             500,000,000 / 100,000 = 5,000
bytes per day        500,000,000 * 200 = 100 GB
three replicas, year 100 GB * 3 * 365 = 109,500 GB
send bandwidth       100 GB / 86,400 s = about 1.2 MB/s
with 10 readers      about 12 MB/s of read bytes
read QPS             5,787 * 10 = 57,870

The formula just changed the bottleneck. Photos broke the pipe. Chat’s pipe is a rounding error next to 74 Gbps, and the year of messages is about 110 TB with three copies, not tens of thousands of terabytes. What is large is the send rate and the fanout. That is a Networking for request paths problem and, later, Design a chat system. The estimate told you which deep dive to offer. That is the point of doing it before you draw twelve boxes.

Tradeoffs

Choice What you gain What you spend
Divide by 100,000 Mental math that stays in your head About 14 percent below the exact rate from 86,400
Size to the average A smaller fleet on paper The busy hour, which you did not model
One replica A third of the disk One failure loses the only copy
30 days instead of a year About 12 times less disk No original older than a month
Assume a cache hit rate Origin bandwidth collapses on the whiteboard A cold cache returns the full number
Convert QPS with Little’s law A thread count that matches the latency You need a latency, and a timeout, not just a rate

Pitfalls

  • Forgetting a factor in the product. Skipping replicas makes the disk look three times larger than the estimate, and the incident looks like “we grew faster than expected.”
  • Treating 2x to 5x as a fact about the product. It is a range you choose. A cron-shaped workload is outside it.
  • Calling a million actions a day a million QPS. You skipped the division by 86,400. The rate is about 12.
  • Mixing the shortcut and the exact division in one sentence as if both were measurements. Pick one and name it.
  • Sizing disk on the thumbnail and then storing the original. The 2 MB assumption has to be the object you keep.
  • Sizing the wire on uploads when each object is viewed 20 times. The read path is the larger term.
  • Quoting concurrency from QPS with no latency. Without Little’s law you have a rate, not a pool size.
  • Using 3 * 10^7 seconds as an exact year when the decision is close. It is about 5 percent short of 365 days.
  • Borrowing a famous company’s QPS. If it is not a published figure you can point at, it is a fiction. Use your assumptions.

In the interview

Spend the first few minutes on users, actions per user, one division for QPS, the four-factor storage product, and a peak you explicitly call a guess. Then stop estimating and draw. The grader wants to see that you can multiply and that you noticed which term dominates. They do not want a spreadsheet.

Skip prices, instance types, and a second feature they did not ask about. If the prompt is photos, do not estimate search.

The question that usually follows is “what happens at ten times the traffic?” Answer with the bottleneck you already computed. In the photo sketch that sentence is: average view rate about 4,600 per second, about 74 Gbps if the origin serves every byte, so the origin cannot be one machine and the next decision is the cache, which buys bandwidth by accepting staleness. In the chat sketch the sentence is the opposite: the bytes are small, the send rate and the fanout are the design.

Early-career interviews want a clear path, a number, a bottleneck, and one honest tradeoff. The tradeoff here is the peak factor or the hit rate. Both are guesses. Saying so is the point.

Check yourself

One hundred million daily users each do 20 actions. What is the average rate, and what do you have to say before you quote a peak?

Two billion actions a day. Divided by 86,400 that is about 23,148 per second. The shortcut 2,000,000,000 / 100,000 is 20,000 per second, which is 0.864 of 23,148, about 14 percent low. The exact rate is about 16 percent higher than 20,000. Same design. A peak of 2x to 5x is an assumption. If you pick 5x, say so: about 116,000 per second on the exact divisor. You do not know the peak until you have traffic.

Two million new objects a day, 100 KB each, three replicas, kept 30 days. How much disk, and which identity checks the product?

Use decimal units: 100 KB is 10^5 bytes. 2,000,000 * 10^5 = 2 * 10^11 bytes, 200 GB a day. Times 3 replicas times 30 days is 18 TB. The check is 3 * 30 = 90 times one day of ingest, and 200 GB * 90 = 18 TB. Miss either the replica count or the retention and you are off by that factor.

Arrival is 2,000 requests per second and each stays 25 milliseconds. How many are in flight, and what happens if latency doubles?

Little’s law: 2,000 * 0.025 = 50 in flight. If latency doubles to 50 milliseconds and the arrival rate holds, concurrency doubles to 100. The law does not say the arrival rate will hold. A retry storm is a second assumption, and it multiplies the 100 again.

The photo sketch serves 40 TB of uploads a day from the origin with no cache in front of the reads. Why is view bandwidth about 20 times ingest?

Each user uploads 2 photos and views 40, and the view is the same 2 MB object. 40 / 2 = 20, so the read path moves 20 times the ingest bytes if nothing is cached: about 74 Gbps average against 3.7 Gbps of uploads. A 90 percent hit rate is a separate assumption that cuts only the read term, down to about 7.4 Gbps, and it disappears the hour the cache is cold.

A teammate divides a million requests a day by 60 and calls it 16,667 QPS. What did they measure?

They divided the daily count by 60 and then called the result QPS. A million requests a day is 1,000,000 / 86,400 = 11.6 per second. Per minute that is 11.6 * 60, about 694, which is also 1,000,000 / 1,440 because a day has 1,440 minutes. 1,000,000 / 60 = 16,667 is about 1,440 times the true QPS. It is not requests per minute, and it is not what the servers see. Name the unit after the division.

Based on: Jeff Dean, Numbers Everyone Should Know; Designing Data-Intensive Applications, Kleppmann and Riccomini; Marc Brooker, AWS Builders' Library; Alex Xu, ByteByteGo.