Chapters

Capstone: opening api.kade.lk

Chapter 1 followed one request in broad strokes. Thirty-three chapters later, we can follow it properly: every packet, every header that changes, every box it passes through, and how long each part takes. You open the Kadé order page at home, and the app calls https://api.kade.lk/orders/1042.

Putting it together · chapter 34 of 34 · 8 min read

The cast

#WhoAddressJob in this storyChapters
1Your laptop192.168.1.23 · a4:83:e7:2b:91:0cThe client1-5, 7
2Home router192.168.1.1 (LAN) · 203.0.113.45 (WAN) · 3c:84:6a:10:ee:01DHCP server, DNS forwarder, gateway, NAT, firewall5, 10, 27
3LankaNet resolver203.0.113.53Recursive DNS with a cache22
4Cloud DNSns-cloud-a1…a4.googledomains.comAuthoritative for kade.lk22-23
5Internet routersISP, transit, Google's edgeForward by longest prefix; BGP picks the path12
6Google front end34.120.88.10 (anycast)Load balancer: ends TCP and TLS, picks a backend25, 33
7kade-api-210.10.1.11:8080 · 42:01:0a:0a:01:0bRuns the Node app21, 31
8kade-db10.10.2.5:5432PostgreSQL3, 21
Timings assume what chapters 12-13 measured: about 41 ms round trip from Colombo to Singapore, where both Kadé's region and (in this example) the nearest Google edge are. Your own numbers will differ; section 5 shows how to measure them.

Before the click: joining the network

Some work happens once, when the laptop joins the home WiFi, long before any page is opened:

LaptopHome routerWiFi association and the WPA2/WPA3 key exchange happen first (not covered in this course)DHCPREQUEST (broadcast): "may I keep 192.168.1.23?"DHCPACK: yes · mask /24 · router and DNS 192.168.1.1 · 24 h leaseARP (broadcast): who has 192.168.1.1?ARP reply: 3c:84:6a:10:ee:01IPv6 Router Solicitation → ff02::2Router Advertisement (if the ISP provides IPv6)

After this, the laptop has an address, a mask, a gateway, a DNS server, and the gateway's MAC in its ARP cache (chapters 3, 5, 9, 27). About 5 ms of work, reused for hours.

The journey, step by step

Step through the whole request. The map shows where the action is; the card shows the packet exactly as it looks at that point, layer by layer. Watch which values change and which stay the same.

step 1 of 20

earlierJoining WiFi: DHCPchapter 27

home · 192.168.1.0/24kade-vpcLaptop192.168.1.23Home routergateway · NATLankaNet resolver203.0.113.53Cloud DNSkade.lk zoneInternetISP · BGPGoogle front end34.120.88.10kade-api-210.10.1.11:8080kade-db10.10.2.5

When the laptop rejoined the home WiFi, it asked to keep its old address. The router said yes and sent the mask, the gateway (192.168.1.1), the DNS server (also 192.168.1.1) and a 24-hour lease.

Frame
a4:83:e7:2b:91:0c → ff:ff:ff:ff:ff:ff (broadcast)
IP
0.0.0.0 → 255.255.255.255
Transport
UDP 68 → 67
Inside
DHCPREQUEST: "may I keep 192.168.1.23?"

Bold values changed compared with the packet before.

The timeline

Here are the same steps on a time axis. The whole first request takes about 176 ms:

#PhaseFrom → toRound tripsTimeChapters
1DNS lookup0 → 42 mslaptop ⇄ resolver ⇄ Cloud DNS42 ms14, 22
2TCP handshake42 → 83 ms141 ms16
3TLS 1.3 handshake83 → 126 ms1 + ~2 ms of crypto43 ms25
4Request → first byte126 → 175 ms1 + ~8 ms in Google and Kadé49 ms24, 33
5Download175 → 176 ms0 (2.8 KB fits in the first window)1 ms20
Total4 round trips176 ms

Change the conditions and see which round trips disappear:

DNS
Connection
Protocol
Response size
DNS42 ms
TCP handshake41 ms
TLS handshake43 ms
Request → first byte49 ms
Download1 ms
0200 ms

Total about 176 ms · 4 round trips · 93% of the time is distance

