Part 1 · Foundations · Lesson 1.3

How the web works

DNS, TCP, TLS and HTTP from a URL to a rendered page, plus the status codes, headers and caching rules every web engineer needs.

L0 · Intern≈ 5 min read
Key points · the two-minute version
  1. URL → DNS → TCP/QUIC → TLS → HTTP request → proxy/CDN → app → response → browser render.
  2. Methods have meaning. GET is safe, PUT and DELETE are idempotent, POST is neither (unless you add an idempotency key).
  3. Retry 429 and 5xx with backoff. Never retry 400, 401, 403, 404 or 422.
  4. Cookies should be HttpOnly, Secure and SameSite. CORS protects browsers; it isn't auth.
  5. Streams (SSE, LLM tokens) break when proxies buffer, so disable buffering on stream routes.
You should be able to
  • Trace a request from URL to response
  • Use HTTP methods, status codes and headers correctly
  • Explain cookies, CORS and caching at a working level

Why this matters

Every API you build and every frontend you ship speaks HTTP. Bugs that look mysterious (CORS errors, stale pages, a 502 from a proxy, a streaming response that arrives all at once) are HTTP behaviour you can reason about once you know the layers.

What happens when you open a URL

  1. DNS: the browser asks resolvers for the IP address of example.com (A/AAAA records; CNAMEs point names at other names).
  2. TCP connection to that IP on port 443 (QUIC over UDP for HTTP/3).
  3. TLS handshake: the server proves its identity with a certificate; both sides agree keys; everything after is encrypted.
  4. HTTP request: method, path, headers, optional body.
  5. The request usually passes a CDN or reverse proxy (Nginx, a load balancer) before reaching your application server.
  6. HTTP response: status code, headers, body.
  7. The browser parses HTML, fetches CSS, JavaScript and images (more requests), builds the page and runs scripts.

HTTP essentials

Method Meaning Safe? Idempotent?
GET Read Yes Yes
POST Create or trigger an action No No (make it so with an idempotency key)
PUT Replace No Yes
PATCH Partial update No Not necessarily
DELETE Remove No Yes
Status Meaning Who should act
200 / 201 / 204 OK / created / no content —
301 / 302 / 304 Moved / found elsewhere / not modified (use cache) Client follows
400 / 422 Bad request / valid JSON but invalid content Client fixes input; don't retry
401 / 403 Not authenticated / authenticated but not allowed Client logs in / stop
404 / 409 Not found / conflict (e.g. duplicate) Client
429 Too many requests (read Retry-After) Client waits, then retries
500 / 502 / 503 / 504 Server bug / bad upstream / unavailable / upstream timeout Server; client may retry with backoff

Headers you'll meet daily: Content-Type, Authorization, Cache-Control, ETag, Set-Cookie, Retry-After, Location, X-Request-ID.

Cookies, CORS and caching

  • Cookies are set by the server and sent back automatically on every request to that site. Mark session cookies HttpOnly (no JavaScript access), Secure (HTTPS only) and SameSite=Lax or Strict.
  • CORS is the browser asking the API "may a page from origin A call you?". The API answers with Access-Control-Allow-Origin and friends. CORS protects users' browsers; it isn't authentication, and curl ignores it entirely.
  • Caching: Cache-Control: max-age=3600 lets caches reuse a response; no-store forbids it (use for anything personal). ETag + If-None-Match lets clients revalidate cheaply (304).

Streaming responses

Some responses arrive in pieces: file downloads, Server-Sent Events (SSE) for live updates, and token-by-token LLM output. Proxies love to buffer responses, which turns a stream into one late lump. That's why AI streaming endpoints send Cache-Control: no-cache and, behind Nginx, X-Accel-Buffering: no (Q14).

Common mistakes

  • Returning 200 with {"error": ...} in the body. Use the status code; clients and monitoring depend on it.
  • "Fixing" CORS with Access-Control-Allow-Origin: * on an API that uses cookies.
  • Caching personalised responses at a CDN.
  • Retrying 4xx errors. They'll fail the same way, and on paid APIs you pay again.

Interview follow-ups

  • "What happens when you type a URL and press enter?" Walk the layers: DNS, TCP/QUIC, TLS, HTTP request, CDN/proxy, app, response, parsing, subresource requests, render. Depth on one layer impresses more than breadth on all.
  • "HTTP/1.1 vs HTTP/2 vs HTTP/3?" /2 multiplexes many requests over one connection with header compression; /3 runs over QUIC (UDP), avoiding head-of-line blocking at the transport layer.
  • "Why does my API work in curl but fail in the browser?" Usually CORS (preflight failing or missing headers) or cookies (SameSite, Secure, domain).

Check yourself

What's the difference between 401 and 403?

401: we don't know who you are (missing or invalid credentials). 403: we know who you are and you're not allowed.

Which status codes are worth retrying, and which never?

Retry 429 (respecting Retry-After), 502, 503, 504 and network errors, with backoff and a limit. Never retry 400, 401, 403, 404 or 422: the same request fails the same way.

Your streaming endpoint works locally but arrives all at once in production. Likely cause?

A proxy or load balancer buffering the response. Disable buffering for that route (X-Accel-Buffering: no for Nginx), set Cache-Control: no-cache, and check any compression layer.

Build it

Use curl -v https://example.com and identify the DNS result, TLS handshake and response headers. Then open your browser's Network tab on any site and find one cached response (304 or "from cache") and one cookie with its flags.

What comes next

1.4 Python for engineers: the backend language of most AI work.

Build it to remember

Two hands-on projects that exercise this lesson. Pick one, build it end to end, and keep it in your portfolio.

Project 1

HTTP inspector

An endpoint that echoes method, headers, cookies and body, plus a client that demonstrates caching with ETag/304 and a 429 with Retry-After.

  • Python
  • FastAPI
  • httpx

Practises Status codes, headers and caching, seen from both sides.

Project 2

Tiny SSE clock

Stream the time every second over SSE, put Nginx in front, watch buffering break it, then fix it.

  • FastAPI
  • Nginx
  • vanilla JS

Practises Streaming responses and proxy behaviour.

Can you answer every check-yourself question out loud without opening the answer?