On a fresh connection with TLS 1.2, an API call across a path with a 60 ms round trip spends about 180 ms on handshakes before the request is even sent. Your handler might answer in 5 ms, and the client will still wait a quarter of a second for the response.
That gap is why client side and server side numbers rarely agree. The client measures API latency from the moment it starts the call. The timer in your handler starts only when the request arrives. Before that, the client had to resolve a name, open a TCP connection, finish a TLS handshake and push the request through whatever proxies and load balancers sit in front of the app. A load balancer can also hold a request in its own queue while it waits for a free upstream connection.
A good first measurement is the time to first byte of a plain HTTPS request to the same endpoint. Look at the first call and at a second call on the same connection. If the second one is much faster, the cost is in connection setup. If both are slow, compare what the client saw with what the server logged.
DNS: the lookup you forget about
Before connecting, the client needs an IP address. It may already sit in a cache inside your HTTP library, the language runtime or the OS. On a miss, the client asks a resolver, which may answer from its own cache or walk the chain of authoritative servers. A name that points to a CNAME adds another record to that chain, and each record in it has its own TTL.
The TTL tells caches how many seconds they may keep a record. If you plan to move an API to a new address, lower the TTL well in advance: resolvers that cached the old record keep it for the old TTL, not the new one. Keep the old address serving traffic for a while after the switch as well. Some resolvers serve stale records when the authoritative servers are unreachable, so an expired TTL does not guarantee every client has moved.
To check the DNS lookup on its own, run dig on the machine where the client runs, with your API hostname:
dig api.example.com A +noall +comments +answer +stats dig api.example.com AAAA +noall +comments +answer +stats The answer section shows the addresses and their TTL. If the answer came from a cache, that TTL is the time left, not the original value. The Query time line in the stats tells you how long dig waited for the resolver. Without an explicit server, dig uses the resolvers from /etc/resolv.conf, while your application may resolve names differently: through /etc/hosts, an NSS module, a local cache or the runtime's own resolver with its own caching rules. A fast dig answer only tells you the system resolver is fast.
Check both record types. A client that gets an AAAA record will usually try IPv6 first, and a broken IPv6 path can add a timeout before the fallback to IPv4.
To take DNS out of the picture entirely, pin the address with curl's --resolve option. The URL keeps the hostname, so curl still sends the right SNI and verifies the certificate for your domain:
# use the IP from a normal run curl -q -4 --noproxy '*' --http1.1 \ --resolve api.example.com:443:203.0.113.10 \ -fsS --max-time 15 -o /dev/null \ -w 'dns=%{time_namelookup} ip=%{remote_ip}\n' \ https://api.example.com/health If the pinned run is fast and the normal one is slow, look at the resolver. If normal runs keep landing on different IPs, you may be talking to different CDN nodes depending on which resolver answered.
TCP and TLS: round trips before the first byte
Opening a TCP connection takes one round trip: the client sends SYN, gets SYN-ACK back and can send right after its ACK. Then the TLS handshake starts. Client and server agree on cipher parameters, the client checks the certificate, and only after that does the HTTP request leave.
A full TLS 1.2 handshake usually needs two round trips on top of TCP. TLS 1.3 cuts that to one. If the server wants a different key exchange group and replies with HelloRetryRequest, add one more. Certificate checks and lost packets can add their own delays. HTTP/3 works differently: it runs over QUIC, which sets up transport and TLS together in a single exchange. To see which TLS version your server actually negotiates, run curl -v and look for the line that starts with "SSL connection using".
Session resumption helps when a client comes back after the connection closed. With TLS 1.3, resumption skips most of the certificate work, but without early data the client still waits for TCP and one TLS round trip. Early data (0-RTT) removes that wait, at a price: the server may receive the same request twice. Anything that moves money or changes state should not go out as early data.
Connection reuse removes both handshakes from every call after the first. In HTTP/1.1 that is plain keep-alive. HTTP/2 goes further and runs many requests over one connection at once. The most common way to lose this benefit by accident is an HTTP client created inside a request handler: many libraries give each client instance its own pool, and every incoming request opens a fresh connection to the upstream API. Even with a long lived client, check idle timeouts. If calls are rare, a load balancer can close the socket between them, and the next call pays for a new handshake.
You can see whether a connection is reused by giving one curl process the same URL twice:
url=https://api.example.com/health curl -q -4 --noproxy '*' --http1.1 -fsS --max-time 15 \ -w 'code=%{http_code} new=%{num_connects} ttfb=%{time_starttransfer}\n' \ -o /dev/null "$url" -o /dev/null "$url" For the second request, new should be 0 and ttfb should drop. That difference is roughly what every cold call costs.
Routing: same IP, different path
Routing decides how packets get from the client to your server, and it depends on how networks exchange traffic with each other. The same IP can be close for one carrier and far for another, and the return path does not have to match the forward one. Many CDNs use anycast: one IP is announced from several places, and clients in different networks land on different nodes. Record the IP from curl output, and if the service returns a node or POP identifier in a header, record that too.
Round trip time is how long a packet takes to reach the destination and come back. A new HTTPS connection waits for the server several times: for the SYN-ACK, for the TLS messages, then for the first byte of the response. So the path latency is already inside several curl timings. Do not add it as a separate line in your budget.
If one location connects slowly, run traceroute and mtr from that machine to the IP you saw in curl. TCP probes to port 443 follow a path closer to real traffic than ICMP, though it can still differ. On Linux, TCP probes usually need root.
ip=203.0.113.10 sudo traceroute -n -T -p 443 "$ip" sudo mtr -n -r -c 30 -T -P 443 "$ip" Read the last hop first: the latency to the destination itself is the number that matters. Then find the hop where the extra delay appears and stays on every hop after it. A router in the middle that shows loss while later hops look clean is usually just rate limiting replies to probes, not dropping your traffic. A star means one probe got no answer in time. Replies from different hops can also come back over different paths, so a single line in the trace rarely proves where the delay is.
If TLS terminates at a CDN, the trace only shows the path to the edge. The CDN may then fetch from your origin, and that time lands in the time to first byte as well. Check whether the response was served from cache. Run the trace once while requests are slow and once after latency returns to normal. If you take the issue to a provider, attach both traces, the destination IP and the time in UTC.
Measuring each stage with curl
For curl timing, pick an HTTPS endpoint with a small response and a stable 200. Run the same command from both locations you want to compare. The first series keeps IPv4 and HTTP/1.1 as set in the command. If you are chasing a problem that a client sees over IPv6 or HTTP/2, run a separate series with their settings. The command also ignores proxy settings, so if your users go through a proxy, repeat the run with it.
url=https://api.example.com/health fmt='code=%{http_code} http=%{http_version} new=%{num_connects}\n' fmt="${fmt}"'ip=%{remote_ip} dns=%{time_namelookup}\n' fmt="${fmt}"'tcp=%{time_connect} tls=%{time_appconnect}\n' fmt="${fmt}"'ready=%{time_pretransfer} first=%{time_starttransfer}\n' fmt="${fmt}"'total=%{time_total} bytes=%{size_download}\n' date -u +%FT%TZ curl -q -4 --noproxy '*' --http1.1 \ -fsS --max-time 15 -o /dev/null -w "$fmt" "$url" All write-out timings are in seconds and count from the start of the request, so each stage is a difference between two of them. This holds for a successful request on a new direct connection without redirects:
| Stage | From the output |
|---|---|
| DNS lookup | dns |
| TCP connect | tcp − dns |
| TLS handshake | tls − tcp |
| Ready to send | ready − tls |
| Waiting for the first byte | first − ready |
| Downloading the body | total − first |
The wait between ready and first covers the request travelling to the server, the server doing its work and the first byte travelling back. To see how long the handler itself took, find the same request in server logs or traces. If the first byte arrives quickly but the call ends much later, look at the response size and transfer speed.
Run each location several times. Use a separate curl process for every run and keep its time, status code, IP and body size. Compute the stage differences per request, then compare medians per stage and count errors and timeouts separately. Before comparing times, check the response body once from each machine: a redirect, a bot challenge or an error page served with a 200 will skew everything. A fresh curl process starts with an empty DNS cache of its own, but the OS and the resolver may still have the answer.
So what do you fix first when an API keeps blowing its latency budget? We would go in this order. Start with connection reuse: it is usually the cheapest change and removes whole round trips from every call. Next, move to TLS 1.3 and make sure resumption works. Then look at DNS, at TTLs and caching in the client. Server location comes last. Moving the server closer only pays off when the handler is already fast and the remaining time is pure distance, and even then compare candidate locations from the networks where the problem shows up. A new region can help users on one carrier and change nothing for another.