0% completed
Back-of-Envelope Math
On This Page
- Why Engineers Estimate
- Numbers to Memorize
- Calculation 1: Rate x Duration = Work in Progress
- Calculation 2: Availability Drops Along a Chain
- Calculation 3: Cache Hit Ratios
- Calculation 4: Sizing
- Example: Sizing the URL Shortener
- Using Estimation in Interviews
- TL;DR
1. Why Engineers Estimate
Back-of-the-envelope math is not about precision. It is about catching large errors quickly. Will one machine handle this load? Is the worst case 600 requests in flight, that is, in progress at once, or 60,000? Does this table fit in memory? A rough answer in thirty seconds changes the design. A precise answer next week arrives too late.
The five questions all need numbers to be useful, and this lesson provides the numbers. Estimation is also a standard part of interviews. "How much storage would that need?" is asked out loud. The interviewer expects you to round confidently and narrate as you go.
Round aggressively. This one rule governs everything below. A day is 86,400 seconds. Call it 100,000. A month is about 2.5 million seconds. Call it 3 million. You are estimating orders of magnitude, that is, powers of ten, not decimals. Clean numbers keep your thinking clear at the whiteboard. When the direction matters, round in the safe direction: overestimate load and underestimate capacity.
2. Numbers to Memorize
Most latency estimates use the following table. Each step down is roughly 10x to 1,000x slower than the one above it:
| Operation | Time |
|---|---|
| Read from memory (RAM) | ~100 nanoseconds |
| Read from a fast SSD | ~100 microseconds |
| Network call inside a datacenter (for example, a cache read) | ~0.5-1 ms |
| A simple database query | ~5-15 ms |
| Disk seek on a spinning disk, or a slow query | ~10-100 ms |
| Network round trip between regions | ~100 ms |
| What users perceive as "instant" | up to ~100-200 ms |
Three facts from this table shape the whole course. A cache hit takes about 1 ms and a database query about 10-15 ms. That gap is the entire reason caching exists. Sequential disk writes are much faster than scattered ones. That fact is the foundation of the write-ahead log lesson. Every round trip between regions costs about 100 ms. That is why designs with many sequential calls stop working when they go multi-region.
For sizes, you need even less. A character is a byte. An ID is about 16-36 bytes. A typical database row is about 1 KB. An image is 100 KB to 1 MB. Each unit is a thousand times the one before it: KB, MB, GB, TB. A million 1 KB rows is a gigabyte. A billion is a terabyte.
3. Calculation 1: Rate x Duration = Work in Progress
How many things are in progress at the same time? Multiply how fast they arrive by how long each one takes:
<center> things in flight = arrival rate x duration </center>For example, say 300 requests arrive per second and each takes 2 seconds to complete. Then 300 x 2 = 600 requests are in progress at any moment. Each one holds a thread or a connection. Say your connection pool, the fixed set of database connections you keep open, has 200 slots. You now know there is a problem. You know it thirty seconds into the design, instead of during an outage.
The same formula works in reverse for queues. Say a queue holds 552,000 messages and consumers drain it, meaning process messages, at 400 per second. The backlog clears in 552,000 / 400 = about 1,400 seconds, or 23 minutes.
This is the most-used calculation in the course. It sizes thread pools, connection pools, bulkhead limits, and queue drain times. It has a formal name, Little's law, which is worth knowing for interviews.
4. Calculation 2: Availability Drops Along a Chain
When a request needs several parts to succeed, the availabilities multiply. The result is always lower than the weakest part:
<center> chain availability = availability of each part, multiplied together </center>Five sequential dependencies at 99.9% each give 0.999^5, which is about 99.5%. There is a simpler shortcut for small failure rates: add the failure rates. Five parts x 0.1% = 0.5% failure. Same answer, no exponents needed.
Percentages are hard to interpret, so translate them into time:
| Availability | Downtime per year | Per month |
|---|---|---|
| 99% | ~3.7 days | ~7 hours |
| 99.9% | ~9 hours | ~43 minutes |
| 99.99% | ~53 minutes | ~4 minutes |
| 99.999% | ~5 minutes | ~26 seconds |
Two design lessons follow directly from this math. First, every additional "nine" costs roughly 10x the engineering effort of the previous one. Second, a chain of dependencies can only lower the total. So the fix is not to make every part perfect. It is making the chain shorter, and removing parts from the must-succeed list. That is the core argument of the graceful degradation lesson, previewed here as arithmetic.
5. Calculation 3: Cache Hit Ratios
For any cache, two numbers follow from the hit ratio h, the share of requests the cache answers:
<center> average latency = h x (fast time) + (1 - h) x (fast time + slow time) </center> <center> load reaching the database = (1 - h) x request rate </center>Run it with real numbers: 12,000 requests per second, 1 ms for a cache hit, 15 ms for a database read.
- At a 99% hit ratio: average latency is about 1.15 ms, and the database sees 1% x 12,000 = 120 queries per second.
- At a 90% hit ratio: average latency is about 2.5 ms, which is still fine. But the database sees 1,200 queries per second. That is ten times more.
💡 This is a trap worth naming in interviews: the difference between a 99% and a 90% hit ratio is not nine percent. It is 10x the database load. What matters is the miss rate, and it went from 1% to 10%. Judge caches by their misses.
This is also why the cache stampede problem is so severe. A stampede is many requests missing the cache at once. When a popular key expires, the hit ratio for that key briefly drops toward zero. The formula above shows what happens to the database.
6. Calculation 4: Sizing
Requirements arrive in per-day numbers, but systems are sized in per-second numbers. The conversion is one division:
<center> per second = per day / 100,000 </center>For example, 10 million orders per day is about 100 orders per second, on average. Then apply the peak rule: real traffic is not flat, so plan for a peak of 3-5x the average. Here, size for 300-500 per second.
Storage is multiplication, using the sizes from Section 2:
<center> 10 million orders/day x 1 KB x 365 days = ~3.7 TB per year </center>That fits on one ordinary database machine. Knowing this in ten seconds prevents a lot of unnecessary sharding, that is, splitting the data across machines.
7. Example: Sizing the URL Shortener
The previous lesson sketched the URL shortener design. Now size it, narrating out loud:
- "Say 100 million redirects per day. Divide by 100,000: that is 1,000 per second on average. Size for about 4,000 at peak."
- "Each mapping is two URLs plus metadata. Call it 1 KB. With 500 million total links, that is 500 GB. It fits on one database with capacity left over. We shard eventually, not today."
- "Reads outnumber writes about 100:1, and popularity is skewed. Caching the most popular 1% means 5 million rows, or about 5 GB of cache memory. At a 99% hit ratio the database sees about 10 redirect misses per second. That is a very small load."
- "In flight: 4,000 per second x 2 ms each = 8 requests in progress at once. This is a small service: one good database, a replica, a cache, and careful failure handling."
That is sixty seconds and five numbers. Every part of the design now has a number behind it. No part was added for load that does not exist.
8. Using Estimation in Interviews
- State assumptions before calculating. "Assuming 1 KB per record and 10 million per day..." Interviewers will correct an assumption you state out loud. They cannot correct one you keep to yourself.
- Round before multiplying, and check the units at the end. Per-second times seconds gives a count. Bytes times a count gives storage. If the units do not work out, the answer is wrong.
- Let the numbers rule out extra components. One of the strongest sentences you can say in an interview is: "At this scale, we do not need that yet." That is the cost-awareness from the first lesson, backed by numbers.
9. TL;DR
| The rule | Round aggressively and estimate orders of magnitude. A day is 100,000 seconds. A million 1 KB rows is a GB. Peak is 3-5x average. |
| The table | Memory ~100 ns, SSD ~100 us, in-datacenter call ~1 ms, DB query ~10 ms, cross-region round trip ~100 ms, "instant" ends at ~100-200 ms. |
| The four calculations | In flight = rate x duration. Chain availability = multiply availabilities (or add failure rates). Cache: average latency and DB load both follow from the hit ratio. Sizing: per day / 100,000 = per second; rows x bytes x retention = storage. |
| The traps | 90% is not close to 99% (it is 10x the misses). Every extra nine costs ~10x. Dependency chains can only lower the total, so shorten them. |
| In interviews | State assumptions, check units, and let arithmetic rule out over-engineering. "We do not need that yet" is a strong answer. |
Reading Progress
0%
On This Page
- Why Engineers Estimate
- Numbers to Memorize
- Calculation 1: Rate x Duration = Work in Progress
- Calculation 2: Availability Drops Along a Chain
- Calculation 3: Cache Hit Ratios
- Calculation 4: Sizing
- Example: Sizing the URL Shortener
- Using Estimation in Interviews
- TL;DR