Cheat sheet · Foundations
Napkin math
The problem
Before building a courier-tracking service, size it on a napkin: users to requests per second, requests to bytes per day, bytes to disks, bandwidth and servers. Rough numbers, a few minutes, and the expensive mistakes show up before the first line of code.
What you learn
- Turning “how many users” into requests per second, average and peak
- Estimating storage from one row size, per day and per year
- Spotting the bandwidth hog before it reaches production
- Latency numbers worth remembering, and counting servers with headroom
Diagram as code
The lesson's final design as text: paste it into Jira, GitLab, GitHub, Notion or your docs, where Mermaid renders by itself. There are D2 and PlantUML (Confluence, IntelliJ) versions too.
Mermaid: flowchart
flowchart LR
n_couriers(["Courier app"])
n_api["Tracking API"]
n_db[("Database")]
n_customers(["Customer app"])
n_archive[("Archive")]
n_cdn[/"CDN"\]
n_couriers --> n_api
n_api --> n_db
n_customers --> n_api
n_api -.-> n_customers
n_db --> n_archive
n_customers --> n_cdnMermaid: request sequence
sequenceDiagram
participant n_couriers as Courier app
participant n_api as Tracking API
participant n_customers as Customer app
participant n_db as Database
participant n_archive as Archive
participant n_cdn as CDN
Note over n_couriers,n_api: Start from the users
n_couriers->>n_api: POST /v1/pings HTTP/1.1
n_api-->>n_couriers: HTTP/1.1 204 No Content
Note over n_customers,n_api: Add the readers
n_customers->>n_api: GET /v1/orders/o_42/track HTTP/1.1
n_api-->>n_customers: HTTP/1.1 200 OK
Note over n_api,n_db: How big is one ping?
n_api->>n_db: SELECT avg(pg_column_size(p.*)) AS row_bytes
n_db-->>n_api: row_bytes = 61
Note over n_api,n_db: Keep everything for a year
n_api->>n_db: SELECT pg_size_pretty(
n_db-->>n_api: 6.3 TB
Note over n_db,n_archive: Keep 7 days hot, archive the rest
n_db->>n_archive: PUT /pings/2026-11-10.parquet HTTP/1.1
n_archive-->>n_db: HTTP/1.1 200 OK
Note over n_customers,n_api: The map is the heavy part
n_customers->>n_api: GET /tiles/14/10712/6131.png HTTP/1.1
n_api-->>n_customers: HTTP/1.1 200 OK
Note over n_customers,n_cdn: Serve the tiles from a CDN
n_customers->>n_cdn: GET /tiles/14/10712/6131.png HTTP/1.1
n_cdn-->>n_customers: HTTP/1.1 200 OK
Note over n_couriers,n_api: How many app servers?
n_couriers->>n_api: load test: 1 instance, 4 vCPUD2
direction: right
n_couriers: "Courier app" {
shape: rectangle
}
n_api: "Tracking API" {
shape: rectangle
}
n_db: "Database" {
shape: cylinder
}
n_customers: "Customer app" {
shape: rectangle
}
n_archive: "Archive" {
shape: stored_data
}
n_cdn: "CDN" {
shape: oval
}
n_couriers -> n_api
n_api -> n_db
n_customers -> n_api
n_api -> n_customers {
style.stroke-dash: 3
}
n_db -> n_archive
n_customers -> n_cdnPlantUML: request sequence
@startuml
participant "Courier app" as n_couriers
participant "Tracking API" as n_api
participant "Customer app" as n_customers
database "Database" as n_db
database "Archive" as n_archive
participant "CDN" as n_cdn
note over n_couriers, n_api : Start from the users
n_couriers -> n_api : POST /v1/pings HTTP/1.1
n_api --> n_couriers : HTTP/1.1 204 No Content
note over n_customers, n_api : Add the readers
n_customers -> n_api : GET /v1/orders/o_42/track HTTP/1.1
n_api --> n_customers : HTTP/1.1 200 OK
note over n_api, n_db : How big is one ping?
n_api -> n_db : SELECT avg(pg_column_size(p.*)) AS row_bytes
n_db --> n_api : row_bytes = 61
note over n_api, n_db : Keep everything for a year
n_api -> n_db : SELECT pg_size_pretty(
n_db --> n_api : 6.3 TB
note over n_db, n_archive : Keep 7 days hot, archive the rest
n_db -> n_archive : PUT /pings/2026-11-10.parquet HTTP/1.1
n_archive --> n_db : HTTP/1.1 200 OK
note over n_customers, n_api : The map is the heavy part
n_customers -> n_api : GET /tiles/14/10712/6131.png HTTP/1.1
n_api --> n_customers : HTTP/1.1 200 OK
note over n_customers, n_cdn : Serve the tiles from a CDN
n_customers -> n_cdn : GET /tiles/14/10712/6131.png HTTP/1.1
n_cdn --> n_customers : HTTP/1.1 200 OK
note over n_couriers, n_api : How many app servers?
n_couriers -> n_api : load test: 1 instance, 4 vCPU
@endumlPlantUML: components
@startuml
left to right direction
agent "Courier app" as n_couriers
component "Tracking API" as n_api
database "Database" as n_db
agent "Customer app" as n_customers
storage "Archive" as n_archive
frame "CDN" as n_cdn
n_couriers --> n_api
n_api --> n_db
n_customers --> n_api
n_api ..> n_customers
n_db --> n_archive
n_customers --> n_cdn
@endumlRecap
A napkin estimate is a chain of simple multiplications: users → requests per second → bytes per day → disks, bandwidth and servers.
1) Write each assumption down and plan for the peak, not the average. 2) A day is ~100 000 seconds. 3) Ask how long data must live. 4) Multiply bytes by rate for every endpoint. 5) Keep the latency ratios in your head: memory, SSD, data centre, continent.
Numbers: at the start and at the end
| Metric | Start | End |
|---|---|---|
| Peak requests | 2 000/s | 2 300/s |
| Database size | 17 GB | 120 GB |
| API egress per second | 0.3 MB | 0.3 MB |
| App servers | 4 | 4 |
Steps
- Case Start from the users
20 000 couriers are on shift at the busiest hour.
- Case Add the readers
300 000 orders a day, and each customer opens tracking about 20 times: 6 million reads a day.
- Case How big is one ping?
Do not guess the row size, ask the database.
- Problem Keep everything for a year
17 GB a day × 365 ≈ 6.3 TB, 63 billion rows.
- Fix Keep 7 days hot, archive the rest
The database keeps only the last 7 days: 7 × 17 ≈ 120 GB, which fits in one ordinary server.
- Problem The map is the heavy part
The tracking screen shows a map, and the API serves the map tiles itself.
- Fix Serve the tiles from a CDN
Tiles are identical for every viewer and rarely change, so a CDN can cache them close to the users.
- Case Latency numbers worth remembering
Memory: ~100 ns.
- Case How many app servers?
Measure, do not guess: a load test shows one 4-vCPU instance handles 800 requests per second at 61% CPU with a good p99.
- Recap Recap
A napkin estimate is a chain of simple multiplications: users → requests per second → bytes per day → disks, bandwidth and servers.
Terms
- Napkin math
- Sizing a system with a chain of simple multiplications: users, requests per second, bytes per day, then disks, bandwidth and servers. Write each assumption down and plan for the peak, not the average.