📒 problems 📖 glossary
Mock interview
46:30
ready
of 46:30
Lesson 57 · Systems

Networking — the floor under everything

📖 Walk me through it — plain English

Networking is just the set of agreed-upon rules ("protocols") that let two computers send bytes to each other across the internet. This lesson stacks up the layers you actually get asked about: how a connection is opened (TCP/UDP), how requests are shaped (HTTP), how they're encrypted (TLS), and how a human-friendly name like example.com becomes a numeric address (DNS). You don't need to memorize bit-level details — interviewers want to hear that you understand the trade-offs and the order things happen in.

Quick analogy for the two ways to send data. TCP (Transmission Control Protocol) is like a phone call: you dial, the other side picks up, you both confirm you can hear each other, and then you talk knowing every word arrives in order — if something's garbled it gets repeated. UDP (User Datagram Protocol) is like shouting across a noisy room: no "hello, can you hear me?" setup, you just fire off messages. Some may get lost, but it's fast and cheap — perfect for live video or voice, where a dropped frame from a second ago is worthless anyway.

That "dial and confirm" part of TCP is the 3-way handshake, and it's a favorite question. A SYN is a "synchronize — let's start talking" packet; an ACK is "acknowledged — got it." Three messages establish a reliable channel before any real data moves:

Step 1 · Client says "I want to talk" by sending SYN to the server.
C
SYN→
S
Step 2 · Server replies SYN/ACK: "got your request, and I want to talk too."
C
←SYN/ACK
S
Step 3 · Client sends final ACK. Channel is open — now real data flows.
C
ACK→ ✓
S

Now zoom out to what happens when you type a URL and hit enter — the whole stack in one trip. (1) DNS (Domain Name System) turns the name into an IP address: your machine asks a "recursive resolver," which walks the hierarchy root → TLD (the .com servers) → the authoritative server for that domain, and the answer gets cached for a while (its TTL, "time to live"). (2) TCP does the 3-way handshake to that IP. (3) TLS (Transport Layer Security, the S in HTTPS) runs its own handshake: the server proves who it is with a certificate signed by a CA (Certificate Authority, a trusted issuer your browser already trusts), and both sides agree on a shared secret key so everything afterward is encrypted and tamper-checked. (4) Finally your HTTP request goes out — a method like GET (read something) or POST (create something) — and the server answers with a status code (2xx = worked, 4xx = you messed up, 5xx = the server messed up).

Why the design makes sense: TCP pays an upfront cost (the handshake round-trip) to buy reliability and ordering, which is exactly what you want for a webpage or a database query. UDP skips that cost when speed beats correctness. TLS adds one more round-trip (just one in the modern TLS 1.3) to make the channel private. And DNS caching means most lookups never hit the full hierarchy — the answer is already sitting nearby. Each layer solves one problem and hands clean bytes up to the next, which is the whole reason the internet composes so well.

Start here: what a "network request" actually is

Before any jargon, hold one picture in your head. Every interaction on the internet is a request sent from a client (your browser, your phone app, one of your servers calling another) to a server, followed by a response coming back. That is the whole game: ask, then receive. Everything in this lesson is machinery in service of getting one request safely and correctly from A to B, and the answer back.

A few words you'll see constantly, defined once up front so nothing later is a mystery:

  • An IP address (Internet Protocol address) is the numeric "house number" of a machine on the network — e.g. 93.184.216.34 for IPv4, or a longer hex form like 2606:2800:220:1:248:1893:25c8:1946 for IPv6. Every packet carries a source and destination IP so routers know where to forward it.
  • A port is a number (0–65535) that says which program on that machine the request is for. The IP gets you to the building; the port gets you to the right office door. Web servers listen on port 80 (HTTP) or 443 (HTTPS) by convention; a database might listen on 5432.
  • A packet is one small chunk of data with addressing info attached. Big messages are split into many packets, sent independently, and reassembled at the other end.
  • A protocol is just a rulebook both sides agree to follow — what the bytes mean, in what order, and how to handle errors. TCP, UDP, HTTP, TLS, and DNS are all protocols.
  • Latency is the delay for one round trip — how long until the first byte comes back, measured in milliseconds. Bandwidth (or throughput) is how much data you can move per second once the pipe is open. They're independent: a satellite link can have huge bandwidth but terrible latency. Most "the site feels slow" problems are latency, not bandwidth — which is why cutting round trips (DNS caching, TLS 1.3, keep-alive connections, CDNs) is where the wins are.

