Napkin math
Before you design a system, size it roughly: how far apart a cache read and a trip across the world are, and how a million users turn into requests a second.
What you'll take away
- Rank cache, RAM, SSD, a datacenter round trip and an ocean crossing by latency
- Tell latency from throughput, and why more servers don't make one request faster
- Use Little's law, rate × latency, to work out how many workers you need
- Turn users a day into requests a second at the peak, then bandwidth and disk
The 1-minute version
Tap a point and the picture below plays that step.
Assumes one request in ten is a write the size of a response. Rough numbers to start thinking with, not a measurement
A system takes 500 requests a second, each lasting 200 ms. On average, how many are in flight at once?
The ladder grows one rung a step on a screen in the city (a log scale, ×1,000 a tick). In the glass pipe, each light is a request inside it. The day is 24 towers, one an hour, with the average and the planned peak across them; gold lights are answers going back out, and the store beside the server grows with what is written. Rough numbers: the ladder's are typical of 2020s hardware and can each be a few times off; the day's are an example to start from. Nothing here is a benchmark.
Compare them all: Latency and throughput
4 requests a second into every tile; only how long each takes differs. See how many are inside at once.
Use it when Things read often and changed rarely: hardly anything waits inside
Skip it when Things that must be fresh every time: think about invalidation (the Caching lesson)
Little's law: on average, in flight = rate × latency. The model's numbers. Press any tile to see it large above, with when it fits.
The numbers worth remembering
| Roughly | If 1 ns were 1 s | |
|---|---|---|
| L1 cache read | about 1 ns | 1 second |
| RAM read, random spot | about 100 ns | 1.7 minutes (at 100 ns) |
| SSD (NVMe) random 4 KB read | 10–100 µs | 14 hours (at 50 µs) |
| Round trip in one datacenter | 0.1–0.5 ms | 5.8 days (at 500 µs) |
| Hard disk seek | 5–10 ms | 3.8 months (at 10 ms) |
| Round trip Bangkok ↔ Singapore | about 30 ms | 11 months (at 30 ms) |
| Round trip Bangkok ↔ US West Coast | 150–200 ms | 5.7 years (at 180 ms) |
Latency vs throughput
Latency is how long one request takes; throughput is how many finish a second. More servers raise throughput, not latency. Little's law ties them together: in flight = rate × latency, which is how many connections or workers you need.
The napkin
Requests a day ÷ 86,400 (call it 100,000) = the average a second. Plan for a peak several times that. Multiply by the response size for bandwidth, by the writes' size for storage. Round everything: you want to know if it is ten servers or a thousand, not the third digit.
In real code
// The ladder on this machine: reading memory in order, jumping around RAM, and a round trip over localhost (through
// Node and the kernel, not a real network). Each rung is far slower than the last: that's what holds on any machine;
// the exact numbers don't.
// Run: node ladder.ts (Node 23.6+; on Node 22: node --experimental-strip-types ladder.ts)
import assert from "node:assert/strict";
import { createServer, connect, type Socket } from "node:net";
const N = 1 << 24; // 16 M items: 128 MB of doubles, 64 MB of ints, far bigger than the CPU's caches
// 1. In order: the CPU sees the pattern and fetches ahead.
const xs = new Float64Array(N).fill(1);
let t = performance.now(), sum = 0;
for (let i = 0; i < N; i++) sum += xs[i];
const inOrder = ((performance.now() - t) * 1e6) / N;
assert.equal(sum, N);
// 2. Jumping around: a random cycle through 64 MB, each step depends on the last, so nothing can be fetched ahead.
const next = new Int32Array(N);
for (let i = 0; i < N; i++) next[i] = i;
let seed = 7;
for (let i = N - 1; i > 0; i--) { seed = (seed * 1103515245 + 12345) % 2147483648; const j = seed % i; [next[i], next[j]] = [next[j], next[i]]; } // Sattolo: one cycle
const hops = 5_000_000;
let at = 0;
t = performance.now();
for (let k = 0; k < hops; k++) at = next[at];
const jumping = ((performance.now() - t) * 1e6) / hops;
assert.ok(at >= 0);
// 3. A round trip: one byte to an echo server on 127.0.0.1 and back, 2,000 times.
const server = createServer((s) => s.on("data", (d) => s.write(d)));
await new Promise<void>((r) => server.listen(0, "127.0.0.1", r));
const sock: Socket = connect((server.address() as { port: number }).port, "127.0.0.1");
await new Promise<void>((r) => sock.once("connect", r));
sock.setNoDelay(true);
const trips = 2_000;
t = performance.now();
for (let k = 0; k < trips; k++) { const back = new Promise((r) => sock.once("data", r)); sock.write("x"); await back; }
const roundTrip = ((performance.now() - t) * 1e6) / trips;
sock.destroy(); server.close();
// Only the order is asserted (any machine, even a busy build server): each rung well above the one before.
assert.ok(jumping > 5 * inOrder, `jumping ${jumping} vs in order ${inOrder}`);
assert.ok(roundTrip > 10 * jumping, `round trip ${roundTrip} vs jumping ${jumping}`);
// Printed coarsely, since timings wobble from run to run: to the nearest ns, the nearest 100 ns, and tens or hundreds of µs.
const trip = roundTrip < 100_000 ? "tens of µs" : "hundreds of µs";
console.log(`in order ~${Math.max(1, Math.round(inOrder))} ns an item · jumping around RAM ~${Math.max(1, Math.round(jumping / 100)) * 100} ns a hop · a localhost round trip: ${trip}`);
node ladder.ts (Node 23.6+ · on Node 22 add --experimental-strip-types)It printed: in order ~1 ns an item · jumping around RAM ~100 ns a hop · a localhost round trip: tens of µsOn the machine that checked this page: reading 16 M numbers in order, jumping around 64 MB of RAM, and a one-byte round trip over localhost through Node. The numbers are that machine's; the ladder (each rung far slower than the last) is what holds. It really runs.
Common questions
What's the difference between latency and throughput?
Latency is how long one request takes; throughput is how many finish a second. More servers raise throughput but don't make one request faster. To cut latency, shorten the path or add a cache.
What is Little's law, and what's it for?
On average, the number inside a system = the rate things arrive × how long each stays. 500 requests a second at 200 ms each means about 100 at once: that's how many connections or workers you need.
Why does one slow backend drag the whole site down?
The rate in stays the same, but each request stays longer, so more pile up inside. 4 a second at 4 seconds each is 16 inside; with a pool of 10, new requests queue up and everything behind them slows too.
How exact do these latency numbers need to be?
Not very: hardware varies by a few times. What matters is the ladder: RAM is about a hundred times slower than cache, an SSD hundreds of times slower than RAM, a trip across an ocean hundreds of times slower than one inside a datacenter.
Why is a far-away server slow even on a fast connection?
Light in fibre goes about 200,000 km a second. Bangkok to the US West Coast is about 13,000 km, so the round trip is over 130 ms in theory, 150–200 ms in practice. Bandwidth doesn't shorten the distance; servers or a CDN near users do.
Why size for the peak instead of the average?
Use isn't spread evenly over a day: evenings, payday or a sale run several times the average. A system sized for the average falls over exactly when the most people are on it.
How do I estimate storage and bandwidth?
Bandwidth = peak requests a second × the response size. Storage = writes a day × the size of each, times a year. Round everything: you want to know if it's ten servers or a thousand, not the third digit.
Next lesson
Scaling up vs scaling outLevel: Basic
With rough req/s numbers in hand, see when one bigger machine stops being enough.