Skip to content

Break down request latency with curl -w: DNS, TCP, TLS, and TTFB

Measure TTFB with curl and split one request into named phases. Use curl -w time_starttransfer and its sibling variables to prove whether slowness is DNS, TCP, TLS, server, or transfer.

“The site is slow” is not something you can act on. A single request travels through five distinct phases before the first byte of the page arrives, and any one of them can be the culprit. curl -w splits that request into named phases and prints a timer for each one, so instead of “slow” you can say exactly which phase is slow: DNS resolution, TCP connect, the TLS handshake, server think time (TTFB), or the download itself. That turns a vague complaint into a measurement you can hand to whoever owns the failing layer.

What you’ll accomplish

  • Print a per-phase timing breakdown of any HTTP or HTTPS request with a single curl command.
  • Read the cumulative timing variables and convert them into per-phase costs.
  • Isolate server latency (TTFB) from network and TLS overhead.
  • Measure redirect chains, force IPv4 or IPv6, and test a specific origin IP directly.
  • Know when a one-shot curl measurement is the wrong tool and what to reach for instead.

Prerequisites

  • curl 7.x or 8.x, which ships on macOS, every mainstream Linux distribution, and Windows 10/11. Check with curl --version.
  • The write-out timing variables have been stable for many years, so version differences barely matter here. The macOS system curl and a Linux curl print the same variable names and the same units.
  • One thing to keep front of mind: every timing variable is cumulative. Each value is the number of seconds elapsed since the request started, not the duration of that one phase. To get the cost of a single phase you subtract the previous variable from the current one. The whole guide hangs on that fact.

How it works

curl exposes an internal timeline through the -w (write-out) option. You ask for named variables wrapped in %{...}, and after the transfer finishes curl substitutes the measured value. The timing variables, in the order the request passes through them:

  • time_namelookup — DNS resolution is done. The hostname is now an IP address.
  • time_connect — the TCP connection to that IP is established (the three-way handshake finished).
  • time_appconnect — the TLS/SSL handshake is complete. On plain http:// this equals zero, because there is no TLS layer.
  • time_pretransfer — curl has finished any pre-request setup and is about to send the request. It sits a hair after time_appconnect.
  • time_starttransfer — the first byte of the response body arrived. This is TTFB (time to first byte), and the gap between this and time_pretransfer is the server’s think time.
  • time_total — the whole transfer is finished, last byte included.
  • time_redirect — the total time spent on all prior redirects in a chain, when you follow them with -L.

Because they are cumulative, the per-phase cost is always the difference between two adjacent variables. DNS cost is time_namelookup minus zero; TCP cost is time_connect minus time_namelookup; TLS cost is time_appconnect minus time_connect; server think time is time_starttransfer minus time_pretransfer; download time is time_total minus time_starttransfer.

Request timeline split into DNS lookup, TCP connect, TLS handshake, server processing to first byte, and content transfer, each mapped to its curl timing variable

Step-by-step

Build a reusable format file. Typing a long -w string every time is error-prone. Put the layout in a file once and reference it. Create curl-format.txt with this content:

     time_namelookup:  %{time_namelookup}s\n
        time_connect:  %{time_connect}s\n
     time_appconnect:  %{time_appconnect}s\n
    time_pretransfer:  %{time_pretransfer}s\n
       time_redirect:  %{time_redirect}s\n
  time_starttransfer:  %{time_starttransfer}s\n
                      ----------\n
          time_total:  %{time_total}s\n

Run the request against the format file. The @ tells curl to read the write-out layout from the file. Send the body to /dev/null so it does not flood your terminal, and use -s to silence the progress meter:

curl -w "@curl-format.txt" -o /dev/null -s https://example.com
     time_namelookup:  0.021340s
        time_connect:  0.058216s
     time_appconnect:  0.134492s
    time_pretransfer:  0.134620s
       time_redirect:  0.000000s
  time_starttransfer:  0.267901s
                      ----------
          time_total:  0.312774s

Or keep it to one line. When you do not want a file lying around, inline the same variables. This is handy inside scripts or over SSH:

curl -o /dev/null -s -w "dns=%{time_namelookup} conn=%{time_connect} tls=%{time_appconnect} ttfb=%{time_starttransfer} total=%{time_total}\n" https://example.com
dns=0.021340 conn=0.058216 tls=0.134492 ttfb=0.267901 total=0.312774

Follow and measure redirects. Add -L to follow redirects, and time_redirect will report how long the chain cost before the final response. Compare it against time_total to see how much of the wait was pure redirecting:

curl -L -o /dev/null -s -w "redirect=%{time_redirect} starttransfer=%{time_starttransfer} total=%{time_total} num=%{num_redirects}\n" http://example.com
redirect=0.181204 starttransfer=0.402551 total=0.447830 num=2