The layers, in one minute (OSI / TCP-IP model)

Networking is taught as a stack of layers, each one only responsible for a single job and handing clean output up to the next. The classic textbook model is OSI (Open Systems Interconnection), seven layers; the practical model engineers actually use is TCP/IP, four layers. You will not be quizzed on all seven names, but the idea — separation of concerns — is the point, and naming the right layer for a thing reads as competence.

The four layers you actually use (TCP/IP)
  • Link layer — the physical hop: Ethernet, Wi-Fi, getting bits onto the wire to the next device. You rarely touch this.
  • Internet layer (IP) — addressing and routing packets across networks using IP addresses. Best-effort only: it does not promise delivery or order.
  • Transport layer (TCP / UDP) — turns best-effort packets into something usable: TCP adds reliability + ordering; UDP stays bare and fast. Ports live here.
  • Application layer (HTTP, DNS, TLS…) — the protocols your code speaks. This is where requests, responses, methods, and status codes live.

Rule of thumb: a "404" is an application-layer problem, a "connection refused" is transport-layer, and "host unreachable" is internet-layer. Knowing which layer broke is half of debugging.

TCP vs UDP
  • TCP: connection-oriented, reliable, ordered. 3-way handshake, retransmits. HTTP, DB protocols.
  • UDP: no connection, no guarantees. Tiny overhead. DNS, video/voice (where stale data is useless), QUIC base.
TCP 3-way handshake
client → SYN     → server
client ← SYN/ACK ← server
client → ACK     → server   then data flows

To make the trade-off concrete: TCP (Transmission Control Protocol) is "connection-oriented" — both sides agree to a connection first (the handshake), then TCP numbers every byte, waits for acknowledgements, and retransmits anything lost so data arrives complete and in order. That guarantee costs you at least one round trip before the first byte and some bookkeeping overhead. UDP (User Datagram Protocol) is "connectionless" — it just fires datagrams (self-contained packets) with no setup, no acknowledgements, and no reordering. You trade reliability for speed and simplicity. Choose TCP when correctness matters (web pages, APIs, databases, file transfer); choose UDP when freshness beats completeness (live video/voice, online games, DNS lookups). Modern QUIC (the basis of HTTP/3) cleverly builds reliability on top of UDP so it gets TCP-like guarantees without TCP's rigidity.

The 3-way handshake, defined step by step

The diagram above shows the shape; here is what each message means, because interviewers love to ask "why three, not two?" The handshake exists so both sides confirm they can send and receive, and so both agree on the starting sequence numbers TCP uses to order bytes.

  • SYN (synchronize) — the client opens with "I want to talk, and here's my starting sequence number." Now the server knows the client can send.
  • SYN/ACK — the server answers with its own SYN plus an ACK of the client's. This proves the server received the client (so the client→server path works) and announces the server's sequence number.
  • ACK (acknowledge) — the client confirms it received the server's SYN. Now the server knows the server→client path works too. Both directions verified ⇒ the connection is established and data flows.

Two messages can't do it: after two, the server has confirmed the client can reach it, but the client has no confirmation the server's reply got through. The third ACK closes that loop. (Closing a connection is symmetric — a FIN/ACK exchange, often four messages — but the handshake is the part that gets asked.)

HTTP essentials
  • Methods: GET (read, idempotent, cacheable) · POST (create) · PUT (replace, idempotent) · PATCH (partial update) · DELETE (idempotent).
  • Status codes: 2xx success · 3xx redirect (301 permanent vs 302 temporary) · 4xx client (401 auth, 403 forbidden, 404 not found, 429 rate-limited) · 5xx server.
  • Headers: Cache-Control, ETag, Authorization, Content-Type.
  • HTTP/2: multiplexing on one TCP connection (no head-of-line at the app layer). HTTP/3 uses QUIC over UDP (no TCP HOL either).

