System Design Part 1 Measurement basics

Napkin Math for Capacity Estimates: Latency Numbers and Simple Arithmetic for Machine Counts and Storage

Bill.Lin 11 min read

System Design series Part 1-3 | Previous: Latency, Throughput, and p99 | Next: Vertical Scaling, Horizontal Scaling, and Stateless Services | Series map

Read first: Part 1-1 The Journey of an HTTP Request, Part 1-2 Latency, Throughput, and p99.

Scenario and problem

At the kickoff meeting for a new project, the PM says, “We expect 2 million daily active users a year after launch.” Your manager turns to you and asks:

  • How many application servers do we need?
  • How big will the database be in three years? Do we need sharding?
  • Where do the product images go?

There is no load test and no real data, but you have to give answers in the meeting that are correct to an order of magnitude.

What 2 million daily users need1 / 4
What 2 million daily users need2M usersApp serversMySQLItem imagesApp serversMySQLItem imagesHow many?3-year size? Sharding?Where do they go?2M usersApp serversPeak 5,787 QPS13 serversApp serversMySQLWrites 276 QPS1.2 TB in 3 yrs, no sharding2M usersItem imagesPeak 2.4 Gbps4.4 TB/yr → object store + CDN
  1. Three questions in the meetingThe PM says daily active users will reach 2 million a year after launch. Your manager asks you: how many application servers? How big will the database be in three years, and does it need sharding? Where do the product images go? There is no load test and no real data.
  2. Traffic: 13 serversAt 50 requests per user per day, 2 million users make 1,157 requests a second on average; multiply by a peak factor of 5 and you get about 5,800 QPS. One server tops out at 800 QPS in a load test, and at a 60% target utilization you need 13, plus extra for redundancy across availability zones.
  3. Database: no shardingPeak writes are only 276 QPS, an easy load for a single MySQL primary. Three years of order data comes to about 1.2 TB (including three copies), which one machine can hold. What you need to start planning is archiving old orders.
  4. Images: object storage plus a CDNImages grow by 4.4 TB a year, and peak bandwidth is 2.4 Gbps. One machine's network card cannot handle that, and the images do not belong in the database or on the application servers' disks either. Put them in object storage with a CDN in front.

The simplest approach and its limits

The simplest approach is “launch first, and add more when it is not enough”. For stateless services that scale horizontally, this actually works: you just add machines.

The problem is that some decisions are hard to reverse: whether to shard the database, which sharding key to pick, whether images go in the database or in object storage. If you find out after launch that you chose wrong, you pay for a data migration.

The goal of an estimate is not precision. It is to answer order-of-magnitude questions like these: is one machine enough? Do you need a distributed system? Being off by 2x does not matter; off by 100x, the architecture is completely different.

Core mechanism

1. Conversions to memorize

Conversion Approximate value
2^10 / 2^20 / 2^30 / 2^40 About a thousand / a million / a billion / a trillion (KB / MB / GB / TB)
Seconds in a day 86,400; for mental math use 10^5 (about 16% off)
Seconds in a month About 2.6 × 10^6
Seconds in a year About 3.15 × 10^7
1 million per day About 12 per second
100 million per day About 1,160 per second
1 Gbps About 125 MB/s (bits and bytes differ by a factor of 8)

2. Latency numbers (orders of magnitude)

Operation Order of magnitude
CPU L1 cache access About 1 ns
Main memory access About 100 ns
NVMe SSD random read of 4 KB About 10–100 µs
Network round trip within a data center (RTT) About 0.1–0.5 ms
Redis GET (same data center, including the network) About 0.2–1 ms
MySQL primary key lookup (data in the buffer pool, including the network) About 0.5–2 ms
Spinning hard disk random seek About 5–10 ms
Taipei ↔ Tokyo RTT About 30–40 ms
Taipei ↔ US West RTT About 130–160 ms
SSD sequential read of 1 GB About 0.3–1 s
Sending 1 GB over a 1 Gbps network About 8–10 s

The numbers in this table change with each hardware generation. What matters is the gaps between orders of magnitude:

  • Memory is 100–1,000 times faster than an SSD, which is why caches exist
  • A network round trip within a data center is thousands of times slower than a memory read. If a request has an N+1 problem and calls Redis 100 times in a row, the network alone costs 20–100 ms
  • A cross-continent RTT is hundreds of times longer than one within a data center; you already saw its impact in Part 1-1

