Latency 00:00:01 Follow one round trip A phone takes a moment to build a request, then another moment to show the result. A database query somewhere in the middle takes only a few milliseconds. Where did the rest of the customer's wait go? The phone needs 2 ms to build the request. 20 ms across the network. This is the largest single part. 1 ms to choose a server. 15 ms of work in our code. 10 ms for the query. The answer exists after 48 ms. 1 plus 20 plus 2 ms. Total 71 ms. Measure from the caller as well as the server. Network travel, connection setup and queueing can all be invisible to a timer wrapped solely around the query. 00:00:48 Which person does the average describe? 100 requests: 90 took 10 ms, 10 took 410 ms. Sort them and point at a rank. Ranks 1 to 90 hold 10 ms. Ranks 91 to 100 hold 410 ms. Rank 50: 10 ms. The typical request is fast. Rank 90 is the last of the fast ones. Rank 95 is already in the slow group. Rank 99: 410 ms as well. Ninety requests took ten milliseconds and ten took four hundred ten. Their average is fifty milliseconds. None of these hundred requests actually took that long. Sort observations to define percentiles. The ninety-ninth percentile is a threshold in that sample, not the finishing time that every hundredth request is destined to experience. 00:01:41 The first connection pays extra One HTTPS request with a 15 ms round trip, on a new and on a kept connection. Find the address. A warm client remembers it. One round trip to open the connection. In this illustrative full handshake, establishing security takes one more network round trip. Protocol version, resumption and transport choices can change that cost. 15 ms of travel and 20 ms of work until the first byte. The rest of the answer. 85 against 50 ms. Reuse can avoid name resolution and handshake work associated with a cold connection. A new network or an expired connection can bring those costs back. 00:02:24 Waiting for the last parallel result A page waits for several answers. Each one is slow 1% of the time. Suppose each independent backend call is slow with probability one percent. A page waiting for one call then faces the same one-percent risk. For three independent calls, the chance all are fast is ninety-nine percent cubed. Subtract that from one and the chance of at least one slow call is about three percent. At ten calls, the chance of waiting on at least one slow response reaches about 9.6 percent. At a hundred independent calls, the risk is about 63.4 percent. When a page waits for every one of many independent calls, one slow dependency determines completion. Independence matters when calculating that probability; common failures can slow many calls together. 00:03:13 Near saturation, work waits its turn The same 10 ms of work on one server that gets busier. Simplest queueing model. Take the simple single-server queue model with exponentially distributed arrivals and service times. A mean service time of ten milliseconds at half utilization gives a mean response time of twenty milliseconds. At seventy percent utilization the same model predicts about thirty-three milliseconds on average. At eighty percent it predicts fifty milliseconds. At ninety percent it predicts a hundred milliseconds, ten times the mean service time. At ninety-five percent utilization it reaches two hundred milliseconds. Most of this response time is waiting to start. Separate execution time from time waiting for an available worker. Keeping requests queued for longer can make latency much worse without making the useful computation any harder. 00:04:09 One expensive request type contaminates the tail P50 is 40 ms and calm. P99 jumped to 2 seconds after Tuesday's release. After the release, the average barely moves, but the ninety-ninth percentile jumps. Of 2,000 ms, 1,900 is waiting for a database connection. The release added a report that holds connections for seconds. Give the report its own small pool, and a timeout. P99 returns to 180 ms. Separating work into bounded pools can protect short requests from long reports. Check that the expensive reports still have enough capacity to finish under their own deadlines. Follow the critical path, pay attention to the users in the slow tail, and spend the next optimization on the stage that actually contributes their time.