AlgoViz

Back-of-the-envelope calculations

easy 25 min

A back-of-the-envelope calculation is a quick, rounded sum that tells you whether a design is feasible.

A back-of-the-envelope calculator

Read it, run it, change a line and run it again.

Loading editor…

Only writes add storage, machine counts round up, and capacity is planned at 70% with a spare. These are the three usual slips.

The same in TypeScript
const DAY = 86_400;const GB = 1e9;const TB = 1e12;const qps = (daily: number, factor = 3) => ({ average: Math.round(daily / DAY), peak: Math.round((daily / DAY) * factor) });const storageTB = (perDay: number, bytes: number, years: number, copies = 1) => (perDay * bytes * 365 * years * copies) / TB;const bandwidthGbps = (perSecond: number, bytes: number) => (perSecond * bytes * 8) / GB;const servers = (peak: number, capacity = 1_000, percent = 70, spare = 1) =>  Math.ceil((peak * 100) / (capacity * percent)) + spare;// URL shortener: 100M new links a month, 100 reads per write, 500 bytes, 5 yearsconst writesPerDay = 100_000_000 / 30;const write = qps(writesPerDay);const read = qps(writesPerDay * 100);console.log("writes", write, "reads", read);console.log("storage", storageTB(writesPerDay, 500, 5).toFixed(2), "TB");console.log("redirect bytes", bandwidthGbps(read.peak, 500).toFixed(3), "Gb/s");console.log("servers", servers(read.peak));
The same in Java
public class Envelope {    static final long DAY = 86_400L;    static final double TB = 1e12;    record Qps(long average, long peak) {}    static Qps qps(long daily) {        double avg = (double) daily / DAY;        return new Qps(Math.round(avg), Math.round(avg * 3));    }    static double storageTB(long newPerDay, long bytes, int years) {        return (double) newPerDay * bytes * 365 * years / TB;    }    static long servers(long peak, long capacity, int percent, int spare) {        return (long) Math.ceil(peak * 100.0 / (capacity * percent)) + spare;    }    public static void main(String[] args) {        // A feed: 500M daily users, 10 views each, 1 new post of 1 KB for each user        Qps q = qps(500_000_000L * 10);        System.out.println("views: " + q);        System.out.println("new posts, 5 years: " + storageTB(500_000_000L, 1_000, 5) + " TB");        System.out.println("servers: " + servers(q.peak(), 1_000, 70, 1));    }}
The same in Go
package mainimport (	"fmt"	"math")const (	Day = 86400.0	GB  = 1e9	TB  = 1e12)func storageTB(perDay, bytes, years float64) float64 { return perDay * bytes * 365 * years / TB }func servers(peak, capacity, percent float64, spare int) int {	return int(math.Ceil(peak*100/(capacity*percent))) + spare}func main() {	// Video: 1M viewers at 5 Mb/s, 1,000 new videos a day of 1 GB	fmt.Printf("bytes out: %.0f Gb/s\n", 1_000_000*5.0/1000)	fmt.Printf("storage: %.0f TB a year\n", storageTB(1000, GB, 1))	fmt.Printf("bytes in: %.1f Mb/s\n", 1000*GB/Day*8/1e6)	fmt.Println("API servers for 50,000/s:", servers(50000, 1000, 70, 1))}