repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

opencode/skills/performance-optimization/napkin-math.md

Raw

Napkin Math

Techniques and numbers for estimating system's performance from first-principles.

The goal of this project is to collect software, numbers, and techniques to quickly estimate the expected performance of systems from first-principles. For example, how quickly can you read 1 GB of memory? By composing these resources you should be able to answer interesting questions like: how much storage cost should you expect to pay for logging for an application with 100,000 RPS?

Numbers

Below are numbers that are rounded from runs on a metal Intel Xeon E-2236 3.4GHz with 12 (virtual) cores.

Note 1: Some throughput and latency numbers don't line up, this is intentional for ease of calculations.

Note 2: Take the numbers with a grain of salt. E.g. for I/O, fio is the state-of-the-art.

Latency Reference

Operation Latency
L1 cache reference 0.5 ns
L2 cache reference 3 ns
Branch mispredict 5 ns
Mutex lock/unlock (uncontended) 15 ns
Main memory reference 50 ns
Compress 1K bytes with Snappy 1,000 ns
Read 4KB from SSD 20,000 ns
Round trip within same datacenter 50,000 ns
Read 1MB sequentially from memory 64,000 ns
Read 1MB over 100 Gbps network 100,000 ns
Read 1MB from SSD 1,000,000 ns
Disk seek 5,000,000 ns
Read 1MB sequentially from disk 10,000,000 ns
Send packet CA->Netherlands->CA 150,000,000 ns

Throughput Reference

Operation 1 MiB 1 GiB
Sequential Memory R/W (64 bytes) - Single Thread, No SIMD 100 μs 100 ms
Sequential Memory R/W (64 bytes) - Single Thread, SIMD 50 μs 50 ms
Sequential Memory R/W (64 bytes) - Threaded, No SIMD 35 μs 35 ms
Sequential Memory R/W (64 bytes) - Threaded, SIMD 30 μs 30 ms
Network Same-Zone 100 μs 100 ms
Hashing, not crypto-safe (64 bytes) 500 μs 500 ms
Random Memory R/W (64 bytes) 1 ms 1s
Fast Serialization 1 ms 1s
System Call N/A N/A
Hashing, crypto-safe (64 bytes) 1 ms 1s
Sequential SSD read (8 KiB) 200 μs 200 ms
Context Switch N/A N/A
Sequential SSD write, -fsync (8KiB) 1 ms 1s
TCP Echo Server (32 KiB) 200 μs 200 ms
Decompression 1 ms 1s
Compression 2 ms 2s
Sequential SSD write, +fsync (8KiB) 100 ms 2 min
Sorting (64-bit integers) 5 ms 5s
Sequential HDD Read (8 KiB) 2 ms 2s
Blob Storage GET, 1 conn 2 ms 2s
Random SSD Read (8 KiB) 15 ms 15s
Serialization 10 ms 10s
Deserialization 10 ms 10s
Random HDD Read (8 KiB) 2 s 30m
Network within same region 500 μs 500 ms
Premium network within zone/VPC 50 μs 40 ms

Database Query Latency

Operation Latency
{MySQL, Memcached, Redis, ..} Query ~500 μs

Network Between Regions

Route Latency Throughput
Network NA Central <-> East 40 ms 25 MiB/s
Network NA Central <-> West 40 ms 25 MiB/s
Network NA East <-> West 60 ms 25 MiB/s
Network EU West <-> NA East 80 ms 25 MiB/s
Network EU West <-> NA Central 100 ms 25 MiB/s
Network NA West <-> Singapore 180 ms 25 MiB/s
Network EU West <-> Singapore 160 ms 25 MiB/s

Cost Numbers

Approximate numbers that should be consistent between Cloud providers.

Compute

What Amount $/Month 1y commit $/month Spot $/month Hourly Spot $
CPU 1 $15 $10 $2 $0.005
GPU 1 $5000 $3000 $1500 $2

Memory & Storage

