5. Latency vs Throughput
5.7 Where does the time go inside one request?
On the highway: A 3-hour trip might be 20 minutes getting out of the city, 2 hours of open road, and 40 minutes stuck at one toll booth. To get there faster, you find the biggest piece first. Buying a faster car does nothing about the toll booth.
In the system: Break one request into its parts before optimising anything. A waterfall shows each step on a timeline.
Figure: in this example, 160 of the 206 ms are network and handshakes. The database query is only 30 ms.
The usual parts:
- Network and handshakes: DNS, TCP, TLS, then the request and response travelling (Chapters 3 and 4). Often the biggest part for users far away.
- Queueing: waiting for a free worker on the server (5.8).
- Work: your code, cache lookups, database queries, calls to other services.
- Response size: big responses take longer to send, and longer for the browser to process.
How you find out in real systems: distributed tracing records each step of a request across services (Chapter 30), and the browser's developer tools show the network part.
The lesson: don't guess. In the example above, making the database twice as fast saves 15 ms, while reusing the connection (no TCP or TLS handshakes) saves 80 ms.
Memory code: "Find the toll booth before you buy a faster car." Measure where the time goes, then fix the biggest piece.