Lesson 28 / 30

Cheat Sheet: Formulas and Anchors

Recall every core formula and rough anchor number from the course on one page.

By · 15 years in web development

Formulas on one page

Time: 1 day = 86,400 s (about 10^5); 30-day month about 2.6 x 10^6 s; year about 3.15 x 10^7 s. Traffic: avg QPS = DAU x actions per user per day / 86,400; peak QPS = avg x peak factor. Storage: objects per day x size x retention days, then x replication x (1 + index and engine overhead) / max fill. Bandwidth: QPS x bytes per response x 8 for bits. Concurrency: Little's Law L = lambda x W. Servers: ceil(peak / (per-server QPS x target utilisation)), then add redundancy. Queueing: M/M/1 W = S / (1 - rho). Cache: DB reads = reads x (1 - hit ratio). Backlog drain: B / (consume - produce). Fan-out tail: 1 - (1 - p)^N. Growth: (1 + g)^months. Cost: units x rate per unit, summed; unit cost = total / business unit. Units: 1 byte = 8 bits; GiB is about 7% bigger than GB.

The order to estimate in

In most problems, estimate traffic first, then storage, then compute, then cost, because each step uses the previous results.

Four connected stages: traffic, storage, compute, cost.
Figure 10.1: traffic -> storage -> compute -> cost

Latency anchors to remember

Main memory about 100 ns; SSD random read tens to a hundred microseconds; in-data-centre round trip a few hundred microseconds; disk seek several milliseconds; cross-continent round trip on the order of 100 ms or more. Remember the gaps, not exact values.

The whole pipeline in one function

A compact estimator chaining traffic, storage and servers; good practice for writing your own.

import math

def estimate(dau, writes_pp, reads_pp, obj_bytes, years, peak=3,
             per_server_qps=1_000, util=0.6, replicas=3):
    w_qps = dau * writes_pp / 86_400
    r_qps = dau * reads_pp / 86_400
    peak_qps = (w_qps + r_qps) * peak
    storage_tb = dau * writes_pp * obj_bytes * 365 * years * replicas / 1e12
    servers = math.ceil(peak_qps / (per_server_qps * util))
    return round(peak_qps), round(storage_tb, 1), servers

print(estimate(dau=10_000_000, writes_pp=3, reads_pp=60, obj_bytes=2_000, years=5))

Output:

(21875, 328.5, 37)

Quick check: Which formula gives the number of requests in flight?

  • Requests per day / 86,400
  • Arrival rate x average time in system
  • Service time / (1 - utilisation)
  • Total reads x (1 - hit ratio)
Answer

Arrival rate x average time in system — That is Little's Law, L = lambda x W. The others give average QPS, M/M/1 response time and backend read load.