What Amount $/Month
Memory 1 GB $2
Warehouse Storage 1 GB $0.02
Blob (S3, GCS) 1 GB $0.02
Zonal HDD 1 GB $0.05
Ephemeral SSD 1 GB $0.08
Regional HDD 1 GB $0.1
Zonal SSD 1 GB $0.2
Regional SSD 1 GB $0.35

Networking

What Amount $/GB
Same Zone 1 GB $0
Blob 1 GB $0
Ingress 1 GB $0
L4 LB 1 GB $0.008
Inter-Zone 1 GB $0.01
Inter-Region 1 GB $0.02
Internet Egress 1 GB $0.1
CDN Egress 1 GB $0.05
CDN Fill 1 GB $0.01

Other

What Amount Cost
Warehouse Query 1 GB $0.005
Logs/Traces 1 GB $0.5
Metrics 1000 $20
EKM Keys 1 $1

Blob Storage Operations

Operation 1M 1000
Reads $0.4 $0.0004
Writes $5 $0.005
EKM Encryption $3 $0.003

Compression Ratios

What Compression Ratio
HTML 2-3x
English 2-4x
Source Code 2-4x
Executables 2-3x
RPC 5-10x
SSL -2% (actually grows slightly)

Note: Typically, *x in compression ratio decreases performance by 10x. E.g. we can get a 2x ratio on English Wikipedia at ~200 MiB/s, and 3x at ~20MiB/s, and 4x at 1MB/s.


Techniques


Example: Time to Quicksort a Billion 4-Byte Numbers

As a rough approximation, a good quicksort algorithm makes log(N) passes over an array of size N:

  1. Memory bandwidth: 4 GB array (4 bytes × 1 billion). At ~16GB/s memory bandwidth per core, each pass takes ~0.25s. With ~30 passes: ~7.5 seconds.
  2. Branch mispredictions: N*log(N) = ~30 billion comparisons. At 50% misprediction rate: 15 billion × 5ns = 75 seconds.
  3. Total estimate: ~82.5 seconds

With cache optimization: Assuming 32MB L3 cache (holds 2^23 numbers), the last 22 passes operate from L3 cache, reducing memory transfer to ~2.5 seconds instead of 7.5.


Example: Time to Generate a Web Page with 30 Image Thumbnails

Option 1: Serial reading

Option 2: Parallel (K disks)

Option 3: Single SSD


Markdown source
File outline
# Napkin Math

Techniques and numbers for estimating system's performance from
first-principles.

The goal of this project is to collect software, numbers, and techniques to
quickly estimate the expected performance of systems from first-principles. For
example, how quickly can you read 1 GB of memory? By composing these resources
you should be able to answer interesting questions like: how much storage cost
should you expect to pay for logging for an application with 100,000 RPS?

## Numbers

Below are numbers that are rounded from runs on a metal Intel Xeon E-2236 3.4GHz
with 12 (virtual) cores.

**Note 1:** Some throughput and latency numbers don't line up, this is
intentional for ease of calculations.

**Note 2:** Take the numbers with a grain of salt. E.g. for I/O, `fio` is the
state-of-the-art.

### Latency Reference

| Operation                         | Latency        |
| --------------------------------- | -------------- |
| L1 cache reference                | 0.5 ns         |
| L2 cache reference                | 3 ns           |
| Branch mispredict                 | 5 ns           |
| Mutex lock/unlock (uncontended)   | 15 ns          |
| Main memory reference             | 50 ns          |
| Compress 1K bytes with Snappy     | 1,000 ns       |
| Read 4KB from SSD                 | 20,000 ns      |
| Round trip within same datacenter | 50,000 ns      |
| Read 1MB sequentially from memory | 64,000 ns      |
| Read 1MB over 100 Gbps network    | 100,000 ns     |
| Read 1MB from SSD                 | 1,000,000 ns   |
| Disk seek                         | 5,000,000 ns   |
| Read 1MB sequentially from disk   | 10,000,000 ns  |
| Send packet CA->Netherlands->CA   | 150,000,000 ns |

