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,
wwwshuffles, and link shorteners on your own domain. Test withcurl -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-Timingtells 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.
navigationStarttoresponseStartis your RUM TTFB — theweb-vitalsonTTFBcallback gives it to you for free. Segment it by CDN cache status if your provider exposes one (x-cachein 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?
Keep reading
Related articles from the SudoSecurity blog.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
PHP API Performance: N+1 Queries, cURL, and Connection Pools
A PHP API case study: fixing an N+1 query, twenty serial cURL calls, missing connection pooling, and stats recalculated on every request with Redis caching.
Inside TypeScript 7: What a Year-Long Rewrite in Go Actually Bought Developers
A deep look at how Microsoft's Go-based TypeScript 7 compiler achieves its speed gains, and what the Visual Studio Code team's year-long migration reveals about adopting it safely.
Node.js vs Deno vs Bun: Picking the Right JavaScript Runtime in 2026
Three JavaScript runtimes. Three different philosophies on what the developer experience should feel like. Here's how they compare on performance, ecosystem, tooling, and the only decision that actually matters for production.