Latency is the time a request takes. A service that answers correctly after thirty seconds feels broken, so speed belongs beside errors in your monitoring.
Time one request
curl can report where time went:
$ curl -s -o /dev/null -w 'dns=%{time_namelookup} connect=%{time_connect} tls=%{time_appconnect} first=%{time_starttransfer} total=%{time_total}\n' https://example.com/
dns=0.012204 connect=0.031518 tls=0.072341 first=0.140087 total=0.142310Each value is seconds measured from the start of the request, so they accumulate. The TCP connection step took about connect - dns, and the TLS handshake about tls - connect. A large gap between tls and first suggests that the server spent time preparing the response, though it also includes one network round trip.
Percentiles, not just averages
Suppose 99 requests take 0.1 seconds and one takes 10 seconds. The average is about 0.2 seconds, which describes nobody. Percentiles are clearer:
- p50 (median): half of requests are this fast or faster.
- p95: 95 percent are this fast or faster.
- p99: 99 percent are this fast or faster.
The slow end is called the tail. One page may need many requests, so tail delays are felt more often than you might expect.
Percentiles from a file
If you have one time per line in times.txt, sort numerically and pick a position:
$ sort -n times.txt | awk '{a[NR]=$1} END {i=int(NR*0.95); if (i<NR*0.95) i++; print a[i]}'
0.412This is the "nearest rank" method. Monitoring tools may interpolate, so their results can differ slightly.
To record times in nginx, add $request_time to your log_format. It is the time, in seconds, from reading the first bytes from the client until the log entry is written after the last bytes were sent. A slow client can therefore raise it.
Compare fairly
Keep failed and successful requests apart. An instant error can make latency look better during an outage. Compare the same endpoint, because a search and a static image have very different normal times.
Try it
Run the curl timing command ten times against a server you control, save each time_total to times.txt, and find the median and the largest value. The streams and text lab helps with the pipeline.
Check your understanding
Latency
All 3 sections · no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.