Skip to lesson

Useful numbers for estimation

ShopLite has become popular, and its owner asks: "If we get 100 times more users, will one server and one disk be enough?" You can answer in minutes with a handful of numbers you know by heart.

Powers of two and data sizes: KB, MB, GB, TB, PB

A back-of-the-envelope estimate is a quick, rough calculation, the kind you could do on the back of an envelope, that shows whether a design is in the right range. It uses a few numbers you know by heart.

Data sizes grow by about 1,000 at each step: 1 KB ≈ 10³ bytes, 1 MB ≈ 10⁶, 1 GB ≈ 10⁹, 1 TB ≈ 10¹², 1 PB ≈ 10¹⁵. Computers really count in powers of two; the code shows how small the gap is.

Example 1 · Powers of two vs powers of ten

PowersDemo.javaJava
public class PowersDemo {    public static void main(String[] args) {        String[] names = {"KB", "MB", "GB", "TB", "PB"};        for (int i = 0; i < names.length; i++) {            int power = 10 * (i + 1);                       // 2^10, 2^20, ...            long binary = 1L << power;            long decimal = (long) Math.pow(1000, i + 1);    // 10^3, 10^6, ...            double diff = (binary - decimal) * 100.0 / decimal;            System.out.printf("%s  2^%d = %,d  vs  10^%d = %,d  (%.1f%% more)%n",                    names[i], power, binary, 3 * (i + 1), decimal, diff);        }    }}
Output
KB  2^10 = 1,024  vs  10^3 = 1,000  (2.4% more)
MB  2^20 = 1,048,576  vs  10^6 = 1,000,000  (4.9% more)
GB  2^30 = 1,073,741,824  vs  10^9 = 1,000,000,000  (7.4% more)
TB  2^40 = 1,099,511,627,776  vs  10^12 = 1,000,000,000,000  (10.0% more)
PB  2^50 = 1,125,899,906,842,624  vs  10^15 = 1,000,000,000,000,000  (12.6% more)

What this code does

  • 1L << power is 2 to that power. Each step up (KB → MB → GB) multiplies by 2¹⁰ = 1,024, which is close to 1,000.
  • The gap grows from 2.4% at KB to about 12.6% at PB. For estimation that is small, so treat 1 KB as 1,000 bytes, 1 GB as 10⁹ bytes and 1 TB as 10¹² bytes.

Binary units are written KiB, MiB and GiB, where 1 KiB is 1,024 bytes. Disk makers and estimates use the decimal units, so a "1 TB" disk shows up as about 931 GiB in an operating system.

Latency numbers: memory, SSD, network, other continents

Latency is how long one operation takes, from asking to getting the result. It is measured in nanoseconds (ns), microseconds (µs) and milliseconds (ms).

Rough values: a memory read takes about 100 ns, a random read from an SSD (solid-state drive) about 100 µs, a round trip inside one datacenter about 0.5 ms, and India to the US about 200 ms. The code prints them on a human scale.

Example 2 · Latency numbers, and the same numbers on a human scale

LatencyNumbersDemo.javaJava
import java.util.LinkedHashMap;import java.util.Map;
public class LatencyNumbersDemo {    public static void main(String[] args) {        Map<String, Double> nanos = new LinkedHashMap<>();        nanos.put("Read from main memory (RAM)", 100.0);        nanos.put("Read 4 KB at random from an SSD", 100_000.0);        nanos.put("Round trip inside one datacenter", 500_000.0);        nanos.put("Read 1 MB one after another from an SSD", 1_000_000.0);        nanos.put("Simple MySQL query by primary key (in RAM)", 1_000_000.0);        nanos.put("Round trip Mumbai to US east coast", 200_000_000.0);
        System.out.printf("%-42s %12s   %s%n", "Operation", "time", "if 100 ns were 1 second");        for (Map.Entry<String, Double> e : nanos.entrySet()) {            double ns = e.getValue();            String time = ns >= 1_000_000 ? String.format("%.0f ms", ns / 1_000_000)                        : ns >= 1_000 ? String.format("%.0f us", ns / 1_000)                        : String.format("%.0f ns", ns);            double humanSeconds = ns / 100;            String human = humanSeconds < 3_600 ? String.format("%.0f s", humanSeconds)                         : humanSeconds < 86_400 ? String.format("%.1f hours", humanSeconds / 3_600)                         : String.format("%.0f days", humanSeconds / 86_400);            System.out.printf("%-42s %12s   %s%n", e.getKey(), time, human);        }    }}
Output
Operation                                          time   if 100 ns were 1 second
Read from main memory (RAM)                      100 ns   1 s
Read 4 KB at random from an SSD                  100 us   1000 s
Round trip inside one datacenter                 500 us   1.4 hours
Read 1 MB one after another from an SSD            1 ms   2.8 hours
Simple MySQL query by primary key (in RAM)         1 ms   2.8 hours
Round trip Mumbai to US east coast               200 ms   23 days

What this code does

