# HTTP Request Lifecycle: DNS, TCP, TLS, Request, Response

**In short.** Typing a URL triggers seven distinct stages: URL parsing, DNS resolution, TCP connection, TLS handshake, request, server processing and response. Almost every performance problem and most failures belong to exactly one of them, which is why this sequence is the foundation of any diagnosis.

 When you type a URL and press Enter, a complex chain of events unfolds in milliseconds. Understanding this lifecycle is fundamental for web developers — it's the foundation for diagnosing performance issues, security problems, and network errors.

## Step 1: URL Parsing

 The browser parses the URL into components:

```
`https://www.example.com:443/path/page?query=value#section │ │ │ │ │ │ scheme host port path query fragment`
```

 The browser checks: Is this a valid URL or a search query? If no scheme, prepend `https://`. If no port, use defaults (443 for HTTPS, 80 for HTTP).

## Step 2: DNS Resolution

 The browser needs to convert the hostname to an IP address:

 - **Browser cache:** Check recently resolved domains (Chrome: `chrome://net-internals/#dns`)
 - **OS cache:** Check the operating system's DNS cache
 - **Hosts file:** Check `/etc/hosts` (or `C:\Windows\System32\drivers\etc\hosts`)
 - **Router cache:** Your local router may cache DNS
 - **ISP resolver:** Query the configured DNS resolver (ISP, Google 8.8.8.8, Cloudflare 1.1.1.1)
 - **Recursive resolution:** Root → TLD → Authoritative nameserver

 Typical time: 1-100ms (cached) or 20-200ms (cold cache).

## Step 3: TCP Connection

 The browser establishes a TCP connection via the **three-way handshake**:

 - **SYN:** Client sends a synchronization packet to the server
 - **SYN-ACK:** Server acknowledges and sends its own sync
 - **ACK:** Client acknowledges — connection established

 This takes one full round trip (RTT). For a server 50ms away, that's 50ms just for the handshake.

## Step 4: TLS Handshake

 For HTTPS, an additional handshake establishes encryption:

 - **Client Hello:** Supported TLS versions, cipher suites, random number
 - **Server Hello:** Chosen cipher suite, server certificate, random number
 - **Certificate verification:** Client verifies the certificate chain against trusted CAs
 - **Key exchange:** Both sides derive a shared session key
 - **Finished:** Both sides confirm encryption is active

 TLS 1.2 requires 2 round trips. TLS 1.3 reduces this to 1 round trip (and supports 0-RTT for returning connections).

## Step 5: HTTP Request

 The browser sends the HTTP request:

```
`GET /path/page?query=value HTTP/2 Host: www.example.com User-Agent: Mozilla/5.0 ... Accept: text/html,application/xhtml+xml Accept-Encoding: gzip, br Accept-Language: en-US,en;q=0.9 Cookie: session=abc123 Connection: keep-alive`
```

## Step 6: Server Processing

 - **Web server:** Nginx/Apache receives the request, applies configuration rules
 - **Reverse proxy:** May forward to an application server
 - **Application:** Routes the request, runs business logic, queries databases
 - **Database:** Executes queries, returns results
 - **Response generation:** Application builds HTML/JSON response
 - **Compression:** Server compresses the response (gzip/Brotli)

## Step 7: HTTP Response

```
`HTTP/2 200 OK Content-Type: text/html; charset=utf-8 Content-Encoding: br Cache-Control: max-age=3600 Set-Cookie: session=abc123; Secure; HttpOnly Content-Length: 45231`
```

## Step 8: Browser Rendering

 - **HTML parsing:** Build the DOM (Document Object Model) tree
 - **CSS parsing:** Build the CSSOM (CSS Object Model) tree
 - **JavaScript execution:** Parse and execute scripts (may block rendering)
 - **Render tree:** Combine DOM + CSSOM into a render tree
 - **Layout:** Calculate exact position and size of each element
 - **Paint:** Fill in pixels — colors, borders, shadows, text
 - **Composite:** Combine layers into the final image on screen

## Performance Implications

 Each step offers optimization opportunities:

 - **DNS:** DNS prefetch, reduce external domains, use fast DNS providers
 - **TCP:** Connection reuse (keep-alive), HTTP/2 multiplexing, TCP Fast Open
 - **TLS:** TLS 1.3, session resumption, OCSP stapling
 - **Server:** Caching, query optimization, CDN for static assets
 - **Response:** Compression, minimal HTML, efficient resource hints
 - **Rendering:** Critical CSS, async/defer scripts, lazy loading

## Conclusion

 Every HTTP request traverses DNS, TCP, TLS, server processing, and browser rendering — each adding latency. Understanding this lifecycle is the key to diagnosing "why is my site slow?" and knowing exactly where to optimize.

---

Source: https://enterno.io/en/articles/http-request-lifecycle
