Skip to lesson

Estimating storage and bandwidth over years

ShopLite stores every order and every product photo, and the disk bill grows every month. How big will it be in five years, and how much network will it use?

Records per day × bytes per record × days × years

A storage estimate is records per day × bytes per record × 365 × years, worked out once for each kind of data that grows: orders, messages, log lines, uploaded files.

ShopLite gets about 50,000 orders a day, each about 200 bytes. The code multiplies it out for five years and checks it with a quick rounded version.

Example 1 · Five years of ShopLite orders

OrderStorageDemo.javaJava
public class OrderStorageDemo {    static String humanBytes(double bytes) {        String[] units = {"B", "KB", "MB", "GB", "TB", "PB"};        int unit = 0;        while (bytes >= 1000 && unit < units.length - 1) {            bytes /= 1000;            unit++;        }        return String.format("%.1f %s", bytes, units[unit]);    }
    public static void main(String[] args) {        long ordersPerDay = 50_000;      // 5% of 1 million DAU        int bytesPerOrder = 200;         // order row + 3 items + overhead        int years = 5;
        double perDay = (double) ordersPerDay * bytesPerOrder;        double perYear = perDay * 365;        double total = perYear * years;        System.out.println("Per day:   " + humanBytes(perDay));        System.out.println("Per year:  " + humanBytes(perYear));        System.out.println("5 years:   " + humanBytes(total));        System.out.printf("Quick check: 5 x 10^4 x 200 x 400 x 5 = %.0e bytes%n", 5e4 * 200 * 400 * 5);    }}
Output
Per day:   10.0 MB
Per year:  3.7 GB
5 years:   18.3 GB
Quick check: 5 x 10^4 x 200 x 400 x 5 = 2e+10 bytes

What this code does

  • Storage = records per day × bytes per record × 365 × years. Five years of ShopLite orders is about 18 GB of raw data.
  • The quick check rounds 365 up to 400 and lands at 2 × 10¹⁰ bytes (20 GB), the same range. Rounding up a little is the safe direction for storage.

Use the record sizes from the useful-numbers lesson and the daily counts from the requests-per-second lesson. Five years is the usual planning period in interviews, and each kind of data is its own line.

Replication factor, indexes and growth

The replication factor is how many copies of the data are kept. A primary database plus two replicas is a factor of 3, so the disk holds three times the data.

Raw data is only the start: indexes add to it, every copy multiplies it and growth compounds year by year. The code applies each factor as its own multiplication.

Five years of ShopLite orders: 33 GB of raw data, times 1.5 for indexes gives 49.5 GB, times 3 for a primary and two replicas gives 148.5 GB on disk. Product images, about 1.4 TB, go to object storage instead and only their key is kept in MySQL.

Example 2 · Adding indexes, replicas and growth

StorageFactorsDemo.javaJava
public class StorageFactorsDemo {    static String humanBytes(double bytes) {        String[] units = {"B", "KB", "MB", "GB", "TB", "PB"};        int unit = 0;        while (bytes >= 1000 && unit < units.length - 1) {            bytes /= 1000;            unit++;        }        return String.format("%.1f %s", bytes, units[unit]);    }
    public static void main(String[] args) {        double ordersPerDay = 50_000;        int bytesPerOrder = 200;        double indexFactor = 1.5;       // indexes add about half again        int copies = 3;                 // primary + 2 replicas        double growth = 0.30;           // orders grow 30% a year
        double raw = 0;        for (int year = 1; year <= 5; year++) {            double thisYear = ordersPerDay * bytesPerOrder * 365;            raw += thisYear;            System.out.printf("year %d: %,9.0f orders/day, %s this year%n", year, ordersPerDay, humanBytes(thisYear));            ordersPerDay *= 1 + growth;        }        System.out.println("Raw data, 5 years:        " + humanBytes(raw));        System.out.println("With indexes (x1.5):      " + humanBytes(raw * indexFactor));        System.out.println("On disk, 3 copies:        " + humanBytes(raw * indexFactor * copies));    }}
Output
year 1:    50,000 orders/day, 3.7 GB this year
year 2:    65,000 orders/day, 4.7 GB this year
year 3:    84,500 orders/day, 6.2 GB this year
year 4:   109,850 orders/day, 8.0 GB this year
year 5:   142,805 orders/day, 10.4 GB this year
Raw data, 5 years:        33.0 GB
With indexes (x1.5):      49.5 GB
On disk, 3 copies:        148.5 GB

