Networking Basics

Everything between the browser and your load balancer: how a request is resolved, connected, secured, and delivered — and where the milliseconds go.

DNS TCP / UDP TLS HTTP/1.1 → 3 CDN
đŸ”Ŧ

Anatomy of a Request

Before a single line of your handler executes, four things had to happen: the name resolved to an address, a TCP connection opened, a TLS session negotiated, and the HTTP request crossed the wire. Each is a round trip or more. On a fresh connection to a distant server, you can spend 300ms on setup before the server even sees the request.

Knowing where that time goes is the point. Most front-end latency wins come from cutting round trips out of this path, not from faster application code.

  Browser                                                        Server
    │                                                              │
    │  1. DNS lookup   example.com ──â–ļ 93.184.x.x                  │   ~20–120ms
    │     (may be cached; skip if warm)                            │   (first time)
    │                                                              │
    │  2. TCP handshake   SYN ─â–ļ ◀─ SYN-ACK   ACK ─â–ļ               │   1 round trip
    │                                                              │
    │  3. TLS handshake   ClientHello ─â–ļ ◀─ ... ─â–ļ                 │   1 RT (TLS 1.3)
    │                                                              │   2 RT (TLS 1.2)
    │  4. HTTP request    GET / ────────────────────────â–ļ          │
    │                     ◀──────────────────────── 200 OK         │   TTFB
    │                                                              │
    â–ŧ  first byte arrives                                          â–ŧ

  TTFB = time to first byte = setup + one request/response round trip.
💡
The reason keep-alive, connection pooling, and CDNs matter is all visible here: they let you skip steps 1–3. A warm connection to a nearby edge turns a 300ms cold start into a single short round trip. "Reuse the connection" is one of the cheapest latency wins you can name in an interview.
🧭

DNS: Names to Addresses