  • The values are rough orders of magnitude for typical servers, not measurements; real numbers change with hardware, but the ratios stay similar.
  • Scaling them up makes the gaps easy to feel: if a memory read took 1 second, an SSD read would take over a quarter of an hour and one request to the US would take 23 days.
  • The lesson for design: memory is about 1,000 times faster than SSD, and a cross-continent call costs as much as hundreds of database lookups.

The exact figures change with hardware; the ratios are what you use. Memory is roughly 1,000 times faster than an SSD, and a call between continents is hundreds of times slower than one inside a datacenter.

1 ms = 1,000 µs = 1,000,000 ns. Mixing these up is the most common latency mistake.

Bytes per record: ids, timestamps, short strings, an order row

Record size is the number of bytes one row or object takes: add its fields, then a rough amount for headers and indexes. Storage estimates multiply it by how many records you keep.

An INT id is 4 bytes, a BIGINT 8, a timestamp about 8, a UUID 16 bytes as binary or 36 as text, and short English text about 1 byte per character. The code adds them up for one ShopLite order.

Example 3 · Bytes in a typical ShopLite order row

OrderRowSizeDemo.javaJava
import java.util.LinkedHashMap;import java.util.Map;
public class OrderRowSizeDemo {    public static void main(String[] args) {        Map<String, Integer> bytes = new LinkedHashMap<>();        bytes.put("id (INT)", 4);        bytes.put("customer_id (INT)", 4);        bytes.put("address_id (INT)", 4);        bytes.put("status (short text, ~10 chars)", 10);        bytes.put("ordered_at (DATETIME)", 8);        bytes.put("total (DECIMAL(10,2))", 5);        bytes.put("3 order items x 20 bytes", 60);        bytes.put("row and index overhead (rough)", 105);
        int total = 0;        for (Map.Entry<String, Integer> e : bytes.entrySet()) {            System.out.printf("%-32s %4d bytes%n", e.getKey(), e.getValue());            total += e.getValue();        }        System.out.printf("%-32s %4d bytes  -> use 200 bytes per order%n", "Total", total);    }}
Output
id (INT)                            4 bytes
customer_id (INT)                   4 bytes
address_id (INT)                    4 bytes
status (short text, ~10 chars)     10 bytes
ordered_at (DATETIME)               8 bytes
total (DECIMAL(10,2))               5 bytes
3 order items x 20 bytes           60 bytes
row and index overhead (rough)    105 bytes
Total                             200 bytes  -> use 200 bytes per order

What this code does

