Latency Numbers Every Programmer Should Know
Rough orders of magnitude, not exact values. The point isn't the digits — it's the ratios between tiers: each tier is roughly ~100× slower than the one above it. Cache → RAM → SSD → spinning disk / network. When guessing where time goes, reach for these.
Units: 1 ms = 1,000 µs = 1,000,000 ns.
1. On-CPU — L1/L2 cache & RAM (nanoseconds)
The "free" tier. You live here. Stay here.
| Operation | Latency | Notes |
|---|---|---|
| L1 cache reference | 0.5 ns | baseline — data already next to the core |
| Branch mispredict | 5 ns | CPU guessed the wrong `if` branch and had to redo work |
| L2 cache reference | 7 ns | 14× slower than L1 |
| Mutex lock/unlock | 25 ns | cost of one lock+unlock when *uncontended* (see note below) |
| Main memory (RAM) reference | 100 ns | 20× slower than L2 |
| Read 4 KB randomly from memory | 1,000 ns | |
| Read 1 MB sequentially from memory | 250,000 ns | 0.25 ms |
What's a mutex? A mutex ("mutual exclusion" lock) is the guard that lets only one thread touch shared data at a time. A thread locks it before entering the critical section and unlocks it after, so two threads can't stomp on the same variable at once. The 25 ns above is the fast, uncontended case — nobody else wants the lock, so it's just a couple of atomic CPU instructions. If another thread is already holding it (contended), yours has to wait/sleep and the cost can jump to microseconds or more — orders of magnitude worse. Takeaway: locks are cheap only when threads rarely collide; lots of contention is a real performance problem.
2. Local storage — SSD & spinning disk (microseconds → milliseconds)
Left the chip. Now you're paying real time.
| Operation | Latency | Notes |
|---|---|---|
| Read 4 KB randomly from SSD | 150,000 ns | 0.15 ms |
| Read 1 MB sequentially from SSD | 1,000,000 ns | 1 ms · 4× slower than RAM |
| Disk seek (spinning) | 10,000,000 ns | 10 ms · ≈ 20 DC round trips |
| Read 1 MB sequentially from disk | 20,000,000 ns | 20 ms · 80× slower than RAM |
3. Over the wire — network (microseconds → hundreds of ms)
Distance is destiny. Local link is cheap; crossing the planet is not.
| Operation | Latency | Notes |
|---|---|---|
| Send 1 KB over 1 Gbps network | 10,000 ns | 0.01 ms |
| Round trip within same datacenter | 500,000 ns | 0.5 ms |
| Packet CA → Netherlands → CA | 150,000,000 ns | 150 ms · 15× a disk seek |
Bonus — CPU work
Compression sits between RAM and SSD — cheaper than it feels.
| Operation | Latency | Notes |
|---|---|---|
| Compress 1 KB with Zippy | 3,000 ns | 0.003 ms |
The three ratios worth memorizing
- Cache → RAM: ~100× (0.5 ns → 100 ns). Stay in cache.
- RAM → disk seek: ~100,000× (100 ns → 10 ms). Avoid random disk.
- DC hop → cross-continent: ~300× (0.5 ms → 150 ms). Geography dominates everything.