Foundations 0/8

Lesson 1 of 8

How the web works

Trace a browser request through DNS, a secure connection and HTTP, and see where time is spent.

1,603 words · 8 min read

How a browser turns a URL into a response

A web request asks a server to perform an operation or return a representation, such as a product page. The browser starts with a URL, but sending that request also requires a destination address and a connection. The elapsed time you observe includes whichever preparation steps the browser still needs, followed by the server’s work and the response transfer.

Every machine reachable on the internet has an IP address, such as 203.0.113.10, and each network service on that machine listens on a numbered port: 443 for HTTPS, 80 for plain HTTP. TCP turns individual packets into an ordered, reliable byte stream by numbering bytes and retransmitting anything lost. UDP delivers independent datagrams with no ordering or retransmission, leaving those duties to the protocol built on top of it.

A page load stacks several protocols on these pieces. DNS maps a hostname to an IP address. TCP connects to a port at that address, and TLS authenticates the server and encrypts the stream for HTTPS. HTTP describes the request and response carried over the connection. A failure in any required step can prevent the page from arriving, even when the application code is working.

Caching changes which work remains. A cached DNS answer removes lookup work. Reusing a connection removes setup work, while a fresh response already stored in the browser can avoid contacting a server at all. A request can reuse any combination of these, which is why a refresh may be faster and why one kind of cache cannot repair every failure.

Many sites terminate browser connections at a CDN edge: a proxy server near the user that answers from its own cache or forwards the request to the origin, the application servers that own the data. Consider opening https://shop.example/products/42 for the first time, with an 80 ms round trip between the browser and the edge. DNS takes 40 ms. The TCP handshake takes one round trip (80 ms) and the TLS 1.3 handshake another (80 ms). Sending the request and receiving the first byte of the reply takes a third round trip plus 5 ms of work at the edge (85 ms). The edge’s exchange with the origin, including the origin’s own work, takes 120 ms, and transferring the response body takes 20 ms.

InputDNS 40; TCP 80; TLS 80; request to edge 80 + 5 edge work; edge to origin 120; body transfer 20 (ms)
Output425 ms before the browser holds the complete response

DNS, TCP and TLS: finding and securing the server

A URL separates the scheme (https), hostname (shop.example) and path (/products/42). The hostname identifies the service the browser needs to reach. DNS answers address queries about that name; it has no role in selecting product 42 or running the shop’s application.

The browser or operating system may already hold an unexpired answer. Otherwise a recursive resolver obtains one, using its own cache or contacting DNS servers. Each answer carries a time to live (TTL). That lifetime reduces repeated lookup work, but it also means an address change may take time to reach clients that still hold a valid old answer.

After obtaining an address, a new HTTP/2 connection first completes the TCP handshake: one round trip before any application data can flow. TLS 1.3 then establishes encryption keys and authenticates the server for the requested hostname in one more round trip; the older TLS 1.2 needed two. Certificate validation is the part that proves identity: reaching the right IP address does not establish that the peer may serve this hostname.

A later request can reuse an open connection and skip both handshakes. HTTP/2 carries many concurrent request streams on one TCP connection, but because TCP delivers one ordered byte stream, a single lost packet delays every stream behind it. HTTP/3 runs over QUIC, which is built on UDP: it combines transport and TLS 1.3 setup into one round trip and recovers each stream independently, so a lost packet delays only the stream it belonged to.

Trace a request’s timeline

Which stages does each kind of reuse remove, and which failures can a cache hide? Compare the cold 425 ms request with a cached DNS answer, a reused connection, an edge cache hit and a fresh browser copy, then fail DNS, TLS or the origin under each.

  1. Browser cache Waiting
  2. DNS lookup Waiting
  3. TCP connection Waiting
  4. TLS handshake Waiting
  5. HTTP request to edge Waiting
  6. Origin application Waiting
  7. Response body Waiting
Elapsed
0 ms
Request result
Ready to send

Advance to inspect each stage. Changing a setting starts a new attempt.

Illustrative HTTP/2 over TCP, full TLS 1.3, serial stages and a small public response. No retries, validation, redirects, TLS resumption or page rendering.

