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.

Level: IntermediateWatch: 13 steps · ~80 secondsA calculator for a dayRecall: 3 questions · 8 cardsCompare 4 latenciesTypeScript · Python

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.

4Compare them all
The ladder of latencyrough numbers
L1 cache read1 nsRAM read, random spot100 ns ×1001 ns1 µs1 ms1 slog scale: ×1,000 a tick
RAM read · about 100 nsA cache read takes about 1 ns, a random spot in RAM about 100 ns: a hundred times longer. Log scale, ×1,000 a tick
Step 1 / 13 · step by step
100 nsthis rung, typical
×100the rung above
100×a cache read

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.

Step 4 · Every option at once, on one clock

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.

0.0 s (simulated)
Answered from a cache50 msinside now 1 · average 0.2

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

RoughlyIf 1 ns were 1 s
L1 cache readabout 1 ns1 second
RAM read, random spotabout 100 ns1.7 minutes (at 100 ns)
SSD (NVMe) random 4 KB read10–100 µs14 hours (at 50 µs)
Round trip in one datacenter0.1–0.5 ms5.8 days (at 500 µs)
Hard disk seek5–10 ms3.8 months (at 10 ms)
Round trip Bangkok ↔ Singaporeabout 30 ms11 months (at 30 ms)
Round trip Bangkok ↔ US West Coast150–200 ms5.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

ladder.ts
// 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}`);
Run: 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 µs

On 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.

Read: latency vs throughput

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.

Table: the numbers worth remembering

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.

Read: the napkin

Next lesson

Scaling up vs scaling outLevel: Basic

With rough req/s numbers in hand, see when one bigger machine stops being enough.