DNS turns example.com into an IP. A recursive resolver (usually your ISP's or something like 8.8.8.8) does the legwork, walking from the root to the authoritative nameserver that actually owns the record, then caching the answer for its TTL. Most lookups never leave the resolver's cache.

DNS is also a load-balancing and routing tool. Return multiple A records and clients spread across them. Return different answers based on the resolver's location — GeoDNS — and you send each user to their nearest region.

What TTL buys and costs the staleness knob

A long TTL means fewer lookups and faster page loads, but slow propagation: change a record and old answers linger until caches expire. A short TTL flips it — quick failover, more lookup traffic.

This is why DNS is a weak failover mechanism. You can't yank traffic off a dead server faster than clients honor the TTL, and some ignore it entirely.

GeoDNS and its limit route by location

The authoritative server picks an answer based on where the resolver sits, sending users to a nearby region. Cheap global routing with no extra hop.

The catch: it sees the resolver, not the user. Someone on a distant public resolver gets routed by the resolver's location, not their own. For precise routing, anycast beats it (Discovery).

âš ī¸
Don't lean on DNS for fast failover or fine-grained load balancing. TTLs are honored inconsistently, and propagation is measured in minutes. Use it for coarse regional routing and let a real load balancer handle health and failover inside the region.
đŸ“Ļ

TCP vs UDP

TCP gives you a reliable, ordered byte stream: it handshakes to open a connection, acknowledges every segment, retransmits losses, and slows down under congestion. All of that costs round trips and head-of-line blocking — a lost packet stalls everything behind it until it's resent.

UDP gives you none of it. Fire a datagram and hope. No handshake, no ordering, no retransmit. That's a feature when late data is useless: in a video call, a dropped frame should be skipped, not resent stale.

PropertyTCPUDP
ConnectionHandshake first (1 RT)None — just send
DeliveryGuaranteed, retransmittedBest-effort, may drop
OrderingIn orderNo guarantee
Head-of-line blockingYes — a loss stalls the streamNo — packets independent
OverheadHigher (state, acks, congestion control)Minimal
FitsWeb, APIs, databases, anything correctness-firstVideo, voice, gaming, DNS, QUIC
â„šī¸
The interesting modern move is QUIC: it runs on UDP but rebuilds reliability, ordering, and encryption in user space — keeping TCP's guarantees while dodging its head-of-line blocking and slow handshakes. HTTP/3 rides on it. So "UDP" no longer means "unreliable" by default.
đŸ”ĸ

HTTP Versions

The through-line across HTTP versions is a war on round trips and head-of-line blocking. Each version removed a place where requests had to wait in line.

  HTTP/1.1   one request at a time per connection. Response N blocks N+1.
             Workaround: open 6 parallel connections and keep them alive.
             â–ŧ still blocks: slow response holds up its whole connection

  HTTP/2     multiplexing — many streams over ONE connection, interleaved.
             Header compression. Server push (mostly unused).
             â–ŧ but all streams share one TCP stream: a lost packet
               stalls every stream (TCP-level head-of-line blocking)

  HTTP/3     runs on QUIC (UDP). Streams are independent, so one lost
             packet stalls only its own stream. Faster handshake
             (crypto + transport in one). 0-RTT resumption.
VersionTransportFixedStill limited by
HTTP/1.1TCPKeep-alive, pipeliningOne in-flight response per connection
HTTP/2TCPMultiplexing, header compressionTCP head-of-line blocking across streams
HTTP/3QUIC / UDPIndependent streams, faster handshake, 0-RTTNewer, some middleboxes block UDP
💡
You rarely need to design around HTTP versions, but naming the progression signals depth: "HTTP/2 solved application-layer head-of-line blocking but not the TCP-layer version; HTTP/3 moves to QUIC to fix that too." One sentence, and it lands.
🔒

TLS: The Encryption Handshake

TLS encrypts the connection and proves the server's identity via a certificate. The cost is the handshake: one extra round trip in TLS 1.3, two in the older TLS 1.2. On a cold connection that's added latency on top of TCP setup.

Two mitigations matter. Session resumption lets a returning client skip the full handshake with a cached key — even 0-RTT, sending data on the first packet. And TLS termination at the load balancer means the expensive crypto happens once at the edge, not on every app server.

            Public internet                 Your network (trusted)
   Client ═══════════════════════â–ļ  Load Balancer ──────────────â–ļ  App servers
          encrypted (TLS)              │  decrypts here            plain HTTP
                                       │  (TLS termination)        (or re-encrypted
                                       â–ŧ                            for zero-trust:
                              certs, crypto centralized             mTLS everywhere)

Terminating TLS at the edge simplifies certificate management and offloads CPU, but it means traffic inside your network is plaintext by default. Whether that's acceptable depends on your trust model — a zero-trust design re-encrypts internally with mTLS (Security Basics).

âš ī¸
"TLS terminates at the load balancer" is a load-bearing sentence. It answers where certs live, why internal calls can be plain HTTP, and where the crypto cost is paid. But say it and you've implicitly decided your internal network is trusted — be ready to defend that or add mTLS.
🌍

CDNs and Edge Delivery

A CDN is a fleet of edge servers (PoPs) spread across the globe, each caching copies of your content. The user hits the nearest edge instead of your origin, cutting latency and offloading traffic. For static and cacheable content, this is the single biggest delivery win available — and the dominant cost line for anything media-heavy (Video Streaming).

Pull vs push how content lands on the edge

Origin pull: the edge fetches from your origin on the first miss, then caches. Lazy, self-managing, the default.

Push: you upload content to the CDN ahead of demand. More control, used for large predictable assets like video segments.

Cache key and invalidation the hard half

The edge keys on URL (plus chosen headers). Change content at the same URL and edges keep serving the old copy until the TTL expires or you purge it.

The standard fix is versioned URLs — app.a1b2c3.js — so new content gets a new key and old caches simply age out.

Put on a CDN what's static or reusable across users: images, video, CSS/JS, fonts, and cacheable API responses. Keep off it anything per-user or sensitive unless you gate access. For private content, signed URLs grant time-limited access to a specific object without making it public — the same mechanism that lets clients pull a paywalled video segment straight from the edge.

â„šī¸
The CDN is the first cache in a read's journey — browser, then CDN, then your gateway and application caches, then the database. It's the same caching logic applied at the edge, so the failure modes rhyme: staleness, invalidation, and cold-miss storms all live here too (Caching).
📋

Quick Reference

StepWhat it costsHow to cut it
DNS lookup~20–120ms cold, ~0 warmCaching + TTL tuning; keep it for coarse routing
TCP handshake1 round tripKeep-alive, connection pooling
TLS handshake1 RT (1.3), 2 RT (1.2)Session resumption, 0-RTT, edge termination
Request / response1 RT to origin (TTFB)CDN edge, HTTP/2+3 multiplexing
TCPUDP
Reliable, ordered, congestion-controlledBest-effort, unordered, minimal
Web, APIs, DBs — correctness firstVideo, voice, gaming, DNS, QUIC/HTTP3
â„šī¸
The one-liners to keep ready. DNS: coarse routing and caching, not fast failover. TCP vs UDP: reliability you pay round trips for, or speed you drop packets for. HTTP versions: a steady war on head-of-line blocking, ending at QUIC. TLS: terminate at the edge, decide if the inside is trusted. CDN: the first and closest cache — serve static content from the edge and keep egress off your origin.