Course contents

02. Networking and Internet Lesson 5 of 10

HTTP Request Lifecycle

Follow one HTTPS request from DNS, TCP and TLS through a CDN edge, load balancer, app server and database, and see where the milliseconds go.

14 minBeginner9 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • A new HTTPS connection costs a DNS lookup, one TCP round trip and one TLS 1.3 round trip before the request is sent.
  • Round trips scale with distance, so terminating connections at a nearby edge saves more than faster servers do.
  • Keep-alive reuses connections; HTTP/2 multiplexes requests on one connection; HTTP/3 uses QUIC over UDP.
  • At light load network time dominates a request; near capacity, queueing in the servers takes over.
  • Cut setup round trips, then request round trips, then queueing, then server work.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

The request lifecycle is everything between "the browser wants a URL" and "the response has arrived": resolving the name, opening and securing a connection, sending the request through every proxy between the user and the data, doing the work, and carrying the answer back. It is the classic "what happens when you type a URL" question, and the clearest way to see where latency comes from.

Why it exists

HTTP is a request/response protocol on top of a reliable, encrypted byte stream. That stream does not exist until both sides set it up, and setting it up takes round trips. Behind the public address there are usually several layers (CDN, load balancer, app servers, databases), each added for a reason: caching, spreading load, isolating failures. Each one adds hops. Knowing the lifecycle tells you which costs are fixed per connection, which are per request, and which depend on load.

How it works

  1. DNS lookup. The browser resolves the hostname. From caches this costs nothing or one short round trip to the resolver; cold it can cost 100 ms or more. CDNs answer with the address of a nearby edge.
  2. TCP handshake. SYN, SYN-ACK, ACK. The client may send data right after its ACK, so a new connection costs one round trip.
  3. TLS handshake. With TLS 1.3, the ClientHello carries a key share, the server answers with its ServerHello, certificate and Finished, and the client can send the request with its Finished: one round trip (TLS 1.2 needed two). A resumed session can send 0-RTT early data, but that data can be replayed, so it is only safe for idempotent requests.
  4. The request. GET /products travels to the edge. If the edge has a fresh cached copy it answers immediately. Otherwise it forwards the request over a warm, long-lived connection to the origin, where a load balancer (or reverse proxy) picks an app server. The app server runs the handler, which queries the database, renders a response, and sends it back along the same path.
  5. The response and the connection afterwards. The connection stays open (keep-alive, the default since HTTP/1.1), so the next request skips steps 1–3. HTTP/1.1 sends one request at a time per connection, so browsers open about six connections per host. HTTP/2 multiplexes many concurrent requests as streams over one TCP connection. HTTP/3 runs over QUIC on UDP: the transport and TLS handshakes are combined into one round trip, and a lost packet only stalls its own stream rather than every request on the connection (TCP head-of-line blocking).

A worked example from the lesson, with the user 10 ms from the edge and the origin 35 ms beyond it: DNS 20 ms + TCP 20 ms + TLS 20 ms of setup, then about 120 ms for the request (20 ms to the edge and back, 70 ms edge to origin and back, about 5 ms in the load balancer and internal hops, about 25 ms of app work including a 5 ms query). Without the edge, the two handshakes would go to the origin, 90 ms away per round trip: 180 ms instead of 40.

When to use it

Use the lifecycle as a checklist whenever latency matters: designing a public API, choosing between regions, deciding what to cache at the edge, planning a page's critical requests, or explaining a slow time to first byte. In interviews it is the backbone for any "how does a request reach your service" discussion.

When not to

Do not optimise by guesswork from the diagram. Measure: browser waterfalls, server timing headers and distributed traces show which step actually dominates. And not every system has every layer. An internal service-to-service call usually skips DNS (cached), TLS setup (long-lived pooled connections) and the CDN entirely.

Trade-offs

Choice You gain You pay
CDN edge in front of dynamic traffic Handshakes near users, warm origin connections, edge caching Another layer to configure, cache invalidation, cost
Long-lived connections and pooling No setup per request Memory and file descriptors per connection; balancing long-lived connections is harder
HTTP/2 Many requests on one connection, header compression One lost TCP packet stalls every stream
HTTP/3 (QUIC) 1-RTT setup, no TCP head-of-line blocking, connection migration UDP blocked on some networks (fallback needed), more CPU in user space
TLS 1.3 0-RTT Saves a round trip on resumed connections Replay risk; only for idempotent requests