At an 80 ms round trip, a cached DNS answer saves 40 ms. An open connection saves DNS plus both handshakes, 200 ms. A fresh edge response removes the 120 ms origin exchange but still needs the connection to the edge, so a cold request served from the edge takes 305 ms. A fresh browser copy needs only a local read of about 2 ms.

A cache can bypass a dependency only when it holds a usable response: a fresh entry that the response’s cache policy allows that cache to store. A miss, an expired entry that must be revalidated, or a private response sends the request on to the origin, so an origin outage still reaches those users.

HTTP requests, responses and cache headers

HTTP gives the request its application meaning. A GET /products/42 asks for a representation of a resource; a POST /orders asks the server to process a submission and may create an order. Headers carry information such as accepted content types, credentials and cache conditions. HTTPS protects the exchange in transit, while the application must still check which account may perform the operation.

Every response starts with a status code whose first digit gives its class. 2xx means success, such as 200 OK or 201 Created. 3xx redirects the client elsewhere, or with 304 Not Modified tells it to reuse its stored copy. 4xx reports a problem with the request, such as 404 Not Found or 429 Too Many Requests. 5xx reports a server-side failure: 502 Bad Gateway when a proxy received an invalid response from the server behind it, 503 Service Unavailable when the server cannot handle the request right now, and 504 Gateway Timeout when a proxy gave up waiting for that server.

The browser’s connection may end at a CDN edge or another reverse proxy. The edge can answer with a fresh, permitted cache entry or forward a miss to the origin, which may read a database before returning a status, headers and body. In the example, the 120 ms covers the whole edge-to-origin exchange, so adding another round trip for that same exchange would count the work twice.

Response cache directives express different promises. private permits the browser’s own cache to store the response but prohibits a shared cache from doing so. no-cache permits storage but requires successful validation with the server before each reuse. no-store prohibits storage. A public product description and an account balance need different policies because the harm from stale or shared data differs.

Validation can save response bytes while still requiring a network exchange. The browser keeps a representation and its entity tag (ETag), then sends a conditional request with If-None-Match. A 304 response lets it reuse the stored body. That still costs a round trip to the validating server, which is what separates it from a fresh local cache hit.

Locate the failure before changing the application

Different request stages need different evidence
ObservationLikely stage to inspectUseful evidence
Hostname cannot be resolvedDNS lookupResolver result, cached answer and its TTL
Connection refused, timed out or certificate errorTCP or TLSConnection timing, port, certificate hostname and expiry
HTTP 502, 503 or 504 from the edgeProxy or the origin behind itStatus, request id, edge and origin logs
HTTP 200 with another account’s dataApplication or cache policyAuthentication, cache key and response headers
Fast first byte, slow pageBody transfer or browser workTransferred bytes, subresource waterfall and main-thread work

An HTTP error means some HTTP server answered. A DNS error can occur before your application sees a request at all. Check the browser’s network waterfall against the server trace rather than inferring the cause from the total duration.

What stays true

Each request pays for the work its current state requires. A cached address, a reusable connection and a cached response eliminate different stages.

Assumptions that send an investigation to the wrong service

  1. Treating DNS caching as page caching.

    Where it breaks
    The hostname resolves immediately, but the client still opens a connection and fetches the response.
    The fix
    Track address caching, connection reuse and response caching separately.
  2. Adding the full handshake cost to every HTTP/2 request.

    Where it breaks
    The estimate overstates latency when requests share an already open connection.
    The fix
    State whether the request uses a new or existing connection and inspect connection timing.
  3. Caching a personalized response under only its URL.

    Where it breaks
    A shared cache may return one user’s data to another user who requests the same path.
    The fix
    Mark the response private or no-store, or build any permitted shared cache key from everything the response varies on.
  4. Calling a received response a fully loaded page.

    Where it breaks
    The HTML still references scripts, images and styles, each a further request, followed by browser rendering work.
    The fix
    Measure request latency, body transfer and page rendering separately.

Follow the request into the rest of the system

Related topics

Further reading