Sub topic 5
Latency Orders of Magnitude: Memory, SSD, Network, Disk
This chapter assumes you can do arithmetic with powers of ten and know what a cache, a database and a network round trip are. You do not need to memorize exact figures. You need the order of magnitude of each, because those orders decide which design is plausible.
Why latency belongs in an estimate
Throughput estimates tell you how much work arrives. Latency tells you how long each unit of work takes. A design that needs 5 disk seeks per request behaves very differently from one that needs 5 memory reads, even at the same request rate. The numbers below let you reject a design before you build anything.
Latency is the time one operation takes from start to finish. Different storage and network layers differ by factors of 10 to 1,000, so a rough figure is enough.
Reference figures
These are rounded, commonly quoted values for modern hardware. Real values vary by machine and workload.
| Operation | Approximate time | Rounded to |
|---|---|---|
| CPU cache or main memory read | 100 nanoseconds (ns) | 0.0001 ms |
| Read 1 MB sequentially from memory | 5 to 10 microseconds (µs) | about 0.01 ms |
| SSD random read | 100 µs | 0.1 ms |
| Round trip inside one data center | 500 µs | 0.5 ms |
| Read 1 MB sequentially from SSD | 1 ms | 1 ms |
| Disk (HDD) seek | 5 to 10 ms | 10 ms |
| Read 1 MB sequentially from HDD | 5 to 20 ms | 10 ms |
| Round trip between continents | 100 to 150 ms | 150 ms |
Units: 1 ms (millisecond) is 1,000 µs, and 1 µs is 1,000 ns.
The ratios matter more than the values
Memorize the steps between layers.
- Memory to SSD: about 1,000 times slower.
- SSD to HDD seek: about 100 times slower.
- Same data center to another continent: about 300 times slower.
Using the numbers
Suppose a page needs 20 sequential lookups to build a response. Compare three designs, using the table above.
- All data in memory: 20 × 100 ns = 2 µs.
- Data on SSD: 20 × 100 µs = 2 ms.
- Data on HDD: 20 × 10 ms = 200 ms.
A 200 ms response is already slow for an interactive request, so the HDD design needs caching or fewer lookups. The SSD design is fine. The calculation fits in the margin of a notebook.
The same arithmetic gives a single-thread limit. If one request spends 10 ms waiting on a disk seek and the thread cannot do other work, that thread serves about 1,000 ms / 10 ms = 100 requests per second. This feeds directly into the server capacity estimate later in the tutorial.
latency_ms = {
"memory": 0.0001,
"ssd": 0.1,
"hdd": 10,
}
lookups_per_request = 20
for name, ms in latency_ms.items():
total_ms = lookups_per_request * ms
per_thread_qps = 1000 / total_ms
print(name, total_ms, round(per_thread_qps))
# memory 0.002 500000
# ssd 2.0 500
# hdd 200.0 5Common mistakes
- Mixing units. A figure in microseconds read as milliseconds is off by 1,000.
- Treating sequential and random access as the same. Sequential reads from disk are far faster per byte than random seeks.
- Ignoring network hops. A request that crosses a data center boundary pays at least 0.5 ms per hop, and each extra hop adds up.
- Assuming exact values. Use the figures to compare designs, not to promise a response time.
Exercise
A request makes 5 sequential random reads. Estimate its total storage latency if the data is on an SSD (0.1 ms per read) and if it is on an HDD (10 ms per read). How many times slower is the HDD design?
Show answer
SSD: 5 × 0.1 ms = 0.5 ms. HDD: 5 × 10 ms = 50 ms. The HDD design is about 100 times slower, matching the ratio between an SSD random read and an HDD seek.
Exercise
A server thread blocks for 0.5 ms on one same-data-center round trip per request and does nothing else while waiting. About how many requests per second can one thread serve?
Show answer
1,000 ms / 0.5 ms = 2,000 requests per second per thread.