### Throughput Reference

| Operation                                                 | 1 MiB  | 1 GiB  |
| --------------------------------------------------------- | ------ | ------ |
| Sequential Memory R/W (64 bytes) - Single Thread, No SIMD | 100 μs | 100 ms |
| Sequential Memory R/W (64 bytes) - Single Thread, SIMD    | 50 μs  | 50 ms  |
| Sequential Memory R/W (64 bytes) - Threaded, No SIMD      | 35 μs  | 35 ms  |
| Sequential Memory R/W (64 bytes) - Threaded, SIMD         | 30 μs  | 30 ms  |
| Network Same-Zone                                         | 100 μs | 100 ms |
| Hashing, not crypto-safe (64 bytes)                       | 500 μs | 500 ms |
| Random Memory R/W (64 bytes)                              | 1 ms   | 1s     |
| Fast Serialization                                        | 1 ms   | 1s     |
| System Call                                               | N/A    | N/A    |
| Hashing, crypto-safe (64 bytes)                           | 1 ms   | 1s     |
| Sequential SSD read (8 KiB)                               | 200 μs | 200 ms |
| Context Switch                                            | N/A    | N/A    |
| Sequential SSD write, -fsync (8KiB)                       | 1 ms   | 1s     |
| TCP Echo Server (32 KiB)                                  | 200 μs | 200 ms |
| Decompression                                             | 1 ms   | 1s     |
| Compression                                               | 2 ms   | 2s     |
| Sequential SSD write, +fsync (8KiB)                       | 100 ms | 2 min  |
| Sorting (64-bit integers)                                 | 5 ms   | 5s     |
| Sequential HDD Read (8 KiB)                               | 2 ms   | 2s     |
| Blob Storage GET, 1 conn                                  | 2 ms   | 2s     |
| Random SSD Read (8 KiB)                                   | 15 ms  | 15s    |
| Serialization                                             | 10 ms  | 10s    |
| Deserialization                                           | 10 ms  | 10s    |
| Random HDD Read (8 KiB)                                   | 2 s    | 30m    |
| Network within same region                                | 500 μs | 500 ms |
| Premium network within zone/VPC                           | 50 μs  | 40 ms  |

### Database Query Latency

| Operation                           | Latency |
| ----------------------------------- | ------- |
| {MySQL, Memcached, Redis, ..} Query | ~500 μs |

### Network Between Regions

| Route                          | Latency | Throughput |
| ------------------------------ | ------- | ---------- |
| Network NA Central <-> East    | 40 ms   | 25 MiB/s   |
| Network NA Central <-> West    | 40 ms   | 25 MiB/s   |
| Network NA East <-> West       | 60 ms   | 25 MiB/s   |
| Network EU West <-> NA East    | 80 ms   | 25 MiB/s   |
| Network EU West <-> NA Central | 100 ms  | 25 MiB/s   |
| Network NA West <-> Singapore  | 180 ms  | 25 MiB/s   |
| Network EU West <-> Singapore  | 160 ms  | 25 MiB/s   |

---

## Cost Numbers

Approximate numbers that should be consistent between Cloud providers.

### Compute

| What | Amount | $/Month | 1y commit $/month | Spot $/month | Hourly Spot $ |
| ---- | ------ | ------- | ----------------- | ------------ | ------------- |
| CPU  | 1      | $15     | $10               | $2           | $0.005        |
| GPU  | 1      | $5000   | $3000             | $1500        | $2            |

### Memory & Storage

| What              | Amount | $/Month |
| ----------------- | ------ | ------- |
| Memory            | 1 GB   | $2      |
| Warehouse Storage | 1 GB   | $0.02   |
| Blob (S3, GCS)    | 1 GB   | $0.02   |
| Zonal HDD         | 1 GB   | $0.05   |
| Ephemeral SSD     | 1 GB   | $0.08   |
| Regional HDD      | 1 GB   | $0.1    |
| Zonal SSD         | 1 GB   | $0.2    |
| Regional SSD      | 1 GB   | $0.35   |

