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
A back-of-the-envelope calculation is a quick estimate, simple enough to write on the back of an envelope. It gives a useful rough answer, not a precise forecast. It finds large mistakes early: whether one machine can handle the load, whether 600 or 60,000 requests are active at once, and whether a table fits in memory. A rough answer in thirty seconds can change the design. A precise answer next week is too late for the current decision.
The five questions need numbers, and this lesson teaches four simple calculations that produce them. Estimation is also a standard part of system design interviews. When an interviewer asks how much storage a design requires, state reasonable assumptions, round the values, and explain each step.
Round aggressively. This means rounding much further than usual. A day contains 86,400 seconds, but you can use 100,000 for a quick estimate. A month contains about 2.5 million seconds, but 3 million is easier to calculate with. The goal is to estimate orders of magnitude, meaning powers of ten, not exact decimal values. When the direction matters, overestimate traffic and underestimate capacity, so the design keeps a safety margin.
2. Numbers to Memorize
Latency is the time one operation takes. Most latency estimates use the values in this table. Each row is roughly 10 to 1,000 times slower than the row 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 influence many designs.
First, a cache is fast storage that keeps copies of recent results. A request the cache answers (a cache hit) takes about 1 ms, while a database query takes about 10 to 15 ms. This explains why caching can reduce read latency.
Second, writes scattered across a disk need many seeks (moves to a new position), so sequential writes, which each go to the next position on the disk, are much faster. This supports the write-ahead log pattern, which records each change in a log before applying it.
Third, a round trip is one request and its response. Each one between regions adds about 100 ms, so several in sequence can exceed the application's response-time limit.
Size estimates need only a few reference values. A character uses about one byte, an identifier uses about 16 to 36 bytes, and a typical database row uses about 1 KB. An image commonly uses 100 KB to 1 MB. Each unit is one thousand times the previous unit: KB, MB, GB, and TB. Therefore, one million rows of 1 KB each require about one gigabyte, and one billion such rows require about one terabyte.
3. Calculation 1: Rate x Duration = Work in Progress
The diagram above repeats the main latency values, from 100 ns at the top to 100 ms at the bottom. The first calculation uses durations like these.
An operation that has started but not finished is in flight, or work in progress. The number in flight at one moment is the concurrency. To estimate it, multiply the arrival rate by the time each operation takes:
<center> things in flight = arrival rate x duration </center>Suppose 300 requests arrive each second and each request takes 2 seconds to complete. The service has 300 x 2 = 600 requests in flight at any moment, and each one may hold a thread or connection. A connection pool is a fixed set of open database connections. If the pool has 200 slots, the service does not have enough connections for this load.
The same relationship tells you how long a queue needs to process its backlog, which is the work waiting in it. If a queue contains 552,000 messages and its consumers process 400 messages per second, it needs 552,000 / 400 = about 1,400 seconds, or 23 minutes.
This is the most frequently used calculation in the course. It helps you size thread pools, connection pools, and bulkhead limits, which stop one failing feature from using all shared resources. It also estimates queue processing time. Its formal name is Little's law, a useful term in an interview.
4. Calculation 2: Availability Drops Along a Chain
Availability is the share of time that a part works. When a request depends on several parts, multiply their availability values to calculate the availability of the complete request. The result is always lower than that of the least reliable part:
<center> chain availability = availability of each part, multiplied together </center>Five sequential dependencies with 99.9% availability each give 0.999^5, or about 99.5% for the request. For small failure rates, a simpler shortcut is to add those rates: five parts x 0.1% failure gives about 0.5% total failure.
Percentages are easier to understand when converted into downtime, the time a system is not available:
| 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 conclusions follow. First, each additional nine in an availability target requires roughly ten times the engineering effort of the previous one. A nine is one digit 9 in the target, so 99.9% is three nines.
Second, each required dependency can only reduce total availability. The practical response is not to make every part perfect, but to reduce the number of dependencies a successful result requires. The graceful degradation lesson shows how a system can return a smaller useful result when an optional dependency fails.
5. Calculation 3: Cache Hit Ratios
A cache miss is a request the cache cannot answer, so it must also read the database. The hit ratio h is the share of requests the cache answers. Two values follow from h:
<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>The fast time is the cache check, and the slow time is the database read. A miss takes both.
Consider 12,000 requests per second, with 1 ms for a cache hit and 15 ms for a database read.
- At a 99% hit ratio, average latency is about 1.15 ms and the database receives 1% x 12,000 = 120 queries per second.
- At a 90% hit ratio, average latency is about 2.5 ms, but the database receives 1,200 queries per second, which is ten times more load.
💡 A change from a 99% hit ratio to 90% does not increase database load by nine percent. It increases the miss rate from 1% to 10%, so the database receives ten times as many queries. Evaluate a cache by its misses as well as its hits.
This calculation also explains why a cache stampede can overload a database. A stampede occurs when many requests miss the same cache entry at once. When a popular entry expires, its hit ratio briefly approaches zero, and most of its requests reach the database.
6. Calculation 4: Sizing
Requirements often describe daily traffic, but capacity plans need per-second traffic. Divide the daily value by 100,000 for a quick conversion:
<center> per second = per day / 100,000 </center>For example, 10 million orders per day is about 100 orders per second on average. Traffic is not constant, so plan for a peak (the busiest period) between three and five times the average, or 300 to 500 orders per second here.
Storage is the number of records multiplied by the size of each record and the retention period (how long you keep them):
<center> 10 million orders/day x 1 KB x 365 days = ~3.7 TB per year </center>This amount fits on one ordinary database machine. The estimate therefore prevents unnecessary sharding, which means splitting the rows across several machines.
7. Example: Sizing the URL Shortener
The diagram above collects the four calculations, each with its formula and example. The previous lesson outlined a URL shortener, and these calculations can now estimate its capacity. Explain each assumption as you go:
- "Assume 100 million redirects per day. Dividing by 100,000 gives 1,000 redirects per second on average, so we should support about 4,000 per second at peak."
- "Each mapping contains two URLs and metadata, which we can estimate as 1 KB. With 500 million links, the database stores about 500 GB. One database has enough capacity today, so sharding can wait until the data grows."
- "Reads outnumber writes by about 100 to 1, and a small share of links receives most of the traffic. Caching the most popular 1% stores 5 million rows, or about 5 GB. With a 99% hit ratio, only about 10 redirect queries per second reach the database on average."
- "At peak, 4,000 requests per second x 2 ms per request gives 8 requests active at once. This load requires one capable database, a replica (a copy of it), a cache, and explicit failure handling."
This explanation takes about sixty seconds and ends with five numbers: 4,000 requests per second, 500 GB of data, 5 GB of cache, 10 database queries per second, and 8 active requests. Every major part now has a capacity reason, and no part exists for traffic that has not appeared.
8. Using Estimation in Interviews
- State assumptions before calculating. Say, "Assuming 1 KB per record and 10 million records per day," before you use those values. The interviewer can correct an assumption you state, but cannot correct one you leave unstated.
- Round before multiplying, and check the units at the end. A per-second rate multiplied by seconds produces a count, and bytes multiplied by a record count produce storage. If the units do not match the expected result, the calculation is wrong.
- Use the numbers to reject unnecessary parts. A useful interview statement is, "At this scale, we do not need that yet." This is the cost awareness from Thinking in Patterns, supported by arithmetic.
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, and rows x bytes x retention = storage. |
| The key facts | 90% is not close to 99% (it is 10x the misses). Every extra nine needs ~10x the effort. Dependency chains can only lower the total, so shorten them. |
| In interviews | State assumptions, check units, and use the arithmetic to reject parts you do not need. "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