HTTP (HyperText Transfer Protocol) is the application-layer language the web speaks: a request goes up, a response comes back, both as plain text headers followed by an optional body. A few terms in that box deserve unpacking:

  • An HTTP method (or "verb") states your intent. GET reads without side effects; POST creates or submits; PUT replaces a whole resource; PATCH tweaks part of it; DELETE removes it.
  • Idempotent means "doing it twice has the same effect as doing it once." GET, PUT, and DELETE are idempotent (re-sending is safe); POST is not (re-sending may create a duplicate). This is why a browser warns before re-submitting a form — that's a repeated POST.
  • A status code is the server's three-digit verdict. Memorize the families, not every number: 2xx = success (200 OK, 201 Created), 3xx = redirect (301 permanent, 302 temporary, 304 Not Modified = "use your cache"), 4xx = the client's fault (400 bad request, 401 unauthenticated, 403 forbidden, 404 not found, 429 too many requests), 5xx = the server's fault (500 internal error, 502 bad gateway, 503 unavailable).
  • Headers are key/value metadata riding alongside the body: Content-Type says what format the body is, Authorization carries credentials, Cache-Control and ETag drive caching (an ETag is a fingerprint of the content so the server can answer "unchanged, 304" cheaply).

Versions matter for one reason: cutting round trips and head-of-line blocking. HTTP/1.1 sends one request at a time per connection (slow, so browsers open many). HTTP/2 multiplexes many requests over a single TCP connection at once. HTTP/3 runs over QUIC/UDP to dodge a subtle TCP penalty (see pitfalls below).

TLS (HTTPS) — what handshake achieves
  1. Client + server negotiate cipher suite.
  2. Server proves identity via certificate (signed by a CA the client trusts).
  3. Both derive a shared symmetric key (via ECDHE — forward secrecy).
  4. All further traffic encrypted + integrity-checked with that key.

TLS 1.3 finishes in 1 round-trip (vs 2 for TLS 1.2).

Plain English for that box: TLS (Transport Layer Security) is what turns HTTP into HTTPS — the "S" is "Secure." It does three jobs at once. First, confidentiality: traffic is encrypted so a snooper on the Wi-Fi sees only gibberish. Second, integrity: every message is tamper-checked, so nobody can quietly alter it in transit. Third, authentication: you're really talking to the site you think you are. That last one rests on certificates. A certificate is a signed document binding a domain name to a public key; it's signed by a Certificate Authority (CA), an organization your browser/OS already trusts out of the box. The chain of trust is: your browser trusts the CA → the CA signed this site's certificate → therefore the browser trusts the site. If the signature is missing, expired, or doesn't match the domain, you get the scary "Your connection is not private" warning. The shared symmetric key both sides derive (cheap to encrypt with) is used for the actual data; the certificate's key is only used during the handshake. That separation is what gives forward secrecy: each session uses a fresh ephemeral key, so stealing the server's long-term key later can't decrypt yesterday's captured traffic.

DNS
  • Hierarchical: root → TLD (.com) → authoritative for example.com.
  • Recursive resolver does the chain, caches per TTL.
  • Common records: A (IPv4), AAAA (IPv6), CNAME (alias), MX (mail), TXT (verification), NS (delegation).

Spelled out: DNS (Domain Name System) is the internet's phone book — it translates a name humans can remember (example.com) into the IP address machines route to. It's hierarchical and read right-to-left. A recursive resolver (usually run by your ISP, or a public one like 1.1.1.1) does the legwork for you: it asks a root server "who handles .com?", then asks that TLD (top-level domain) server "who's authoritative for example.com?", then asks that authoritative server for the actual record. The answer comes back with a TTL (time to live) — how many seconds it may be cached — so the next lookup skips most of the walk. The records themselves come in types: A (name → IPv4), AAAA (name → IPv6), CNAME (an alias pointing one name at another), MX (where mail for the domain goes), TXT (free-form text, often for domain-ownership verification), and NS (which servers are authoritative — "delegation").

The full trip: typing a URL, step by step

This is the single most common networking interview question — "what happens when you type a URL and press enter?" The on-ramp above gave the shape; here is the same journey broken into named, defined steps you can recite. We'll trace https://example.com/page.

  1. Parse the URL. The browser splits it into scheme (https ⇒ use TLS, default port 443), host (example.com), and path (/page).
  2. DNS resolution. The browser needs example.com's IP. It checks its own cache, then the OS cache, then asks the recursive resolver, which (if nothing is cached) walks root → TLD → authoritative and returns an IP with a TTL. Result: a numeric address to connect to.
  3. TCP connection. The browser opens a TCP connection to that IP on port 443 via the 3-way handshake (SYN → SYN/ACK → ACK). One round trip; now there's a reliable, ordered byte channel.
  4. TLS handshake. Over that channel, client and server negotiate a cipher, the server presents its certificate, the browser verifies the CA chain and that the cert matches example.com, and both derive a shared key. After this the channel is encrypted. (TLS 1.3: one round trip.)
  5. HTTP request. The browser sends GET /page HTTP/2 with headers (Host, User-Agent, Accept, any cookies). This is the actual "ask."
  6. Server processing. The request may hit a load balancer first, get routed to one of many app servers, possibly served from a CDN edge cache, and the app may query a database before composing a response.
  7. HTTP response. Back comes a status code (hopefully 200 OK), response headers, and the body (HTML). A 3xx would send the browser to a new URL; a 4xx/5xx would be an error page.
  8. Render. The browser parses the HTML, discovers more resources (CSS, JS, images), and fires off more requests — often reusing the same connection — until the page is painted.