3. Estimation formulas

flowchart LR
    DAU[Daily active users] --> AVG[Average QPS]
    AVG --> PEAK[Peak QPS]
    PEAK --> INST[App server count]
    PEAK --> WRITE[Peak write QPS]
    WRITE --> DB[Split the database?]
    DAU --> DATA[New data per day]
    DATA --> STORE[Storage over the years]
    STORE --> DB
    STORE --> OBJ[Object storage and a CDN?]
Item Formula
Average QPS Daily active users × requests per user per day ÷ 86,400
Peak QPS Average QPS × peak factor (2–3 for a typical service; 10 or more with on-the-hour events or flash sales)
Write QPS Peak QPS × write ratio
Machines Peak QPS ÷ (single-machine load-test ceiling × target utilization), plus redundancy
Storage New records per day × size per record × days retained × index and overhead factor × number of copies
Bandwidth Peak QPS × average response size

Target utilization is usually 60–70%; the queueing model in the previous post explains why.

Comparing options: estimates, load tests, and working back from monitoring

Method When to use it Accuracy Cost
Napkin math Early in a project, when choosing an architecture, in interviews Order of magnitude A few minutes
Load testing Before launch and after a major redesign, to get the key parameter “single-machine ceiling” Medium to high, depends on the test scenarios A few days
Working back from monitoring data Already live, for annual capacity planning High Needs accumulated historical data

The three do not replace one another: the assumptions napkin math relies on (single-machine ceiling, peak factor, read/write ratio) have to be calibrated with load tests and monitoring data, and the calibrated numbers then feed the next estimate.

In Java: a complete estimation example

Assume a mid-sized e-commerce site (the numbers are made up):

  • 2 million daily active users, 50 API requests per user per day, a peak factor of 5, and a read/write ratio of 20:1
  • One Spring Boot instance tops out at 800 QPS in a load test (with p99 < 200 ms), and the target utilization is 60%
  • 5% of users place orders each day, 1.2 orders on average; each order (header plus line items) is about 2 KB, ×1.5 for indexes and overhead, 3 copies (1 primary, 2 replicas), kept for 3 years
  • 5,000 new products are listed each day, 8 images each, 300 KB per image; at peak there are 3,000 thumbnail requests per second, 100 KB each

Keep every assumption at the top of the program, so when real data arrives you only change the numbers and run it again:

java
public class CapacityEstimate {

    static final double SECONDS_PER_DAY = 86_400;

    public static void main(String[] args) {
        // ---- Assumptions: all in one place, calibrated later with load tests and monitoring ----
        double dau = 2_000_000;
        double requestsPerUserPerDay = 50;
        double peakFactor = 5;
        double writeRatio = 1.0 / 21;         // read:write = 20:1
        double maxQpsPerInstance = 800;       // load-test ceiling per instance (with p99 < 200 ms)
        double targetUtilization = 0.6;

        double orderingUserRatio = 0.05;      // 5% of daily active users place an order
        double ordersPerOrderingUser = 1.2;
        double bytesPerOrder = 2_000;         // header + line items, indexes not included
        double indexOverhead = 1.5;
        double replicas = 3;                  // 1 primary, 2 replicas
        double retentionYears = 3;

        double newProductsPerDay = 5_000;
        double imagesPerProduct = 8;
        double bytesPerImage = 300_000;
        double peakImageRequestsPerSec = 3_000;
        double bytesPerThumbnail = 100_000;

        // ---- Traffic and instance count ----
        double avgQps = dau * requestsPerUserPerDay / SECONDS_PER_DAY;
        double peakQps = avgQps * peakFactor;
        double peakWriteQps = peakQps * writeRatio;
        double instances = Math.ceil(peakQps / (maxQpsPerInstance * targetUtilization));

        // ---- Order storage ----
        double ordersPerDay = dau * orderingUserRatio * ordersPerOrderingUser;
        double orderBytesPerYear = ordersPerDay * bytesPerOrder * 365;
        double orderBytesTotal = orderBytesPerYear * retentionYears * indexOverhead * replicas;

        // ---- Images ----
        double imageBytesPerYear = newProductsPerDay * imagesPerProduct * bytesPerImage * 365;
        double peakImageBitsPerSec = peakImageRequestsPerSec * bytesPerThumbnail * 8;

        System.out.printf("Average QPS           : %,.0f%n", avgQps);
        System.out.printf("Peak QPS              : %,.0f%n", peakQps);
        System.out.printf("Peak write QPS        : %,.0f%n", peakWriteQps);
        System.out.printf("App servers           : %.0f%n", instances);
        System.out.printf("Orders per day        : %,.0f%n", ordersPerDay);
        System.out.printf("Raw order data / year : %s%n", human(orderBytesPerYear));
        System.out.printf("Order disk in 3 years : %s%n", human(orderBytesTotal));
        System.out.printf("Images / year         : %s%n", human(imageBytesPerYear));
        System.out.printf("Peak image bandwidth  : %.1f Gbps%n", peakImageBitsPerSec / 1e9);
    }