  • Numbers take fixed space: INT 4 bytes, BIGINT 8, a date-time about 5–8. Short ASCII text takes about 1 byte per character in UTF-8.
  • Databases add per-row headers and index entries, so a rough overhead line keeps the estimate honest. The real total depends on the engine; here it rounds to 200 bytes per order.
  • A round number is the goal: 200 bytes is easy to multiply and close enough for every decision this module makes.

Add a rough amount for row headers and indexes, then round up to a friendly number. For ShopLite, an order with its items is about 200 bytes and a product row with name and description is about 1 KB.

Seconds per day, and rounding rules

The order of magnitude of a number is its power of ten. 300 and 500 are the same order of magnitude; 300 and 30,000 are not, and only a different order of magnitude changes the design.

A day has 86,400 seconds. Round it to 10⁵ (100,000), so "10 million requests a day" becomes "about 100 a second". The code shows how close the rounded answer is.

Example 4 · Seconds per day: exact vs rounded

SecondsPerDayDemo.javaJava
public class SecondsPerDayDemo {    public static void main(String[] args) {        long secondsPerDay = 24 * 60 * 60;        System.out.println("Seconds per day: " + secondsPerDay + " (about 10^5)");        System.out.println("Seconds per year: " + secondsPerDay * 365 + " (about 3 x 10^7)");
        long requestsPerDay = 10_000_000;        double exact = (double) requestsPerDay / secondsPerDay;        double rounded = requestsPerDay / 100_000.0;        System.out.printf("10 million requests a day: exact %.1f per second, rounded %.0f per second%n", exact, rounded);        System.out.printf("Rounding error: %.0f%%%n", (exact - rounded) * 100 / exact);    }}
Output
Seconds per day: 86400 (about 10^5)
Seconds per year: 31536000 (about 3 x 10^7)
10 million requests a day: exact 115.7 per second, rounded 100 per second
Rounding error: 14%

What this code does

  • A day has 86,400 seconds. Rounding it to 100,000 (10⁵) turns "divide by 86,400" into "drop five zeros", which you can do in your head.
  • The rounded answer is about 14% low. That is fine for a first estimate, and you can say so: "about 100 a second, really a bit more".

Round every input to one or two significant figures (1 million, 200 bytes, 5 years) and say each assumption out loud. Round up when unsure, because under-estimating is the riskier mistake. Keep these in your head:

  • A day is about 10⁵ s, a month about 2.5 × 10⁶ s, a year about 3 × 10⁷ s.
  • 1 million a day is about 12 a second, so "about 10".

Find the bug

Find the wrong unit

Click the line that gives the wrong answer.

This should print the average requests per second.

Round 1 of 3

A Java helper that prints byte counts in human units

Estimators produce huge byte counts such as 3,650,000,000. humanBytes turns them into 3.7 GB: it divides by 1,000 while the value is at least 1,000 and keeps one decimal.

Example 5 · A helper that prints byte counts in human units

HumanBytesDemo.javaJava
public class HumanBytesDemo {    static String humanBytes(long bytes) {        String[] units = {"B", "KB", "MB", "GB", "TB", "PB"};        double value = bytes;        int unit = 0;        while (value >= 1000 && unit < units.length - 1) {            value /= 1000;                       // decimal units, as estimates use            unit++;        }        return unit == 0 ? bytes + " B" : String.format("%.1f %s", value, units[unit]);    }
    public static void main(String[] args) {        long[] sizes = {512, 200L * 1_000, 200L * 50_000 * 365, 2_500_000_000_000L, 4_000_000_000_000_000L};        for (long s : sizes) {            System.out.printf("%-27s -> %s%n", String.format("%,d", s), humanBytes(s));        }    }}
Output
512                         -> 512 B
200,000                     -> 200.0 KB
3,650,000,000               -> 3.7 GB
2,500,000,000,000           -> 2.5 TB
4,000,000,000,000,000       -> 4.0 PB

What this code does

  • The loop divides by 1,000 until the number is below 1,000, moving one unit up each time, then prints one decimal place.
  • The third size is a real estimate: 50,000 orders a day × 200 bytes × 365 days = 3.7 GB a year. A helper like this makes any estimator's output readable.
  • It uses decimal units (1 KB = 1,000 bytes), which match the rounded maths of estimation. Operating systems often show binary units instead (1 KiB = 1,024 bytes).

Pass a long or a double: an int cannot hold more than about 2 GB. Every estimator in this module can end with this helper, so the answer is readable at a glance.

Your turn: size a year of orders

ShopLite gets 50,000 orders a day and each order takes about 200 bytes. Write your own estimate on paper first, then pick the closest option and read the steps.

Predict the output

Estimate, then check

Do the sum yourself, then pick the closest answer.

Round 1 of 2

How big is one year of ShopLite orders?

long ordersPerDay = 50_000;
int bytesPerOrder = 200;
// How many bytes do 365 days of orders take?

Next step

Check your understanding

11 quick questions