A simple model with a 50 Mbps line. Larger responses need extra round trips while slow start grows the congestion window from 10 segments (chapter 20).

#SituationRound trips savedTime (41 ms RTT)Why
1First visit, as above-~176 ms
2DNS already cached (within 300 s TTL)DNS~134 msChapter 22's caches
3HTTP/3 (QUIC)1~135 msTransport and TLS handshakes combined (chapter 24)
4TLS 1.2 instead of 1.3−1 (costs one more)~218 msChapter 25's timeline
5Next request on the same connectionDNS + TCP + TLS~49 msKeep-alive and pools (chapter 21)

Measure your own real timings

A model is useful; a measurement is better. curl can print when each phase finished:

curl -so /dev/null -w 'dns %{time_namelookup}\nconnect %{time_connect}\ntls %{time_appconnect}\nttfb %{time_starttransfer}\ntotal %{time_total}\n' https://www.google.com

Run it twice (the second run usually has DNS cached), against any HTTPS site, and paste the output here:

DNS42 ms
TCP handshake41 ms
TLS handshake43 ms
Request → first byte48 ms
Download1 ms
0200 ms
  1. DNS took 42 ms: the resolver probably had to look the name up (a cold cache), or your resolver is far away. Run again and it should drop.
  2. The TCP handshake is one round trip, so your RTT to this server is about 41 ms.
  3. TLS took about one round trip: TLS 1.3.
  4. The server itself answered fast: about 7 ms beyond one round trip.

What changed at each hop

The request travelled over three separate connections: laptop ⇄ Google front end, front end ⇄ kade-api, and kade-api ⇄ kade-db. Here is the first request packet at every point:

#WhereFrame src → dst MACIP src → dstTTLPortsInside
1Laptop → home router (WiFi)a4:83:e7:2b:91:0c → 3c:84:6a:10:ee:01192.168.1.23 → 34.120.88.106452814 → 443TLS (encrypted HTTP/2)
2Home router → ISProuter WAN → ISP gateway203.0.113.45 → 34.120.88.106340001 → 443same bytes
3Across the internetnew at every hopsame62, 61 …samesame bytes
4Arrives at Google's edge-203.0.113.45 → 34.120.88.10~5540001 → 443connection 1 ends: decrypted here
5Google front end → kade-api-242:01:0a:0a:01:01 → 42:01:0a:0a:01:0b35.191.12.34 → 10.10.1.116451277 → 8080HTTP/1.1 + X-Forwarded-For (connection 2)
6kade-api-2 → kade-db42:01:0a:0a:01:0b → 42:01:0a:0a:01:01 (VPC gateway)10.10.1.11 → 10.10.2.56441822 → 5432SQL query (connection 3, from the pool)
MACs change at every hop (chapter 8). IPs and ports change only at NAT (row 2) and at the proxy (row 5), where a brand-new connection starts. TTL restarts at 64 on each new connection. The response retraces the same path backwards, and the home router reverses its NAT row.

One packet, all the layers

The packet carrying the HTTP request (step 12 in the journey) on the WiFi, byte by byte:

Frame 14IP 20TCP 32TLS 5HTTP/2 HEADERS ~64 (encrypted)tag 164
#LayerBytesCarriesChapter
1Frame header14MACs (laptop → router), type 0x0800 = IPv47-8
2IPv4 header20192.168.1.23 → 34.120.88.10, TTL 64, protocol 6 (TCP), DF12
3TCP header3252814 → 443, seq/ack, window, timestamps option17-19
4TLS record header5type "application data", length25
5HTTP/2 HEADERS~64GET /orders/1042, api.kade.lk, the token; compressed (HPACK) and encrypted24
6TLS auth tag16Proves nobody changed the bytes25
7Frame check sequence4Detects damage on the link7
About 155 bytes, of which only ~64 are the actual request. Five layers of envelopes, exactly as chapter 1 and chapter 11 drew them. (On WiFi the real radio header is longer; the laptop and router convert to and from this Ethernet form.)

When it breaks: a map of where to look

Every step is also a place where things fail. Find the symptom, and you know the layer and the chapter:

