06. HTTP & the Network Path
Big picture: After DNS gives you an IP, the client still must open a connection, secure it, and speak HTTP before your API code runs. Most of a βslow requestβ is often network + TLS, not your database.
This lesson fills the gap between DNS and Load Balancing: what happens on the wire, how HTTP versions differ, and how to budget latency like an interviewer expects.
Prerequisites: 04. Clients, Servers & APIs, 05. How DNS Resolution Works
Read next: 07. Load Balancing β many connections land on one LB IP.
Learning goals
By the end of this lesson you will be able to:
- Trace DNS β TCP β TLS β HTTP β response for a typical API call
- Explain RTT and why geography matters
- Describe TCP handshake and TLS handshake at a beginner level
- Compare HTTP/1.1, HTTP/2, and HTTP/3 for system design
- Use connection reuse (keep-alive) to cut latency
- Build a rough latency budget (where time goes)
- Place CDN / edge / TLS termination correctly in an HLD
- Avoid confusing βapp slowβ with βnetwork slowβ
The full path of one HTTPS request
| Stage | What it does | Often forgotten? |
|---|---|---|
| DNS | Name β IP | Yes β see lesson 05 |
| TCP | Reliable byte stream to the IP:port | Yes |
| TLS | Encrypts the stream (HTTPS) | Yes β expensive RTT |
| HTTP | Request / response semantics | No β this is what APIs discuss |
| App + DB | Your business logic | Where we spend most design time |
Interview phrase: βUser-perceived latency is the sum of network round trips plus server time β not server time alone.β
RTT β the unit of network pain
RTT (Round-Trip Time) = time for a packet to go to the server and back.
| Client β server | Typical RTT (ballpark) |
|---|---|
| Same city / region | 5β20 ms |
| Cross-country | 40β80 ms |
| Intercontinental | 100β250+ ms |
Why RTT matters
Many steps need at least one RTT:
- TCP handshake β 1 RTT (classic)
- TLS 1.2 handshake β 2 RTTs (often)
- TLS 1.3 β 1 RTT (better)
- Each HTTP/1.1 request on a new connection pays this again
Example: Mumbai user β Virginia API, RTT β 180 ms.
TCP 1Γ180 = 180 ms
TLS 2Γ180 = 360 ms (TLS 1.2 style)
HTTP request/response body travel β 180 ms
ββββββββββββββββββββββββββββββββββββ
Network-ish floor β 720 ms
+ DNS (if cold) + server time
Your handler can be 20 ms and the user still waits ~1 second. That is why CDN, regional deploy, and connection reuse show up in designs.
Elevator analogy
| Network idea | Elevator |
|---|---|
| RTT | Time to ride to the top floor and back |
| Handshake | Calling the elevator before you can load cargo |
| Keep-alive | Keeping the elevator at your floor for the next trip |
| CDN | Building a second elevator shaft in your neighborhood |
TCP β opening the pipe
TCP gives a reliable, ordered stream between client and server IP:port (e.g. 203.0.113.10:443).
Three-way handshake (simplified)
Client β Server: SYN ("want to connect")
Server β Client: SYN-ACK ("OK, me too")
Client β Server: ACK ("go")
Cost β 1 RTT before any HTTP bytes.
Beginner facts worth knowing
| Idea | Plain meaning |
|---|---|
| Port 443 | Usual HTTPS |
| Port 80 | Usual HTTP (often redirects to HTTPS) |
| Loss / retransmit | Bad Wi-Fi β TCP retries β latency spikes |
| Connection = memory | Each open TCP conn costs RAM on LB/app β limits matter at huge fan-out |
You do not need Congestion Window deep-dives in most interviews. You do need: βnew connection costs RTT(s).β
TLS β making the pipe private
HTTPS = HTTP over TLS (TLS replaced older βSSLβ in practice).
What TLS buys you
| Property | Meaning |
|---|---|
| Encryption | Eavesdroppers canβt read the JSON |
| Integrity | Tampering is detectable |
| Authentication | Certificate proves youβre talking to api.myapp.com (not an impostor) |
Handshake cost (mental model)
TLS 1.2 (typical story): ~2 RTTs before application data
TLS 1.3: ~1 RTT (session resumption can be 0-RTT in some cases)
Certificates are usually terminated at the load balancer or CDN (Load Balancing β SSL termination). App servers often see plain HTTP on a private network.
Certificate check failure
User sees browser warnings β not your 500 handler. DNS pointing to the wrong host + mismatched cert is a classic outage class.
HTTP β the language of APIs
Once the TLS pipe is up, the client speaks HTTP.
Request / response (reminder)
GET /api/v1/feed?cursor=abc HTTP/1.1
Host: api.myapp.com
Authorization: Bearer ...
Accept: application/json
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 1234
{"items":[...]}
API design (verbs, status codes, idempotency) lives in Lesson 04. Here we care about how versions share the connection.
HTTP/1.1 vs HTTP/2 vs HTTP/3
HTTP/1.1 β one request at a time per connection (classic)
- Keep-alive reuses the TCP+TLS session for many requests (huge win vs new handshake each time).
- Head-of-line blocking: one slow response delays the next request on that connection.
- Browsers open ~6 parallel connections per host to fake concurrency.
Conn1: req1 .......... resp1 | req2 ..... resp2
β stuck waiting even if req2 is tiny
HTTP/2 β multiplex on one connection
- Many request/response streams share one TCP+TLS connection.
- Binary framing, header compression (HPACK).
- Still suffers if TCP packet loss blocks the whole connection.
System design takeaway: Fewer connections to your LB, better parallel loading of APIs + assets on one host.
HTTP/3 β HTTP over QUIC (UDP)
- Built on QUIC, not TCP.
- Loss on one stream hurts less (no TCP HOL blocking at transport the same way).
- Growing adoption via browsers/CDNs; apps often get it βfor freeβ at the edge.
| Version | Transport | Multiplex | Beginner interview line |
|---|---|---|---|
| 1.1 | TCP | Limited (multi-conn) | Keep-alive matters |
| 2 | TCP | Yes | One conn, many streams |
| 3 | QUIC/UDP | Yes | Better on lossy mobile nets |
You rarely βpick HTTP/3 in LLD.β You recognize CDNs/LBs negotiate versions and that mobile + high RTT benefits most from H2/H3 + edge.
Keep-alive & connection reuse
Without reuse (worst case)
For every API call:
DNS? + TCP + TLS + HTTP
Mobile apps that open a fresh TLS session per request feel terrible on high RTT.
With keep-alive / connection pool
Once:
DNS + TCP + TLS
Then many:
HTTP request / response on same connection
| Layer | Reuse mechanism |
|---|---|
| Browser | Persistent connections, H2 multiplex |
| Mobile / backend HTTP client | Connection pool |
| LB β app | Pooled upstream connections |
Interview tip: When discussing chatty microservices, mention connection pooling and avoid chatty cross-region calls (each call Γ RTT).
Latency budget β where does 800 ms go?
Worked example: user in Bangalore, API in us-east-1, cold connection, TLS 1.2-ish, simple read.
| Step | Budget (illustrative) |
|---|---|
| DNS (cold) | 30β80 ms |
| TCP handshake | 1 Γ RTT β 180 ms |
| TLS handshake | 2 Γ RTT β 360 ms |
| HTTP request + response flight | β 180 ms |
| LB + app + DB | 20β50 ms |
| Total | ~770β930 ms |
Same app in Mumbai region (RTT 15 ms):
| Step | Budget |
|---|---|
| TCP + TLS 1.3 | ~30 ms |
| Flight + server | ~40 ms |
| Total | ~70β100 ms |
What this teaches for HLD
- Put static & cacheable content on a CDN near users.
- Deploy APIs regionally if audience is multi-continent.
- Reuse connections; avoid request waterfalls (A waits for B waits for C).
- Measure TTFB (time to first byte) separately from download time of large bodies.
- Optimizing SQL from 40 ms β 5 ms does nothing visible if TLS+RTT dominate.
TTFB, download time, and large responses
| Metric | Meaning |
|---|---|
| TTFB | Time until first response byte β handshake + queue + server think time |
| Content download | Rest of the body β depends on size / bandwidth |
| p99 latency | Tail β often retries, GC, cold caches, network blips |
A 2 MB JSON feed over a slow mobile link can dominate after TTFB. Prefer pagination, compression (gzip / br), and smaller payloads β API design meets network reality.
Where TLS and HTTP terminate in real architectures
| Pattern | Client β edge | Edge β origin | Notes |
|---|---|---|---|
| TLS at CDN | HTTPS | Often HTTPS to origin | Common for websites |
| TLS at LB | HTTPS | HTTP in VPC | Classic app tier β Lesson 07 |
| mTLS service mesh | β | Mutual TLS between services | Advanced microservice hardening |
Beginners: terminate TLS at CDN or LB; donβt invent per-instance public certs unless you must.
Proxies youβll hear mixed up
| Term | Role |
|---|---|
| Forward proxy | Client-side middlebox (company egress) |
| Reverse proxy | Sits in front of your servers (Nginx, Envoy, often the LB) |
| API gateway | Reverse proxy + auth, rate limit, routing β Case 24 |
| Load balancer | Distributes across many backends β Lesson 07 |
Same box can wear multiple hats. In diagrams, label what problem it solves.
Mobile and flaky networks
| Reality | Design implication |
|---|---|
| High RTT, packet loss | Prefer H2/H3 via CDN; minimize round trips |
| App backgrounded | Timeouts, retries with jitter β see 16. Retries & Resilience |
| Radio spin-up | Batch requests; donβt chat every 100 ms |
| Offline | Queue writes locally; sync later |
Chat and realtime (Case 07) use long-lived connections (WebSockets) so you donβt redo TLS constantly β different trade-offs (stateful gateways, sticky sessions).
Back-of-the-envelope: connection capacity
Rough intuition for interviews (order of magnitude):
Each idle TLS connection β few KB to tens of KB RAM on server/LB
100k concurrent connections β hundreds of MB to a few GB RAM
+ CPU for handshakes during thundering herd (sale start, outage recovery)
Thundering herd: millions of clients reconnect at once after a blip β handshake CPU spike. Mitigate with staggered reconnect, connection reuse, edge termination.
Sale-night lesson (Case 44): CDN + warm connections matter as much as app count.
Check your understanding
- List the steps after a user taps βLoad feedβ on HTTPS, starting from DNS.
- RTT is 100 ms. Roughly how much time for TCP + TLS 1.2 before HTTP?
- Why does HTTP/2 help a page that loads 30 small API resources?
- Your DB query dropped from 50 ms to 5 ms, but users in another continent still see ~800 ms. Why?
- Where should TLS usually terminate in a beginner HLD?
- What is keep-alive and why do mobile clients need connection pools?
Answers
- DNS β TCP handshake β TLS handshake β HTTP request β LB/app/DB β HTTP response.
- TCP ~100 ms + TLS ~200 ms β 300 ms before HTTP data (classic 1+2 RTT model).
- Multiplexes many streams on one connection β avoids 30Γ handshake or 6-conn limit waterfalls.
- Cross-continent RTT + TLS dominate; server time was never the bottleneck.
- CDN or load balancer (public edge), not every app VM.
- Reuses TCP/TLS so later requests skip handshakes; pools manage many parallel reusable conns.
Common beginner mistakes
| Mistake | Better |
|---|---|
| Optimizing only SQL for βlatencyβ | Budget DNS+TCP+TLS+RTT first |
| New TLS connection per API call | Keep-alive / H2 / client pools |
| Drawing Client β App with no DNS/LB/TLS | Show edge termination |
| βHTTP/3 will fix my bad API chatty designβ | Reduce round trips and payload size |
| Ignoring geography | Regional deploy or CDN for static; measure RTT |
Recap
- User time = network round trips + server time; RTT is the currency.
- Path: DNS β TCP β TLS β HTTP β app.
- TLS handshakes are expensive; reuse connections; prefer TLS 1.3 / H2 at the edge.
- HTTP/1.1 vs 2 vs 3 = how streams share connections, not different REST verbs.
- Build a latency budget before blaming the database.
- Terminate HTTPS at CDN/LB; pool connections; design for mobile RTT.
Next: Load Balancing β distributing many TCP/TLS sessions across healthy backends.
Previous: How DNS Resolution Works