What this code does

  • With 30% growth a year, the fifth year alone stores almost 3 times the first. Over five years the raw data is about 33 GB, not the 18 GB of the flat estimate.
  • Indexes and replicas multiply it again: about 148 GB of disk in total across the three database servers. Each step is one multiplication, written down so anyone can change it.

Indexes (extra structures that make queries fast) often add 50–100% on top of the table data. Growth matters most over years: at 30% a year, year five stores almost 3 times year one, so apply it year by year.

Trace the code

Growth, year by year

public class GrowthSteps {
public static void main(String[] args) {
double ordersPerDay = 50_000;
double totalBytes = 0;
for (int year = 1; year <= 3; year++) {
totalBytes += ordersPerDay * 200 * 365;
ordersPerDay *= 1.3;
}
System.out.printf("%.1f GB%n", totalBytes / 1e9);
}
}

Variables

ordersPerDay
50000

Output

(nothing printed yet)

Line 3Year one starts at 50,000 orders a day.

Step 1 of 8

Images and media dominate: object storage vs the database

Object storage is a service that stores files by a key, cheaply and with its own copies, such as Amazon S3 or Google Cloud Storage. The database keeps only the key or URL.

One product row is about 1 KB, but its five photos in three sizes are about 3 MB. The code compares all product rows with all product images.

Example 3 · Images dominate: product photos vs product rows

MediaVsRowsDemo.javaJava
public class MediaVsRowsDemo {    static String humanBytes(double bytes) {        String[] units = {"B", "KB", "MB", "GB", "TB", "PB"};        int unit = 0;        while (bytes >= 1000 && unit < units.length - 1) {            bytes /= 1000;            unit++;        }        return String.format("%.1f %s", bytes, units[unit]);    }
    public static void main(String[] args) {        long products = 50_000;        int rowBytes = 1_000;                  // name, description, price, stock        int imagesPerProduct = 5;        int[] sizeVariants = {500_000, 100_000, 20_000};   // original, page size, thumbnail
        long bytesPerImage = 0;        for (int v : sizeVariants) bytesPerImage += v;
        double rows = (double) products * rowBytes;        double images = (double) products * imagesPerProduct * bytesPerImage;        System.out.println("Product rows in MySQL:  " + humanBytes(rows));        System.out.println("Product images:         " + humanBytes(images));        System.out.printf("Images are %.0f times the rows%n", images / rows);    }}
Output
Product rows in MySQL:  50.0 MB
Product images:         155.0 GB
Images are 3100 times the rows

What this code does

  • One product row is about 1 KB; its five photos in three sizes are about 3 MB. So the images are about 3,000 times bigger than all the product rows.
  • That is why media goes to object storage (a service such as Amazon S3 or Google Cloud Storage that stores files by key), while the database keeps only the image key or URL. The database stays small and fast to back up.

So ShopLite stores images in object storage and keeps only the key in MySQL. The database stays small, fast and easy to back up, and the images are served through a CDN.

Storing images as BLOB columns in MySQL makes backups, replicas and restores many times slower. Keep files out of the database.

Bandwidth: requests × response size

Bandwidth is how many bytes flow over the network each second: requests per second × bytes per response, worked out at peak.

Work out API responses and images separately, at peak. Network speeds are quoted in bits, so the code also multiplies bytes by 8.

Example 4 · Bandwidth: requests × response size, in bytes and bits

BandwidthDemo.javaJava
public class BandwidthDemo {    public static void main(String[] args) {        double peakApiRps = 1_736;        double apiResponseBytes = 20_000;        double pageViewsPerSecond = 500;           // product pages at peak        int imagesPerPage = 8;        double imageBytes = 100_000;               // page-size image
        double apiBytesPerSecond = peakApiRps * apiResponseBytes;        double imageBytesPerSecond = pageViewsPerSecond * imagesPerPage * imageBytes;        System.out.printf("API:    %.1f MB/s = %.0f Mbit/s%n", apiBytesPerSecond / 1e6, apiBytesPerSecond * 8 / 1e6);        System.out.printf("Images: %.1f MB/s = %.0f Mbit/s%n", imageBytesPerSecond / 1e6, imageBytesPerSecond * 8 / 1e6);        double perDay = (apiBytesPerSecond + imageBytesPerSecond) / 5 * 86_400;   // peak / 5 = average        System.out.printf("Outgoing per day (average load): %.1f TB%n", perDay / 1e12);    }}
Output
API:    34.7 MB/s = 278 Mbit/s
Images: 400.0 MB/s = 3200 Mbit/s
Outgoing per day (average load): 7.5 TB

What this code does