#StepSymptomLikely causeCheck withCh
1DHCPAddress 169.254.x.x; "no internet"No DHCP server replyipconfig getifaddr en027
2ARP / gatewayCan't even ping 192.168.1.1WiFi, cable, wrong networkarp -a, ping 192.168.1.15, 9
3DNS"Could not resolve host" / NXDOMAIN / SERVFAILMissing record, broken DNSSEC, resolver downdig api.kade.lk, dig @8.8.8.8 …22-23
4RoutingTimeouts; traceroute stops at one hopA route or peering problemtraceroute -T -p 443 api.kade.lk12-13
5TCP"Connection refused" vs "timed out"Nothing listening vs firewall dropnc -vz api.kade.lk 44316, 31
6MTUHandshake works, big responses hangPMTUD black hole (VPNs)ping -D -s 1472 …18
7TLSCertificate errors; handshake failureExpired cert, wrong name, missing chain, TLS inspectionopenssl s_client -connect api.kade.lk:443 -servername api.kade.lk25
8Browser rulesWorks in curl, fails in the browserCORS, mixed content, HSTSDevTools console28
9Load balancer502 / 503 / 504Keep-alive race, no healthy backend, timeoutLB logs statusDetails, get-health33
10App500, 401, 404Your code, auth, routesApp logs (with the real client IP, thanks to trust proxy)24
11SpeedSlow but workingRound trips, loss, bufferbloat, no connection reusecurl -w (section 5), mtr13, 20, 21
Work from the bottom up: if ping 192.168.1.1 fails, nothing above it matters yet. Each passing check clears a whole layer.

How the course fits together

#Journey stepBuilt on
1Getting on the networkIP addresses (2), subnets and masks (3-4), gateways (5), MACs and hex (7), DHCP (27), IPv6 SLAAC (30)
2Finding the serverUDP (14-15), DNS (22), records (23), anycast (12)
3Reaching the serverFrames and hops (8), ARP (9), NAT (10), layers (11), routing and BGP (12), ICMP and traceroute (13), switches and routers (32)
4A reliable connectionTCP (16), sequence numbers (17), segments and MTU (18), flow control (19), congestion control (20), sockets and ports (21, 31)
5A private connectionTLS and certificates (25), SSH for admin access (29)
6The conversationHTTP and its versions (24), CORS (28), WebSockets (26)
7The server sideKadé's GCP network (6, 0), load balancers (33), time sync (27)

Summary

#IdeaIn one line
2JoiningDHCP, ARP and an IPv6 router advertisement happen once, before any request
3The journeyDNS → ARP (cached) → TCP → TLS → HTTP → load balancer → kade-api → kade-db → back
4Timing~176 ms first request: 4 round trips of 41 ms + ~12 ms of work; ~49 ms on a reused connection
5Measuringcurl -w and DevTools Timing show the same phases
6HopsMACs change every hop; IP/ports change only at NAT and proxies; three separate connections
7Layers~155 bytes on the wire for ~64 bytes of request
8DebuggingCheck layer by layer, from the bottom up

Try it yourself: the whole journey from your laptop

#CommandStep it showsWhat to look for
2ipconfig getpacket en0macOS · Linux: nmcli dev showYour DHCP leaseYour IP, router, DNS server, lease time.
2arp -a | grep '192.168.1.1)'The gateway's MAC3c:84:… style MAC, cached.
3dig +trace www.google.comThe full DNS walkRoot → TLD → authoritative.
3traceroute -T -p 443 www.google.comLinux (sudo) · macOS: traceroute -P tcp -p 443The routers on the wayYour router, the ISP, then Google's edge.
3openssl s_client -connect www.google.com:443 -servername www.google.com </dev/null | head -20The TLS handshake and certificate chainSubject, issuer, TLSv1.3.
3curl -v https://www.google.com -o /dev/nullTCP, TLS, HTTP in one go"Connected to", "SSL connection using TLSv1.3", ALPN h2, the request and response headers.
5The curl -w command from section 5Real timings per phasePaste into the tool; compare a first run with a second.
3sudo tcpdump -n -i en0 host www.google.com while running curl in another windowEvery packet of the journeySYN, SYN-ACK, ACK, TLS records, FIN.

Spotted a mistake, or something unclear? Suggest an edit on GitHub