# The request/response cycle

> Everything that happens between pressing Enter and seeing the page.

Source: https://aminbytes.dev/bytes/http/basics/request-response-cycle/


You type `https://example.com` and press Enter. Here's what happens, in order.

![The steps between browser and server](cycle.svg "1. DNS lookup · 2. connect · 3. TLS handshake · 4. request · 5. response")

## 1. DNS: find the server's address

Computers connect to IP addresses, not names. The browser asks a DNS resolver: *"what's the IP of example.com?"* (Side note: The answer is cached for a while (the record's *TTL*), so the next visit usually skips this step.)

```bash
dig +short example.com
```

```text
93.184.215.14
```

## 2. Open a connection

The browser opens a TCP connection to that IP on port `443` (because the scheme is `https`). HTTP/3 uses QUIC over UDP instead, but the role is the same: a reliable channel between the two machines.

## 3. TLS handshake

For `https`, client and server use TLS to agree on encryption keys and the server proves it really is `example.com` with a certificate. After this, everything is encrypted.

> [!TIP]
> `curl -v` shows this step too. Look for lines like `SSL connection using TLSv1.3` and `server certificate verification OK`.

## 4. Send the request

Now the actual HTTP request goes over the connection:

```http
GET / HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 ...
Accept: text/html
```

## 5. Get the response

The server finds (or generates) the page and answers:

```http
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8

<!doctype html>
<html>...
```

## 6. …and then many more requests

The HTML mentions CSS, JavaScript and images. For each of those the browser sends **another request**, usually reusing the same connection. A typical page makes dozens of them. Open your browser's DevTools → **Network** tab and reload any page to see the list.

## Measure it

`curl` can time each phase:

```bash
curl -o /dev/null -s -w "dns: %{time_namelookup}s\nconnect: %{time_connect}s\ntls: %{time_appconnect}s\nfirst byte: %{time_starttransfer}s\ntotal: %{time_total}s\n" https://example.com
```

```text
dns: 0.012s
connect: 0.031s
tls: 0.068s
first byte: 0.101s
total: 0.102s
```

Notice how much time passes *before* the first byte of the response arrives. That's why reusing connections matters.


## Byte check

1. What does the browser need first, before it can connect to `example.com`?
   - **The server's IP address, from DNS** (correct)
   - The server's TLS certificate
   - The page's HTML
   Computers connect to IP addresses, so the name is looked up with DNS first.

2. What does the TLS handshake do?
   - Compresses the page
   - **Agrees on encryption keys, and the server proves who it is with a certificate** (correct)
   - Looks up the server's IP address
   After the handshake, everything on the connection is encrypted.

3. The HTML mentions 10 images. What does the browser do?
   - Nothing more: images come inside the HTML
   - **Sends about 10 more requests, one per image, often over the same connection** (correct)
   - Sends one request for all 10
   Every file is its own request. Reusing the connection keeps them fast.


