HV
home / core-cs / networking

Networking Fundamentals

OSI/TCP-IP layers, TCP vs UDP, HTTP evolution, DNS, and load balancing — the vocabulary every system design answer is built on.

1. OSI Model (7 Layers)

#LayerResponsibilityExamples
7 Application Interface for end-user applications HTTP, DNS, FTP, SMTP
6 Presentation Data translation, encryption, compression TLS/SSL, JPEG, gzip
5 Session Establishes/manages/terminates sessions Sockets, RPC sessions
4 Transport End-to-end delivery, reliability, flow control TCP, UDP
3 Network Routing between networks via logical addressing IP, ICMP, routers
2 Data Link Node-to-node delivery on the same network Ethernet, MAC address, switches
1 Physical Raw bit transmission over a medium Cables, radio, Wi-Fi signals

The practical model most engineers actually use is TCP/IP (4 layers): Application (HTTP/DNS) → Transport (TCP/UDP) → Internet (IP) → Link (Ethernet/Wi-Fi). OSI is mostly a teaching/reference model.

2. TCP vs UDP

TCP (Transmission Control Protocol)
  • · Connection-oriented (handshake required)
  • · Guarantees delivery, ordering, no duplication
  • · Flow control + congestion control (slow start)
  • · Higher overhead, higher latency
Use: HTTP, file transfer, databases, chat — anything needing correctness
UDP (User Datagram Protocol)
  • · Connectionless — fire and forget
  • · No delivery/order guarantees, no retransmission
  • · Minimal overhead, very low latency
  • · App must handle loss/reordering if it cares
Use: video/voice calls, live streaming, DNS, gaming — speed > perfect accuracy
TCP 3-way Handshake (connection setup): Client Server |----------- SYN (seq=x) --------------->| |<----- SYN-ACK (seq=y, ack=x+1) --------| |----------- ACK (ack=y+1) -------------->| | connection ESTABLISHED | TCP 4-way Termination (connection teardown): Client Server |----------------- FIN ------------------>| |<---------------- ACK -------------------| |<---------------- FIN --------------------| |----------------- ACK ------------------->| | client waits in TIME_WAIT, then closes|

3. HTTP Evolution

VersionKey ChangeTrade-off
HTTP/1.0 New TCP connection per request. Simple, but connection setup overhead on every request.
HTTP/1.1 Persistent connections (keep-alive), pipelining. Reuses TCP connections, but pipelining suffers from head-of-line blocking.
HTTP/2 Multiplexing — many requests over 1 TCP connection, binary framing, header compression (HPACK), server push. Fixes app-layer head-of-line blocking, but a single lost TCP packet still stalls all multiplexed streams (TCP-level HOL blocking).
HTTP/3 Runs over QUIC (UDP-based) instead of TCP. Streams are independent at the transport layer. Solves TCP head-of-line blocking entirely; faster connection setup (0-RTT). Needs UDP support through firewalls/NATs.
Common Status Codes
200 OK · 201 Created · 204 No Content
301 Moved Permanently · 302 Found · 304 Not Modified
400 Bad Request · 401 Unauthorized · 403 Forbidden · 404 Not Found · 429 Too Many Requests
500 Internal Server Error · 502 Bad Gateway · 503 Service Unavailable
HTTPS / TLS Handshake (simplified)

Client Hello (supported ciphers) → Server Hello + certificate → Client verifies cert against CA, generates a symmetric session key encrypted with the server's public key → both switch to fast symmetric encryption for the actual data.

4. DNS Resolution

GET request for www.example.com typed in browser | 1. Check browser cache -> OS cache -> router cache (fast paths, TTL-based) | (all miss) v 2. Query configured DNS Resolver (e.g. ISP or 8.8.8.8) | 3. Resolver -> Root Name Server returns: "ask the .com TLD server" | 4. Resolver -> TLD Name Server (.com) returns: "ask example.com's authoritative NS" | 5. Resolver -> Authoritative Name Server for example.com | returns: A record -> 93.184.216.34 v 6. Resolver caches the result (per TTL) and returns IP to browser | 7. Browser opens TCP connection to 93.184.216.34 -> sends HTTP request

Record types: A (IPv4), AAAA (IPv6), CNAME (alias to another domain), MX (mail server), TXT (verification/SPF), NS (name server delegation).

5. Load Balancing, Proxies & CDN

LB AlgorithmHowWhen it shines
Round Robin Requests distributed sequentially across servers. Servers are homogeneous and requests are roughly uniform cost.
Least Connections Send to the server with fewest active connections. Requests have variable duration (e.g. long-lived WebSocket connections).
IP Hash Hash client IP to consistently route to the same server. Session affinity needed without a shared session store.
Weighted Round Robin Servers get traffic proportional to assigned weight. Heterogeneous server capacity (some bigger instances than others).
Forward Proxy vs Reverse Proxy

Forward proxy sits in front of clients, hiding client identity from servers (e.g. corporate proxy, VPN). Reverse proxy sits in front of servers, hiding server identity/topology from clients (e.g. Nginx, load balancer, API gateway) — also handles TLS termination, caching, compression.

CDN

Geographically distributed edge servers cache static (and sometimes dynamic) content closer to users. Reduces latency (avoids cross-region round trips) and offloads origin servers. Cache invalidation is the classic hard problem (TTL-based or explicit purge).

6. Common Interview Questions

Q: Why does HTTP/2 use a single TCP connection instead of multiple?

A: Multiple TCP connections each need their own slow-start ramp-up and consume separate OS resources; multiplexing multiple logical streams over one connection avoids that overhead and lets the browser make many concurrent requests without the old '6 connections per domain' hack.

Q: What is head-of-line blocking and how does QUIC (HTTP/3) fix it?

A: In TCP, if one packet is lost, all data behind it (even for unrelated streams) must wait for retransmission before the OS delivers anything to the application — because TCP guarantees strict in-order bytes on a single connection. QUIC runs over UDP and manages its own independent streams, so a lost packet only blocks its own stream, not others.

Q: What happens between typing a URL and the page loading?

A: DNS resolution → TCP handshake (+ TLS handshake if HTTPS) → HTTP request sent → server processes and responds → browser parses HTML, discovers additional resources (CSS/JS/images), fetches them (often in parallel over HTTP/2) → renders the page.

Q: How would you design a system that needs strict message ordering — TCP or UDP?

A: TCP, because it guarantees in-order, reliable delivery on a single connection. If using UDP for latency reasons (e.g. video), the application layer must add its own sequence numbers and reordering/dedup logic.

Q: Why can't you just increase the number of DNS root servers infinitely to scale?

A: In practice DNS scales via caching (TTL) and anycast — the 13 root server 'names' are actually served by hundreds of physical anycast nodes worldwide, so a client's request is routed to the nearest instance rather than a single physical machine.