LOAD-BEARINGfree
FoundationsTier 1 · unit 1 of 6 · 12 min read

DNS, TCP, TLS: three trips before hello

In Tier 0 we said "the connection opens" and moved on. Now let's open the hood, because before your application code runs even once, the network has already spent a shocking amount of time — and most engineers optimising "slow APIs" are looking in completely the wrong place.

DNS: the phone book lookup

Your phone asks: "what's the IP address for api.example.com?" The question travels through a chain: your recursive resolver (usually your ISP or 8.8.8.8) asks the root servers ("who handles .com?"), then the .com servers ("who handles example.com?"), then example.com's authoritative server ("what's api's address?"). Sounds slow — and cold, it is: 20–120 ms. But every layer caches the answer for a duration you control called the TTL (time to live). Warm, the lookup costs approximately zero.

DNS is secretly also a load balancing tool. Give different answers to people in different places — a Mumbai IP to Indian users, a Virginia IP to American ones — and you've built global routing without touching a single server. That's GeoDNS.

The catch is the TTL dial. Set it low (say 60 seconds) and when a server dies you can point users elsewhere within a minute — fast failover — but everyone re-asks the phone book constantly. Set it high (say a day) and lookups are nearly free, but clients keep hammering a dead IP for up to a day after you've removed it. TTL is a failover-speed dial, and choosing its value is a real design decision, not a default to ignore.

TCP: "can you hear me?"

Address in hand, your phone opens a TCP connection with the famous three-way handshake: it sends SYN ("want to talk?"), the server replies SYN-ACK ("yes, and I hear you"), the phone confirms ACK ("great, I hear you too"). That's one full round trip across the internet before any data moves — pure overhead, paid every time a fresh connection opens.

It gets worse: a new TCP connection starts cautious. A mechanism called slow start begins by sending only a little data, then ramps up as it confirms the network can handle more. For short connections, you never reach full speed — like a car that's only ever driven one block, never getting out of first gear. Hold that thought; it explains the single most important fix below.

TLS: agreeing on a secret code

Now encryption. The two sides must agree on keys nobody in the middle can steal. TLS 1.2 needed two more round trips for this negotiation. TLS 1.3 — use it — cut that to one, and to zero when reconnecting to a server you've talked to recently (called 0-RTT resumption).

Add it all up

On a link where one round trip takes 60 ms: DNS (~60) + TCP (~60) + TLS (~60) ≈ 180 ms before your server sees byte one. Your beautifully optimised 30 ms endpoint is wrapped in 180 ms of ceremony. This is why the fixes below matter more than most server-side tuning:

  • Keep-alive and connection pooling. Pay the handshakes once, then reuse the open connection for hundreds or thousands of requests. Most "our API is slow" investigations end here: the client was opening a fresh connection per call, paying 180 ms of tax every time. Reuse also fixes slow start — a long-lived connection has already warmed up to full speed.
  • TLS 1.3 with session resumption. A config change that deletes round trips. Free latency.
  • Terminate TLS at the edge. Put a server 20 ms from the user (a CDN point of presence) that handles the handshakes, then relays traffic to your origin over its own already-warm connection. The expensive ceremony happens over a short distance instead of a long one.

TCP vs UDP: the reliability trade

TCP promises every byte arrives, in order. The price: if packet #5 is lost, packets #6–#100 wait while #5 is re-sent — head-of-line blocking. For a file download, fine. For a live video call? Disaster: you'd rather skip the lost frame than freeze the whole stream waiting for stale data. That's what UDP is for — no promises, no waiting, right choice when late data is worthless: live video, voice, multiplayer games. And QUIC (next lesson) is the clever middle path: reliability rebuilt on top of UDP, but per-stream, so one loss doesn't stall everything.

The trap

Optimising server-side p99 from 40 ms to 30 ms while every client pays 180 ms of connection setup per call. You cannot see this from server logs — the server's clock starts after the handshakes. Measure the whole path, from the device, or you'll optimise the wrong 15% forever.