  • Bandwidth = requests per second × bytes per response. Network speeds are quoted in bits per second, so multiply bytes by 8: 34.7 MB/s of JSON is about 278 Mbit/s.
  • Images again dominate, about 400 MB/s at peak. Serving them from a CDN (servers near the users that keep copies of static files) takes that load off ShopLite's own servers.
  • Dividing the peak by the peak factor (5) gives the average, and × 86,400 the bytes per day: about 7.5 TB, a number that drives the hosting bill.

1 byte = 8 bits, so 35 MB/s is 280 Mbit/s; mixing them up is the most common bandwidth mistake. A CDN (content delivery network) keeps copies of images on servers near the users, so only the misses reach ShopLite.

Find the bug

Find the mistake

Click the line that gives the wrong answer.

This should say whether 60 MB/s fits a 400 Mbit/s link.

Round 1 of 3

Worksheet: 5-year storage for ShopLite

A worksheet is a table of every kind of data with its rate, size, growth, index factor and copies, which ends with a design conclusion.

ShopLite's worksheet adds orders, products, customers and images, then states the conclusion. The code prints it line by line.

Example 5 · Worksheet: 5-year storage for ShopLite

StorageWorksheet.javaJava
public class StorageWorksheet {    static String humanBytes(double bytes) {        String[] units = {"B", "KB", "MB", "GB", "TB", "PB"};        int unit = 0;        while (bytes >= 1000 && unit < units.length - 1) {            bytes /= 1000;            unit++;        }        return String.format("%.1f %s", bytes, units[unit]);    }
    static double fiveYears(double perDayStart, double growth) {        double total = 0, perDay = perDayStart;        for (int y = 0; y < 5; y++) {            total += perDay * 365;            perDay *= 1 + growth;        }        return total;    }
    public static void main(String[] args) {        double orders = fiveYears(50_000 * 200.0, 0.30) * 1.5 * 3;        // Products: 50,000 now, +20,000 a year; rows 1 KB (x1.5 indexes, 3 copies)        double productCount = 50_000 + 5 * 20_000;        double productRows = productCount * 1_000 * 1.5 * 3;        // Images: 5 per product, 620 KB for three sizes, object storage keeps 3 copies itself        double images = productCount * 5 * 620_000 * 3;        // Customers: 5 million accounts by year 5, 150 bytes each (x1.5, 3 copies)        double customers = 5_000_000 * 150.0 * 1.5 * 3;
        double database = orders + productRows + customers;        System.out.println("MySQL (orders, products, customers): " + humanBytes(database));        System.out.println("  orders:    " + humanBytes(orders));        System.out.println("  products:  " + humanBytes(productRows));        System.out.println("  customers: " + humanBytes(customers));        System.out.println("Object storage (images):             " + humanBytes(images));        System.out.printf("Images share of all storage: %.0f%%%n", images * 100 / (images + database));    }}
Output
MySQL (orders, products, customers): 152.6 GB
  orders:    148.5 GB
  products:  675.0 MB
  customers: 3.4 GB
Object storage (images):             1.4 TB
Images share of all storage: 90%

What this code does

  • Every line of the worksheet is an assumption you can point at: rate, size, growth, index factor, copies. Changing any one changes only its line.
  • The database ends up around 150 GB after five years, which fits comfortably on one MySQL server and its replicas. The images reach about 1.4 TB, about 90% of all storage, and live in object storage.
  • The design conclusion is the real output: the database does not need sharding for storage reasons; media needs object storage and a CDN.

ShopLite's answer: about 150 GB of database across three copies and about 1.4 TB of images in object storage. The last line is always a decision: one MySQL primary with replicas is enough for storage, and media belongs in object storage behind a CDN.

Your turn: a support-ticket system

A support-ticket system gets 20,000 tickets a day of about 2 KB each, kept for 5 years, with 3 copies on disk. Write your own two numbers first, then check.

Predict the output

Estimate, then check

Do the sum yourself, then pick the closest answer.

Round 1 of 2

How big is the raw data after 5 years?

long ticketsPerDay = 20_000;
int bytesPerTicket = 2_000;
// raw bytes for 365 * 5 days = ?

Next step

Check your understanding

12 quick questions