Skip to content

Lesson 4 of 14 · 20m

TTFB: Redirects, Handshakes, and Server Work

Time to first byte is a chain of redirects, handshakes, and server compute — the Server-Timing header makes the server say which link is slow.

When a user says your site is slow and every metric you control in the browser looks fine, the number to interrogate is TTFB — time to first byte. It is everything that happens before the first byte of HTML lands: redirects, DNS, TCP and TLS handshakes, your server's queue time, your server's think time, and the flight back. It is the first LCP sub-part, and every subsequent phase waits on it. A 1.5 s TTFB has already spent more than half of a 2.5 s LCP budget before the browser has parsed a single tag.

The wrong mental model is "TTFB means my server is slow." Sometimes it does. But TTFB is a chain, and the slowest link is often not the app at all.

The chain, in order

request ─┬─ redirects? (each hop = +1 RTT minimum)
         ├─ DNS lookup        (~20–200 ms cold, cached after)
         ├─ TCP handshake     (1 RTT)
         ├─ TLS handshake     (1–2 RTTs; HTTP/3 + resumption can hit 0)
         ├─ request flight    (1 RTT to your origin's region)
         ├─ server queue      (worker pool, not measured anywhere by default)
         ├─ server work       (auth, DB, template, serialize)
         └─ first byte flight (1 RTT back)

A mobile user in São Paulo hitting an origin in us-east-1 pays roughly 150–250 ms per round trip. Count the arrows. Redirects are the silent killer: http→https, apex→www, a tracking-link hop, a locale redirect — I have measured three redirects adding 700 ms before the origin was even contacted. Each one reruns DNS/TCP/TLS when the scheme or host changes.

Server-Timing: make the server confess

Browsers show you the total; they cannot show you where it went. The Server-Timing response header fixes that — your app reports its own phases and DevTools renders them in the Timing tab:

Server-Timing: auth;dur=4, db;desc="product+inventory";dur=61, render;dur=14, edge;desc="miss";dur=3, total;dur=82
// Express-style middleware sketch — stamp each phase
const t0 = performance.now();
res.locals.timing = [];
const mark = (name, start) =>
  res.locals.timing.push(`${name};dur=${(performance.now() - start).toFixed(1)}`);

let t = performance.now();
const product = await db.query(/* ... */);
mark("db", t); t = performance.now();
const html = render(template, product);
mark("render", t);
res.setHeader("Server-Timing", [...res.locals.timing, `total;dur=${(performance.now() - t0).toFixed(1)}`].join(", "));

Do it once per codebase and every slow endpoint starts explaining itself. The first time I added this to a slow product page, db was 480 ms of an 800 ms TTFB — a missing index on a WHERE slug = ? doing a full scan. Nobody would have found that by staring at Lighthouse.

What actually fixes TTFB

  • Kill redirects. Serve the canonical host directly; collapse migrations, www shuffles, and link shorteners on your own domain. Test with curl -sIL -o /dev/null -w '%{url_effective} %{num_redirects} %{time_total}\n' https://shop.northwindgear.com.
  • Cache HTML at the edge for anonymous content (see the caching lesson: s-maxage + stale-while-revalidate). A distant user's TTFB collapses from ~1,800 ms to ~150 ms when the edge answers instead of your origin.
  • Co-locate compute with data or users. The serial killer is an app server in Virginia making six sequential queries to a database in Virginia for a user in Jakarta. Either the compute moves to the user (edge functions hitting a regional read replica) or the chatty calls collapse into one query.
  • Cut the work. Profile the endpoint itself — slow ORM hydration, N+1 queries, a 400 KB JSON serialize, per-request re-auth against a remote session store. Server-Timing tells you which.
  • Keep connections warm. HTTP/2 and HTTP/3 multiplex on one connection; make sure your CDN terminates TLS at the edge and reuses an upstream connection, so origin handshake cost is paid once, not per request. HTTP/3 (QUIC) also removes a handshake RTT on resumption.

Field notes

  • Measure TTFB per region, not just per route. A 120 ms origin TTFB for you is a 1.4 s TTFB for the customer the CDN was supposed to help.
  • navigationStart to responseStart is your RUM TTFB — the web-vitals onTTFB callback gives it to you for free. Segment it by CDN cache status if your provider exposes one (x-cache in a custom response header you echo back).
  • Cold starts still exist: serverless functions behind low-traffic routes add hundreds of milliseconds sporadically. The p75 hides it; watch p95+ on the endpoint.
  • A fast TTFB on a redirect chain still ends slow — the browser paid for every hop.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.