    // Estimates always use decimal units (1 KB = 1,000 bytes)
    static String human(double bytes) {
        String[] units = {"B", "KB", "MB", "GB", "TB", "PB"};
        int i = 0;
        while (bytes >= 1_000 && i < units.length - 1) {
            bytes /= 1_000;
            i++;
        }
        return String.format("%.1f %s", bytes, units[i]);
    }
}

Output (java CapacityEstimate.java):

text
Average QPS           : 1,157
Peak QPS              : 5,787
Peak write QPS        : 276
App servers           : 13
Orders per day        : 120,000
Raw order data / year : 87.6 GB
Order disk in 3 years : 1.2 TB
Images / year         : 4.4 TB
Peak image bandwidth  : 2.4 Gbps

Reading the results: how numbers become architecture decisions

  • 13 application servers: a stateless service scales horizontally without trouble. What needs care is how you count redundancy: deployed across 2 availability zones (AZs), if you must still handle the peak when one AZ fails, each AZ has to carry all 13 servers’ worth on its own, 26 in total; with 3 AZs, losing one still leaves two, so each AZ needs only 7 (13 ÷ 2, rounded up), 21 in total. One more AZ actually costs less
  • 276 peak write QPS: an easy load for a single MySQL primary, so no sharding is needed. Replicas and caches can share the reads
  • 1.2 TB of orders over 3 years (including 3 copies, about 400 GB per copy): a single MySQL machine can hold it, but start planning an archiving strategy for old orders so the hot table does not grow forever
  • 4.4 TB of images per year, 2.4 Gbps at peak: they must go into object storage (S3, for example) with a CDN in front. Neither the database nor the application servers’ disks make sense, and one machine’s network card cannot handle that bandwidth either

Conclusion: at this scale, “stateless application + MySQL primary and replicas + Redis + object storage + CDN” is enough, and you do not need sharding or microservices from day one. That is the value of an estimate: it tells you which complexity you do not have to pay for yet.

Pitfalls and gotchas

  • Planning machines from average QPS: you forget the peak factor, and the service gets overwhelmed on the first night after launch
  • Unit mistakes: Mbps and MB/s differ by a factor of 8; KB and KiB differ by 2.4% (negligible in an estimate, unlike bits and bytes)
  • Forgetting replicas, indexes, binlogs, and backups: actual disk usage is often several times the raw data
  • Guessing the single-machine ceiling: a number like “one Spring Boot instance handles about 1,000 QPS” can be off by ten times or more, depending on what each request does. Always load test this parameter
  • Computing bandwidth from the average response size only: a few large files (images, exported reports) often take most of the bandwidth, so estimate them separately
  • Being too precise: two decimal places in an estimate mean nothing; one significant figure is enough. What matters is the order of magnitude and whether the assumptions are reasonable
  • Estimating only the first year: data accumulates and traffic grows, so estimate at least two points in time, 1 year and 3 years
  • Forgetting internal traffic amplification: one request at the entry point may hit Redis 3 times, the database twice, and another service once, so downstream QPS is several times the entry QPS

When to use it, and when not to

  • Use it: before choosing an architecture (sharding or not, a cache or not, object storage or not), when drafting a cloud budget, when assessing a new feature’s impact on the existing system, and in system design interviews
  • Do not rely on it alone: once you have monitoring data, the real data wins; and do not set parameters such as connection pool size straight from an estimate, always verify them with a load test
  • When to re-evaluate: real traffic differs from the estimate by 3 times or more, a new feature changes the read/write ratio (for example adding real-time notifications), or the business model changes (for example starting on-the-hour flash sales, which moves the peak factor from 5 to 50)

References

Comments