Here two redirects ate 0.181s before the request that actually served content even began. If you want the full accounting of a redirect chain per hop, see auditing a redirect chain with curl.

Force IPv4 or IPv6. If DNS returns both an A and an AAAA record, curl picks one, and the two paths can differ a lot. Pin each with -4 and -6 and compare:

curl -4 -o /dev/null -s -w "v4 connect=%{time_connect} ttfb=%{time_starttransfer}\n" https://example.com
curl -6 -o /dev/null -s -w "v6 connect=%{time_connect} ttfb=%{time_starttransfer}\n" https://example.com
v4 connect=0.058216 ttfb=0.267901
v6 connect=0.061880 ttfb=0.271044

Compare cold versus warm DNS. The first lookup pays for the recursive resolver; a second lookup often hits a cache. Run the same request twice back to back and watch time_namelookup:

curl -o /dev/null -s -w "cold dns=%{time_namelookup}\n" https://example.com
curl -o /dev/null -s -w "warm dns=%{time_namelookup}\n" https://example.com
cold dns=0.048112
warm dns=0.004302

A cold lookup that stays high on every run points at the resolver, not at caching.

Test a specific origin IP directly. To measure the origin behind a CDN or load balancer without changing DNS, use --resolve to map the hostname to an IP for this request only. TLS and the Host header still use the real hostname, so virtual hosting and certificates keep working:

curl --resolve example.com:443:203.0.113.10 -o /dev/null -s -w "origin ttfb=%{time_starttransfer} total=%{time_total}\n" https://example.com
origin ttfb=0.198744 total=0.240310

Compare that TTFB against the CDN-fronted number to see how much the edge is adding or absorbing.

Validate it works

The output gives you each phase in seconds as a cumulative timer. To validate the breakdown, subtract adjacent values and confirm the pieces add up to time_total. Using the first run above:

  • DNS: 0.021 − 0 = 0.021s
  • TCP connect: 0.058 − 0.021 = 0.037s
  • TLS handshake: 0.134 − 0.058 = 0.076s
  • Server think time (TTFB): 0.268 − 0.135 = 0.133s
  • Download: 0.313 − 0.268 = 0.045s

Those five add to roughly time_total. The single most useful subtraction is time_starttransfer minus time_appconnect (or time_pretransfer): it isolates server latency from everything the network and TLS did before it. If that number is large while every earlier phase is small, the connection is fine and the backend is the problem.

Troubleshooting

  • High time_namelookup. DNS is slow. The recursive resolver is far away, the authoritative nameservers are slow, or a record has an aggressive TTL that forces a fresh lookup every time. Test against a different resolver (for example by switching the machine’s DNS) and re-measure. If a warm lookup is fast but a cold one is always slow, the authoritative side is the suspect.
  • High time_connect. The TCP handshake is slow, which is almost always network distance or routing. This is roughly one round-trip time to the server, so a large value means high RTT, a congested path, or a firewall adding delay. Compare -4 against -6, and try --resolve against a closer edge.
  • High time_appconnect. The TLS handshake is expensive. Look at certificate chain length (extra intermediates cost round trips), OCSP or CRL checks that block the handshake, an outdated TLS version negotiating down, or a missing session resumption. On plain HTTP this variable is zero, so a jump here is purely a TLS story.
  • High time_starttransfer with a fine connect. Everything before TTFB is quick but the first byte is late. The backend is thinking too long: a slow database query, an uncached page render, a cold application worker, or an upstream dependency the app is waiting on. This is the case where curl points squarely at the server and away from the network.
  • time_total far larger than time_starttransfer. The server started responding promptly but the body took a long time to arrive. That is a slow or large download: an oversized uncompressed payload, a throttled connection, or a slow disk or stream on the origin. Check the response size and whether compression is on.
  • Keepalive skew when reusing connections. If you reuse an already-open connection (for example a second request in the same curl invocation, or an HTTP/2 stream), time_namelookup, time_connect, and time_appconnect can all read as near zero because that setup already happened. That is correct, not a bug, but it means the numbers describe a warm connection. To measure a genuine cold request, run a fresh curl each time.

When this is the wrong approach

A single curl request is a spot check from one machine at one moment. It is excellent for attribution (“which phase is slow right now”) and terrible at anything statistical. It is not real-user monitoring: it does not measure what your visitors experience across networks, devices, and geographies. It is not load testing: one request tells you nothing about behavior under concurrency. And it measures fetch time only, not render time. A browser waterfall or RUM (real user monitoring) captures DOM parsing, render-blocking resources, and paint, which curl never sees. When you need percentiles over time or a picture of real traffic, reach for a proper monitoring tool and use curl to diagnose the specific slow phase it flags. For a worked example of chasing TTFB through a full application stack, see measuring WordPress TTFB across its lifecycle. The full list of write-out variables lives in the curl manual and the write-out chapter of Everything curl.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.