Two infrastructure pieces from step 6 are worth defining because they show up in every system-design round. A load balancer sits in front of a pool of servers and spreads incoming requests across them (round-robin, least-connections, or by hashing a key), so no single box is overwhelmed and a dead one can be dropped without users noticing. A CDN (Content Delivery Network) is a fleet of caching servers placed physically close to users around the world; it serves static assets (and increasingly dynamic ones) from the nearest "edge," which slashes latency because the bytes travel a short distance instead of crossing an ocean to your origin server.

Pitfalls & gotchas

  • DNS caching and TTL. A record's TTL is a double-edged sword. A long TTL means fast lookups but a slow rollout: after you change where a domain points, clients and resolvers keep using the stale IP until their cached copy expires — sometimes hours. The fix when you know a migration is coming: lower the TTL days in advance, cut over, then raise it again. "Why didn't my DNS change take effect?" is almost always a cache that hasn't expired.
  • Head-of-line (HOL) blocking. When messages must be delivered in order, one stuck message stalls everything queued behind it — like one slow shopper freezing a single checkout lane. HTTP/1.1 has it at the application layer (one slow response blocks the connection). HTTP/2 fixed that with multiplexing — but a deeper version lurks at the TCP layer: because TCP guarantees order, a single lost packet makes TCP withhold all later data until the retransmit arrives, stalling every multiplexed stream. HTTP/3 over QUIC/UDP solves this by giving each stream independent delivery, so one lost packet only stalls its own stream.
  • Latency adds up across round trips. DNS + TCP + TLS can be three or four sequential round trips before the first byte of HTML. Across an ocean (~150ms each way) that's most of a second spent on setup alone. This is why connection reuse (keep-alive), TLS 1.3, session resumption, and CDNs matter — each one removes a round trip.
  • Idempotency and retries. Networks fail mid-request, so clients retry. Retrying a GET or PUT is safe (idempotent); blindly retrying a POST can double-charge a card or create duplicates. Real systems use idempotency keys to make POST safe to retry.
  • Expired/mismatched certificates. The most common "site is down" page is a TLS cert that expired or doesn't cover the exact hostname (e.g. www. vs apex). Browsers hard-fail rather than warn-and-continue, by design.

Why it matters for interviews: "what happens when you type a URL" and "TCP vs UDP" are near-guaranteed in any backend or system-design screen — they're a quick proxy for whether you understand the machine under your code. You don't need bit-level detail; you need to name the layers in order (DNS → TCP → TLS → HTTP), state the one trade-off each makes (reliability vs speed, privacy vs a round trip, caching vs freshness), and connect them to design decisions (a CDN for latency, a load balancer for scale, idempotent methods for safe retries). Saying "I'd put a CDN in front to cut latency, and DNS TTL lets me fail over" signals you think about networks the way they actually get operated.

Next up, see how these pieces assemble into real architectures in System design — load balancers, CDNs, and caching all reappear there as building blocks.

Go deeper (optional): the canonical references are Kurose & Ross's Computer Networking: A Top-Down Approach for the layered model, and the IETF RFCs for the protocols themselves (RFC 9293 for TCP, RFC 8446 for TLS 1.3, RFC 9110 for HTTP semantics). High Performance Browser Networking by Ilya Grigorik is excellent on the round-trip costs above.

Takeaway: a request travels down a stack of single-purpose layers and back up. DNS turns a name into an IP; TCP opens a reliable ordered channel with the 3-way handshake (UDP skips all that for speed); TLS makes the channel private, tamper-proof, and authenticated via a CA-signed certificate; HTTP carries the actual ask (a method) and answer (a status code). Every optimization is really about killing round trips — caching, TLS 1.3, keep-alive, CDNs — because latency, not bandwidth, is usually what you feel.

→ Going deeper: TCP and DNS become retries, timeouts, and circuit breakers in production. See Distributed systems in practice.