Alternatives

  • Fewer requests: bundling, inlining critical resources, or a single API call that returns everything a screen needs (a backend for frontend) removes round trips altogether.
  • Edge compute: run the handler at the edge so dynamic responses avoid the trip to the origin; the data must then be replicated or cached near the edge.
  • Preconnect and DNS prefetch hints: the browser starts DNS, TCP and TLS before the request is needed.
  • Long-lived channels (WebSockets, server-sent events) for many small messages, so setup is paid once.

Failure modes

  • Redirect chains: every redirect is another full round trip, sometimes to a new host with its own DNS, TCP and TLS.
  • Connection churn: clients or proxies that close connections after every request pay full setup each time and can exhaust ephemeral ports.
  • Cold caches after a deploy or purge: edge misses send every request across the long distance to the origin at once.
  • Queueing under load: near capacity, waiting for a free worker or database connection dominates latency; timeouts and retries can then amplify the load.
  • Slow handshakes: expensive certificate chains, OCSP checks or a far-away TLS terminator add to every new connection.

In production

Terminate TLS at the edge or the closest load balancer, keep connections to backends pooled and warm, enable HTTP/2 and HTTP/3, and cache everything that can be cached at the edge. Watch time to first byte broken down by phase (browser Navigation Timing and server timing headers), and trace requests through each hop so you can see which layer adds latency. Keep enough capacity headroom that requests do not queue at peak.

Common misconceptions

  • "Bandwidth makes the web fast." For typical requests, the number of round trips and the distance matter far more than bandwidth.
  • "An extra proxy hop always makes things slower." A nearby edge that terminates TCP and TLS usually makes first requests much faster.
  • "HTTP/2 makes every request faster." It removes waiting in line on the client side; on lossy networks a single TCP connection can be worse, which is what HTTP/3 addresses.
  • "Server time is what users wait for." At light load it is often a minority of the total.

Interview angle

Walk through DNS, TCP, TLS and the request path in order, naming the round trips and putting rough numbers on them. Then show you know where to cut: CDN edges, connection reuse, HTTP/2 and HTTP/3, caching, fewer round trips. Mentioning TLS 1.3's one round trip, 0-RTT's replay caveat and TCP head-of-line blocking shows depth; connecting latency under load to queueing shows systems thinking.

Model assumptions

The lesson mixes two kinds of animation. The DNS lookup, TCP and TLS handshakes, the no-reuse and keep-alive comparisons and the HTTP/2 example are scripted: hand-placed packets with illustrative timings (10 ms one way to the edge, 1 ms processing per hop). The engine does not model TCP, TLS, packet loss or congestion control. The request path (browser → CDN edge → load balancer → app servers → database) is simulated by the queueing engine: 35 ms one way from edge to origin, an edge that forwards every request (no edge caching in the simulation) with about 1 ms of work, two app servers with 2 workers and a 20 ms mean service time each (lognormal, cv 0.4) that also wait for a 5 ms database query, round-robin balancing. Arrivals are evenly spaced at first and random (Poisson) from the "Under load" step on. Numbers show behaviour and orders of magnitude, not benchmarks.