### Networking

| What            | Amount | $/GB   |
| --------------- | ------ | ------ |
| Same Zone       | 1 GB   | $0     |
| Blob            | 1 GB   | $0     |
| Ingress         | 1 GB   | $0     |
| L4 LB           | 1 GB   | $0.008 |
| Inter-Zone      | 1 GB   | $0.01  |
| Inter-Region    | 1 GB   | $0.02  |
| Internet Egress | 1 GB   | $0.1   |
| CDN Egress      | 1 GB   | $0.05  |
| CDN Fill        | 1 GB   | $0.01  |

### Other

| What            | Amount | Cost   |
| --------------- | ------ | ------ |
| Warehouse Query | 1 GB   | $0.005 |
| Logs/Traces     | 1 GB   | $0.5   |
| Metrics         | 1000   | $20    |
| EKM Keys        | 1      | $1     |

### Blob Storage Operations

| Operation      | 1M   | 1000    |
| -------------- | ---- | ------- |
| Reads          | $0.4 | $0.0004 |
| Writes         | $5   | $0.005  |
| EKM Encryption | $3   | $0.003  |

---

## Compression Ratios

| What        | Compression Ratio             |
| ----------- | ----------------------------- |
| HTML        | 2-3x                          |
| English     | 2-4x                          |
| Source Code | 2-4x                          |
| Executables | 2-3x                          |
| RPC         | 5-10x                         |
| SSL         | -2% (actually grows slightly) |

**Note:** Typically, *x in compression ratio decreases performance by 10x. E.g.
we can get a 2x ratio on English Wikipedia at ~200 MiB/s, and 3x at ~20MiB/s,
and 4x at 1MB/s.

---

## Techniques

- **Don't overcomplicate.** If you are basing your calculation on more than 6
  assumptions, you're likely making it harder than it should be.
- **Keep the units.** They're good checksumming. Wolframalpha has terrific
  support if you need a hand in converting e.g. KiB to TiB.
- **Calculate with exponents.** A lot of back-of-the-envelope calculations are
  done with just coefficients and exponents, e.g. `c * 10^e`. Your goal is to
  get within an order of magnitude right--that's just `e`. `c` matters a lot
  less. Only worrying about single-digit coefficients and exponents makes it
  much easier on a napkin.
- **Perform Fermi decomposition.** Write down things you can guess at until you
  can start to hint at an answer. When you want to know the cost of storage for
  logging, you're going to want to know how big a log line is, how many of those
  you have per second, what that costs, and so on.

---

## Example: Time to Quicksort a Billion 4-Byte Numbers

As a rough approximation, a good quicksort algorithm makes log(N) passes over an
array of size N:

1. **Memory bandwidth**: 4 GB array (4 bytes × 1 billion). At ~16GB/s memory
   bandwidth per core, each pass takes ~0.25s. With ~30 passes: ~7.5 seconds.
2. **Branch mispredictions**: N*log(N) = ~30 billion comparisons. At 50%
   misprediction rate: 15 billion × 5ns = 75 seconds.
3. **Total estimate**: ~82.5 seconds

**With cache optimization**: Assuming 32MB L3 cache (holds 2^23 numbers), the
last 22 passes operate from L3 cache, reducing memory transfer to ~2.5 seconds
instead of 7.5.

---

## Example: Time to Generate a Web Page with 30 Image Thumbnails

**Option 1: Serial reading**

- One seek + transfer per image: 5ms + 10ms = 15ms per image
- Total: 30 × 15ms = 450ms

**Option 2: Parallel (K disks)**

- Latency drops by factor of K
- With hundreds of disks: ~15ms

**Option 3: Single SSD**

- Sequential read: 20μs + 1ms per image = ~30ms total

---