Sub topic 5
Latency Orders of Magnitude: Memory, SSD, Network, Disk
This chapter assumes you can do arithmetic with powers of ten, you know what a cache, a database and a load balancer do, and you can read short Python snippets. You do not need to remember exact hardware figures. You need to remember the ratios between them, because those ratios decide where a design spends its time.
Why latency numbers belong in an estimate
Traffic and storage estimates tell you how much work a system does. Latency estimates tell you how long one unit of that work takes, and which resource dominates that time. A request that reads from memory and a request that reads from a spinning disk can differ by a factor of 100,000. No amount of server tuning closes a gap that size. A design change does, such as adding a cache or removing a sequential network call.
In an estimate you use latency numbers for three purposes:
- Deciding whether a data path can meet a response-time target at all.
- Choosing where a cache or an index must sit to keep a hot path fast.
- Estimating per-server throughput, which the later chapter on server capacity builds on.
The reference figures
The table gives round numbers that are widely quoted and good to within a small factor on current hardware. They are not benchmarks. Real values vary with the machine, the load and the software stack.
| Operation | Approximate time | Rounded to |
|---|---|---|
| CPU register or L1 cache access | 1 ns | 1 ns |
| Main memory (RAM) read | 100 ns | 100 ns |
| Compress 1 KB of data | 2 to 10 microseconds | 10 µs |
| Read 1 MB sequentially from RAM | 3 to 10 microseconds per 1 KB-class block, about 50 to 250 µs per MB | 100 µs |
| SSD random read (4 KB) | 16 to 100 µs | 100 µs |
| Round trip inside one data center | 0.5 ms | 0.5 ms |
| Read 1 MB sequentially from SSD | about 1 ms | 1 ms |
| Spinning disk seek | 5 to 10 ms | 10 ms |
| Read 1 MB sequentially from spinning disk | 5 to 20 ms | 10 ms |
| Round trip between continents | 100 to 150 ms | 150 ms |
Units are nanoseconds (ns, one billionth of a second), microseconds (µs, one millionth) and milliseconds (ms, one thousandth). Each step from ns to µs to ms is a factor of 1,000.
The ratios to memorize
Do not try to memorize every row. Keep these relationships:
- Memory is about 1,000 times faster than SSD for a random read (100 ns against 100 µs).
- SSD is about 100 times faster than a spinning disk seek (100 µs against 10 ms).
- A round trip inside a data center costs about as much as several SSD reads.
- A round trip across an ocean costs about 300 times a round trip inside a data center.
- Sequential access is far cheaper per byte than random access, on every medium.
Seeing the gap with a scaled clock
Nanosecond figures are hard to feel. A common trick is to scale time so that one memory access takes one second. Under that scale, the following figures hold.
# Scale latencies so that one RAM read (100 ns) becomes 1 second.
latencies_ns = {
"RAM read": 100,
"SSD random read": 100_000,
"Data center round trip": 500_000,
"Disk seek": 10_000_000,
"Cross-continent round trip": 150_000_000,
}
scale = 1 / 100 # 100 ns -> 1 s, so each ns becomes 0.01 s
for name, ns in latencies_ns.items():
seconds = ns * scale
days = seconds / 86_400
print(f"{name:28s} {seconds:>12,.0f} s ({days:,.1f} days)")The output reads as follows:
- RAM read: 1 second.
- SSD random read: 1,000 seconds, about 17 minutes.
- Data center round trip: 5,000 seconds, about 1.4 hours.
- Disk seek: 100,000 seconds, about 1.2 days.
- Cross-continent round trip: 1,500,000 seconds, about 17 days.
If a memory read is a heartbeat, a disk seek is a long weekend and a trip across the ocean is more than two weeks. This is why a single uncached call to a distant service can cost more than thousands of in-memory operations.
Using the numbers in an estimate
Latency of a request path
To estimate the time of a request, list the operations on the critical path and add them. Operations that run in parallel count once, at the slowest branch. Operations that run in sequence add up.
Suppose a web request does the following in sequence:
- Load balancer to application server: one data center round trip, 0.5 ms.
- Application server checks a cache: one round trip, 0.5 ms.
- On a miss, application server queries a database on SSD: one round trip plus one SSD read, about 0.6 ms.
- Application server writes the result back to the cache: 0.5 ms.
A cache hit costs about 1 ms. A cache miss costs about 2.1 ms. The difference is small in absolute terms, so you would only call the cache essential if the database were on a spinning disk (10 ms or more per seek) or if the database were under load. Use the arithmetic to see whether an optimization matters before you add it.
Latency versus throughput per server
Latency also bounds how many requests one worker can handle. If a worker handles one request at a time and each request waits 10 ms for a disk, that worker completes at most 1,000 ms / 10 ms = 100 requests per second. This rule of thumb feeds the server-count estimate later in the series.
def max_requests_per_second(latency_ms: float, concurrency: int = 1) -> float:
"""Upper bound on throughput for workers that block on one slow step."""
return concurrency * 1000 / latency_ms
print(max_requests_per_second(10)) # 100.0 (one worker, disk seek)
print(max_requests_per_second(10, 32)) # 3200.0 (32 workers)
print(max_requests_per_second(0.1)) # 10000.0 (one worker, SSD read)The numbers are upper bounds. CPU work, lock contention and queueing lower the real rate.
Sequential versus random access
The table lists both a random SSD read and a sequential 1 MB read. Compare them. A 4 KB random read takes about 100 µs, so reading 1 MB (256 such blocks) by random access would take about 25 ms. Reading the same 1 MB sequentially takes about 1 ms. Random access is roughly 25 times slower for the same data.
The gap is larger on a spinning disk. Each random read pays a seek of about 10 ms, so 256 random reads cost about 2.5 seconds. A sequential read of the same 1 MB costs about 10 ms. That is a factor of 250.
This is why databases favor append-only logs and sorted files, and why an estimate that reads many small records from disk should use the random-access figure for each one.
block_kb = 4
mb_in_kb = 1024
blocks = mb_in_kb // block_kb # 256
ssd_random_us = 100
ssd_seq_ms_per_mb = 1
hdd_seek_ms = 10
hdd_seq_ms_per_mb = 10
print("SSD random 1 MB:", blocks * ssd_random_us / 1000, "ms") # 25.6 ms
print("SSD sequential 1 MB:", ssd_seq_ms_per_mb, "ms") # 1 ms
print("HDD random 1 MB:", blocks * hdd_seek_ms, "ms") # 2560 ms
print("HDD sequential 1 MB:", hdd_seq_ms_per_mb, "ms") # 10 msEdge cases and common errors
- Treating the table as exact. Cloud storage, network-attached disks and virtualized hosts often add latency. A network-attached SSD volume may behave closer to 1 ms per read than 100 µs. Say so in your assumptions when it matters.
- Mixing units. A value of 0.5 ms is 500 µs, not 50 µs. Convert everything to one unit before adding.
- Adding parallel calls. If a service fans out to five backends at once, the delay is the slowest of the five, not the sum. Tail latency makes the slowest of many calls worse than the typical call, so the more backends you fan out to, the higher the chance that one of them is slow.
- Ignoring serialization and queueing. The table covers the raw medium. Real requests also pay for encoding, TLS handshakes and waiting in queues under load.
- Forgetting the speed of light. Cross-continent latency has a physical floor. Light in fiber covers about 200 km per millisecond, so a 10,000 km path costs at least 50 ms one way, and 100 ms for a round trip, before any equipment delay. No engineering removes that floor. Only moving data closer to users does.
Summary of decisions the numbers support
| Question | What the latency numbers tell you |
|---|---|
| Do we need a cache in front of the database? | Yes if the database read is 100 times slower than a memory read and the data is read often. |
| Should this data live in memory or on SSD? | Memory for hot data on a strict latency budget. SSD when 100 µs per read is acceptable. |
| Can we call this service in series five times? | Five data center round trips cost about 2.5 ms. Five cross-continent trips cost about 750 ms. |
| Do we need a regional deployment? | If users are far away and the budget is under 100 ms, yes. |
Exercise
A request handler makes three sequential calls to services in the same data center (0.5 ms each round trip), then one random read from a spinning disk (10 ms). Another version of the handler does the three calls in parallel and reads from an SSD (100 µs). Estimate the latency of each version, and say which change saves more time.
Show answer
Version one: 3 x 0.5 ms + 10 ms = 11.5 ms. Version two: the parallel calls cost the slowest single call, 0.5 ms, plus 0.1 ms for the SSD read, so about 0.6 ms. The total saving is about 10.9 ms. Of that, the disk-to-SSD change saves 9.9 ms, and parallelizing the calls saves 1 ms. The storage change saves much more.
Exercise
One worker thread handles requests one at a time, and each request blocks for 2 ms on a data center round trip. What is the maximum throughput of one worker, and how many workers do you need to sustain 5,000 requests per second, ignoring CPU time?
Show answer
One worker handles at most 1000 / 2 = 500 requests per second. For 5,000 requests per second you need 5,000 / 500 = 10 workers. In practice you would add headroom, because CPU work and queueing push the real rate below this bound.