The lesson as textEvery step of the simulation, written out.
  1. 1. First, find the address

    The user opens https://shop.example.com/products. Before a single byte of HTTP is sent, the browser needs the server’s IP address. Here the recursive resolver has it cached, so the lookup costs one round trip of about 20 ms. A cold lookup can cost 100 ms or more (see the DNS lesson).

    The address returned is that of a nearby CDN edge, not of the origin servers: the CDN uses DNS (or anycast) to send each user to an edge close to them.

    Scene: The browser asks its DNS resolver for shop.example.com and gets the address of a nearby CDN edge back after about 20 ms.

    Key idea: Every new hostname costs a DNS lookup before anything else can start.

  2. 2. Open a TCP connection

    HTTP/1.1 and HTTP/2 run over TCP, which needs a handshake before data flows: the browser sends SYN, the edge answers SYN-ACK, the browser sends ACK. The browser may put data right behind its ACK, so the handshake costs one round trip: 20 ms to an edge 10 ms away.

    One round trip does not sound like much. It is paid on every new connection, and it scales with distance: to a server on another continent it is 100–150 ms.

    Scene: The browser sends SYN to the CDN edge and receives SYN-ACK: one 20 ms round trip opens the TCP connection.

    Key idea: A new TCP connection costs one round trip before the first request byte.

  3. 3. Secure it with TLS

    For HTTPS the browser and the edge now agree on keys and the edge proves its identity with a certificate. With TLS 1.3 that is one round trip: the ClientHello already carries the browser’s key share, and the ServerHello, certificate and Finished come back together. TLS 1.2 needed two.

    Only now can the browser send GET /products. So far: DNS 20 + TCP 20 + TLS 20 = 60 ms of setup, and the request has not left yet. (TLS 1.3 can resume a previous session with 0-RTT data, but that early data can be replayed, so servers accept it only for safe, idempotent requests.)

    Scene: The browser sends a TLS ClientHello to the edge; the ServerHello, certificate and Finished come back in one round trip. Setup has cost 60 ms.

    Key idea: TLS 1.3 adds one round trip to a new connection; TLS 1.2 added two.

  4. 4. Through the edge to the origin

    The product page is dynamic, so the edge cannot answer it from its cache. It forwards the request over an already-open, warm connection to the origin, 35 ms away. The load balancer picks an app server, the app server does about 25 ms of work including a 5 ms database query, and the answer travels back the same way.

    The whole request takes about 120 ms, on top of the 60 ms of setup. Terminating TCP and TLS at the edge matters: the two setup round trips cost 40 ms there instead of about 180 ms to the distant origin.

    Scene: Requests travel from the browser to the CDN edge, across 35 ms to the origin load balancer, to App 1 or App 2, which queries the database, and back. Each takes about 120 ms.

    Key idea: Terminate connections close to the user; let the edge keep warm connections to the origin.

  5. 5. Where the milliseconds go

    Add it up for one request: browser ↔ edge 20 ms, edge ↔ origin 70 ms, load balancer and internal hops about 5 ms, app server work about 25 ms including the database. Roughly three quarters of the time is spent travelling, not computing.

    That tells you where to cut: cache at the edge what you can (a cached response skips the 70 ms entirely), avoid extra round trips (redirects, chained API calls, extra hostnames), and only then tune the code. On a first visit the 60 ms of DNS, TCP and TLS setup comes on top.

    Scene: The same requests with each part labelled: 20 ms to the edge and back, 70 ms to the origin and back, about 25 ms of server work. Network time is most of the 120 ms.

    Key idea: Count round trips before optimising code: distance usually dominates.

  6. 6. A new connection for every request

    The page also needs a stylesheet and a script, which the edge has cached. If the browser opened a new connection for each file, every one would pay TCP and TLS again: three round trips per file, 60 ms each, or 120 ms for the two, one after the other.

    This is how HTTP/1.0 behaved by default. It wastes round trips and makes both ends do the expensive part of TLS over and over.

    Scene: The browser fetches two cached files from the edge, opening a new TCP and TLS connection for each: three round trips per file, about 120 ms in total.

    Key idea: Connection setup is a fixed tax per connection. Paying it per request multiplies it.

  7. 7. Reuse the connection

    With keep-alive (the default since HTTP/1.1) the browser sends both requests over the connection it already has: one round trip each, 40 ms for the two. HTTP/1.1 still sends one request at a time per connection, which is why browsers open about six connections per host.

    HTTP/2 multiplexes many requests as streams on one connection: both go out at once and both answers return in a single round trip, about 20 ms. HTTP/3 runs over QUIC on UDP: its transport and TLS handshake together take one round trip, and a lost packet stalls only its own stream instead of every request on the connection.

    Scene: First both files are fetched one after the other over the open connection, 40 ms in total. Then, with HTTP/2, both requests go out together and both responses return within one round trip.

    Key idea: Reuse connections: keep-alive removes setup per request, HTTP/2 removes waiting in line, HTTP/3 shortens setup and avoids TCP head-of-line blocking.

  8. 8. Under load, waiting takes over

    Now many browsers send 140 requests per second. The two app servers can do about 150 between them (a worker is tied up for about 27 ms per request: 20 ms of work plus the database call), so they are close to full. Arrivals from many users are random, so requests often find every worker busy and wait.

    The network part has not changed, but P95 latency grows by roughly 50 ms, all of it queueing inside the app servers. Under load the breakdown flips: the milliseconds go to waiting, and the fix is capacity (more servers, faster code, fewer queries), not fewer round trips.

    Scene: At 140 requests per second both app servers are nearly fully busy and requests queue, so P95 latency rises well above the light-load value although the network is unchanged.

    Key idea: At light load distance dominates; near capacity, queueing does.

  9. 9. The whole lifecycle

    A first request pays for a DNS lookup, a TCP handshake and a TLS handshake before HTTP even starts, then travels through every proxy between the user and the data. Later requests on the same connection skip all of the setup.

    To make it faster: terminate connections near users (CDN edges), reuse them (keep-alive, HTTP/2, HTTP/3), cache responses at the edge, remove round trips, and keep enough server capacity that requests do not queue.

    Scene: Browsers send 20 requests per second through the CDN edge, the load balancer, two app servers and the database; latency is back near 120 ms.

    Key idea: Latency = setup round trips + request round trips + queueing + work. Attack them in that order.