The Network from First Principles
DNS, TCP, TLS, and the speed of light — what actually happens when you curl a URL, and why latency is physics you cannot abstract away.
🎯 By the end you'll understand
The exact sequence of events between typing a URL and receiving a response — DNS lookup, TCP three-way handshake, TLS negotiation, HTTP request/response — what each hop costs in milliseconds, why geographic distance sets a hard floor on latency that no engineer can optimize past, and how to diagnose "it won't connect" by decomposing the request into its layers. You'll also understand why "the network is reliable" is the first and most dangerous fallacy of distributed computing.
KEY INSIGHTThe network is not a function call
Every network request is a negotiation conducted by strangers over infrastructure neither of them controls. Treating it like a local function call is how production outages happen.
Your TraceLab script runs requests.get("https://api.example.com/runs"). If your mental model is "Python sends a request, Python gets a response," you cannot debug a timeout, a certificate error, a flapping DNS entry, or the 340ms round-trip from your Sydney lab to a Virginia server. The network has five distinct hops before your bytes arrive, and any one of them can fail silently in a different way. This lesson dissects them.
A single HTTPS request involves at minimum: a DNS resolution (a separate UDP request to a resolver), a TCP three-way handshake (one round-trip to establish a connection), a TLS handshake (one to two round-trips to negotiate encryption), and finally the HTTP request/response pair. Each hop crosses real wire at a speed that is bounded by the speed of light — roughly 200 km/ms in fiber. Sydney to Virginia is ~16,000 km. The physics invoice arrives whether your code is elegant or not.
HOW IT WORKSDNS: the phonebook that doesn't lie (but can lie slowly)
Before TCP can do anything, your machine needs an IP address — a 32-bit (IPv4) or 128-bit (IPv6) number that actually routes packets. DNS (Domain Name System) is the distributed database that maps human names to those numbers. When your Python code calls requests.get("https://api.example.com/runs"), the OS libc resolver fires a UDP packet — typically to your router, which forwards it to a recursive resolver (often your ISP's or a public one like 8.8.8.8 or 1.1.1.1), which in turn may need to walk the delegation tree: root nameservers → .com nameservers → example.com nameservers → the authoritative record.
The answer comes back with a TTL (time-to-live): a number of seconds the resolver is allowed to cache it. Short TTLs (30s, 60s) let operators do fast failovers but force frequent lookups. Long TTLs (3600s+) are cached almost everywhere but leave stale addresses in the wild after a migration. This is why "just change the DNS" is not a zero-downtime strategy.
DNS queries use UDP port 53 by default. UDP is connectionless — no handshake, no reliability guarantee. If the packet is lost, the resolver retries. Responses larger than 512 bytes (DNSSEC-signed records, many IPv6 records) fall back to TCP. DNS-over-HTTPS (DoH) and DNS-over-TLS (DoT) tunnel the protocol over encrypted transports — privacy wins, but they add TLS overhead to the lookup itself.
.com nameserver cached, so it only needs one extra hop. But cold cache resolution touches all of them.Tools for diagnosis: dig api.example.com +trace shows the full delegation walk and the TTL on each answer. dig @8.8.8.8 api.example.com forces a specific resolver. If your app resolves DNS once at startup and caches the result in a variable, you've broken failover — the server moves, your IP stays. Most HTTP libraries resolve per-connection by design.
HOW IT WORKSTCP: the handshake that proves you're there
IP routes packets. It makes no guarantees about delivery, ordering, or duplication. TCP (Transmission Control Protocol) is the layer that adds reliability on top — and it does so at the cost of a round-trip before any data can flow.
The three-way handshake establishes a connection. The client sends a SYN (synchronize) packet with a random sequence number. The server replies with a SYN-ACK (synchronize-acknowledge), proving it heard the client and sending its own sequence number. The client sends an ACK, proving it heard the server. Only after this exchange can either side send application data — and that exchange takes one full round-trip.
The figure below runs a whole request from Sydney to Virginia, with the numbers the widget further down uses. It starts with the DNS lookup you just read about, then this handshake, then the TLS and HTTP exchanges the next two sections explain.
After the handshake, TCP's job is to make the connection look like a reliable stream. Lost packets are retransmitted. Out-of-order packets are reordered. The congestion window (cwnd) starts small and grows exponentially — "slow start" — so a fresh connection underperforms for the first few hundred milliseconds. This is why connection reuse (HTTP keep-alive, HTTP/2 multiplexing) matters so much: you've already paid the handshake and warmed the window.
HOW IT WORKSTLS: the lock you can't skip
TCP gives you reliable delivery. TLS (Transport Layer Security) adds confidentiality, integrity, and authentication. For HTTPS it is not optional — and understanding why "just use HTTP" is wrong is part of understanding what TLS actually does.
TLS 1.3 (current; RFC 8446, 2018) has reduced the handshake to one round-trip in most cases (down from two in TLS 1.2). The client sends its supported cipher suites and a key share in the first message. The server replies with its choice, its certificate, and its key share. The client verifies the certificate chain against its trust store (a list of Certificate Authorities shipped with your OS or browser), derives session keys, and sends its own Finished message. Both sides now share symmetric keys — the asymmetric cryptography was only used to bootstrap them.
A TLS certificate is a signed statement: "A Certificate Authority (CA) verified that the entity presenting this certificate controls the domain api.example.com." The CA's signature is verifiable because your OS has the CA's public key pre-installed. This chain-of-trust model is why a self-signed certificate triggers a browser warning (no CA vouches for it) and why a compromised CA (DigiNotar, 2011) is a catastrophic event — suddenly every certificate it ever signed is suspect.
After the TLS handshake, all TCP payload bytes are encrypted. Neither your ISP, nor the network path, nor an attacker with a packet sniffer sees your HTTP headers, cookies, or body. The domain name is visible in plaintext during TLS (in the SNI extension) and via DNS — HTTPS is not anonymous, just confidential and authenticated. HSTS (HTTP Strict Transport Security) is a response header that tells browsers to never talk to this domain over plain HTTP again, preventing downgrade attacks.
HOW IT WORKSHTTP: the conversation on top of all of this
With DNS resolved, TCP connected, and TLS negotiated, HTTP is almost anticlimactic. HTTP/1.1 (the default for most simple clients) sends a text-format request — a method, a path, headers, an optional body — and receives a status line, headers, and a body. The entire exchange is synchronous and sequential: one request in flight per TCP connection, which is why HTTP/1.1 pipelining never shipped properly.
HTTP/2 multiplexes multiple requests over a single TCP connection using binary framing, solving head-of-line blocking at the HTTP layer. HTTP/3 replaces TCP with QUIC (UDP-based) to eliminate TCP's head-of-line blocking at the transport layer too — a packet loss only stalls the stream that lost the packet, not the entire connection.
For your TraceLab API calls, the parts that matter most are: the status code (200 OK, 404 Not Found, 503 Service Unavailable), the Content-Type header (what is this body?), and — critically — whether the response body was fully received before your code tries to parse it. Most Python HTTP libraries handle this transparently, but streaming responses and timeouts are where the transparency breaks down.
A 5xx error means the server admitted to a problem. A 4xx means the server thinks the problem is yours (though it might be wrong). A timeout means no response arrived in time — a completely different failure mode that could be DNS, TCP, TLS, or the server taking too long. Knowing which layer failed tells you whose log to look at.
SEE ITPacket journey step-runner
Step through the full request lifecycle, inject failures at each hop, and watch the latency budget accumulate. The latency numbers are calibrated to real-world medians — they will change when you move the location slider.
Notice three things as you step through. First, a single HTTPS request in Sydney costs ~340 ms of pure physics before any application code runs — and that is on a fast connection with no retransmits. Second, injecting a DNS failure produces a different error message than a TCP timeout, which differs from a TLS certificate error: each layer has its own failure signature. Third, the "HTTP 503" failure is the only one where the server was successfully reached — which is also the only one where the server's logs will have an entry. If you can't reach the server's logs, the failure is at a lower layer.
IN PRACTICEThe numbers every engineer should know
Jeff Dean's "Numbers Every Engineer Should Know" (periodically updated at various venues) includes the seminal latency table. The version below reflects modern hardware; the exact numbers drift year to year, but the ratios are stable:
| Operation | Latency | Notes |
|---|---|---|
| L1 cache hit | ~1 ns | on-chip; near-zero cost |
| L2 cache hit | ~4 ns | still on-chip |
| L3 cache access | ~15–40 ns | ~15–40× slower than L1 |
| RAM access | ~60–100 ns | ~60–100× slower than L1 |
| SSD read (NVMe) | ~100 µs | 1,000× slower than RAM |
| Network RTT, same datacenter | ~0.5 ms | mostly switching fabric |
| Network RTT, same continent | ~10–30 ms | fiber + routing |
| Network RTT, cross-continent | ~80–150 ms | trans-oceanic fiber; ~300 ms Sydney↔Virginia |
| Full HTTPS round trip (same city) | ~20–60 ms | DNS + TCP + TLS 1.3 + HTTP |
| Full HTTPS round trip (cross-ocean) | ~600–1,100 ms | Sydney→Virginia, cold DNS cache |
The physics floor: light travels ~200,000 km/s in fiber (two-thirds of vacuum speed due to refractive index). Sydney to Virginia via submarine cables is roughly 16,000 km each way. That is a minimum of 80 ms one-way, 160 ms RTT — just for photons. Then add routing hops, queuing, the TCP handshake, TLS negotiation, and server processing. The 300 ms figure in the widget is a realistic median, not a pessimistic estimate.
You cannot optimize past physics. What you can do: put servers in regions closer to users (CDN, multi-region deploy); reuse connections so you pay the handshake tax once per connection not per request; use TLS 1.3 (one RTT instead of two); cache DNS responses; use HTTP/2 to multiplex multiple requests; and batch small requests. None of these cheat physics — they all reduce the number of round-trips required.
CORE CONCEPTThe eight fallacies of distributed computing
In 1994, L. Peter Deutsch at Sun Microsystems listed the first four assumptions that new distributed systems programmers inevitably make. James Gosling (also at Sun) later expanded the list to eight. They are still a near-perfect checklist of how production systems fail:
The network is reliable
Packets are dropped, cables are cut, switches reboot, ISPs BGP-peer incorrectly. A connection that worked 10 minutes ago may not work now — and may start working again in 30 seconds with no code change. Your code must handle partial failures.
Latency is zero
As the previous section shows, it is not. A function call that crosses a datacenter boundary is 0.5 ms minimum. Cross-continent is 100× that. Code that chatters over a network — dozens of small requests instead of one batched request — pays this cost repeatedly.
Bandwidth is infinite
Practically, it is high — but the combination of many services each doing "a few" requests per second, multiplied by payload size, multiplied by number of instances, creates real egress bills and real saturation. Serialization format (JSON vs Protobuf) and response filtering (GraphQL, sparse fieldsets) exist because of this fallacy.
The network is secure
It is not. Traffic traverses networks owned by strangers. TLS exists to address this, but TLS is only as good as its certificate management — expired certificates cause production outages weekly across the industry.
Topology doesn't change
Servers are added and removed. IP addresses change. DNS TTLs mean old addresses linger. Services that cache TCP connections indefinitely break on server replacement. Kubernetes pods have 15-second lifespans in some configurations.
There is one administrator
In practice, the path from your client to your server crosses networks owned by your cloud provider, multiple transit ISPs, and your user's ISP. None of them coordinate their changes with you.
Transport cost is zero
Cloud egress pricing exists. Cross-AZ traffic costs money. Naively fanning out data across regions can make an application economically unviable.
The network is homogeneous
Your laptop, your CI runner, your prod server, and your user's phone run on different networks with different MTUs, different DNS resolvers, different TCP congestion algorithms, different TLS stacks. "Works on my machine" is a network-topology statement.
These are not historical curiosities. Every major cloud outage post-mortem references at least one of them. When the widget injects a failure and you diagnose it layer by layer, you're building the reflex that corrects fallacy #1.
WEAKNESSWhere this bites
The single most common networking bug: calling requests.get(url) without a timeout argument. If the server accepts the TCP connection but stops sending data mid-response — a crash, a hung thread, a misconfigured proxy — your Python thread waits forever. No exception is raised; the thread simply stops making progress. In TraceLab's context, a find_run that hits a hung remote API would block the entire CLI. Always pass timeout=(connect_s, read_s) — a two-tuple lets you set different limits for connection establishment versus data transfer.
An HTTP 200 means the server understood and responded. It does not mean the server did what you asked. A write API that returns 200 with {"status": "queued"} has not committed your data — it has promised to try. Confusing HTTP-layer success with application-layer success leads to lost writes discovered only when the queue processor crashes. Check the response body; don't just check the status code.
"It can't connect" is five different bugs depending on which layer failed. DNS failure: no IP address found — your exception is socket.gaierror. TCP timeout: IP routable but server not listening — ConnectionRefusedError. TCP timeout mid-handshake: firewall dropping packets silently — TimeoutError after your timeout fires. TLS failure: certificate invalid — ssl.SSLCertVerificationError. HTTP 4xx/5xx: server reachable but refusing. Engineers who can't map exception types to layers start from scratch on every incident instead of going directly to the right log.
KEY INSIGHTHow this connects
- The OS Contract (1.3) — every network call ultimately becomes a syscall:
connect(),send(),recv(). The OS kernel owns the TCP stack; your Python code is asking it for a service, exactly as we saw withwrite()and the file system. - How Data Is Actually Represented (1.5) — bytes go on the wire, not Python strings. Encoding (UTF-8 vs ASCII vs binary) determines what lands in the TCP payload. A TraceLab that logs robot names with non-ASCII characters and posts them to an HTTP API must encode them correctly on both ends.
- TraceLab v0 (1.9) — the project you write in the next lesson is a local script, but the eventual TraceLab will expose an HTTP API and log to a remote store. The latency budget and failure modes you've just internalized are the constraints that design must satisfy.
The network is the last piece of the Stage 1 mental model: execution (1.1–1.3), memory (1.2), the OS (1.3), the shell (1.4), data representation (1.5), cost models (1.6), data structures (1.7), and now the wire (1.8). Stage 1 ends in 1.9 by deliberately building the wrong solution — so its pain can be the syllabus for everything after.
GLOSSARYKey terms introduced
- DNS (Domain Name System)
- A distributed hierarchical database mapping hostnames to IP addresses; responses carry a TTL specifying how long resolvers may cache the answer.
- TCP three-way handshake
- The SYN → SYN-ACK → ACK exchange that establishes a TCP connection; costs one round-trip before any data can flow.
- TLS (Transport Layer Security)
- The cryptographic protocol layered over TCP that provides confidentiality (encryption), integrity (MAC), and server authentication (certificates); TLS 1.3 requires one round-trip.
- Round-trip time (RTT)
- The elapsed time for a packet to travel from sender to receiver and back; sets the hard floor for any protocol that requires acknowledgements.
- TTL (time-to-live)
- In DNS: the number of seconds a resolver may cache a record. In IP: a hop counter decremented by each router to prevent routing loops.
- Certificate Authority (CA)
- A trusted third party whose public key is pre-installed in operating systems and browsers; a CA's signature on a TLS certificate vouches that the issuer verified domain control.
- SNI (Server Name Indication)
- A TLS extension allowing the client to specify which hostname it is connecting to, enabling one IP address to serve multiple TLS certificates — visible in plaintext.
- Fallacies of distributed computing
- Eight assumptions (first four by L. Peter Deutsch, expanded to eight by James Gosling, both at Sun Microsystems) that developers incorrectly make about networks: reliability, zero latency, infinite bandwidth, security, static topology, single administration, zero transport cost, homogeneity.
ACTIVE RECALLCheck your understanding
Answer in your own words first, then reveal.
1Name the four protocol-layer steps between typing requests.get("https://api.example.com/data") and your code receiving a response body.›
1. DNS resolution — a separate UDP request translates api.example.com to an IP address. 2. TCP three-way handshake — SYN/SYN-ACK/ACK establishes a reliable connection (one RTT). 3. TLS handshake — one to two RTTs to negotiate cipher, exchange certificates, and derive session keys. 4. HTTP request/response — the actual application-layer exchange. Each layer adds to the latency total; each can fail independently.
2Why can't you engineer a Sydney-to-Virginia HTTPS response time below roughly 160 ms, no matter how optimized your code is?›
The physical distance is ~16,000 km. Light travels at ~200,000 km/s in fiber (two-thirds of vacuum speed). That is an 80 ms one-way trip minimum — 160 ms round-trip — just for the photons. Any protocol step that requires acknowledgement (TCP handshake, TLS handshake, HTTP response) must wait at least one RTT. No amount of algorithmic optimization changes the speed of light. This is why geographic distribution (CDN, multi-region deploy) is the only lever for cross-continental latency.
3Your Python script throws ssl.SSLCertVerificationError. What does that tell you about which layer of the request succeeded and which failed?›
DNS resolved successfully (you got an IP). TCP connected successfully (the handshake completed — otherwise you'd have a ConnectionRefusedError or TimeoutError). The failure is at the TLS layer: the server's certificate was either expired, self-signed with no trusted CA, issued for a different hostname (SNI mismatch), or the chain is broken. The server is reachable — but its identity could not be verified. This immediately narrows the investigation to certificate management, not networking or firewall rules.
4TraceLab's requests.get(url) call hangs indefinitely in production. What is the most likely cause, and what is the one-line fix?›
The server accepted the TCP connection but stopped responding mid-transfer — a crash, a blocked thread, or a misconfigured proxy. With no timeout set, Python's socket read blocks forever. The fix: requests.get(url, timeout=(5, 30)) — 5 seconds to establish the connection, 30 seconds to receive data between chunks. When the timeout fires, a requests.exceptions.Timeout exception is raised and you can retry or fail fast, rather than hanging the process indefinitely.