Networking Basics
TL;DR (30-second summary)
The internet runs on protocols: TCP (reliable), UDP (fast), HTTP (web), WebSockets (real-time). DNS translates names to IPs. CDN caches content close to users. Load balancers distribute traffic.
Key concept: Network is unreliable and slow - design for it.
Why This Matters
In interviews: You'll design systems that communicate over networks. Understanding protocols helps you choose the right tool and discuss latency/reliability trade-offs.
At work: Every distributed system depends on networking. Poor choices cause cascading failures.
Core Concepts
1. OSI Model (Simplified)
What matters for system design:
- Layer 4 (Transport): TCP vs UDP
- Layer 7 (Application): HTTP, WebSockets, gRPC
2. TCP vs UDP
| Feature | TCP | UDP |
|---|---|---|
| Reliability | Guaranteed delivery, ordered | Best effort, no guarantee |
| Connection | Connection-oriented (3-way handshake) | Connectionless |
| Speed | Slower (overhead for reliability) | Faster (no handshake) |
| Use Cases | HTTP, database connections, file transfer | Video streaming, DNS, gaming |
| Overhead | ~40 bytes per packet | ~8 bytes per packet |
TCP Three-Way Handshake:
When to use:
- TCP: When you need reliability (web requests, APIs, databases)
- UDP: When speed matters more than reliability (live video, VoIP, gaming)
Say: "We'll use TCP for API calls since we need reliability, but UDP for real-time video streaming where occasional packet loss is acceptable."
3. HTTP/HTTPS
HTTP (HyperText Transfer Protocol): The foundation of the web.
Request structure:
GET /api/users/123 HTTP/1.1
Host: api.example.com
Authorization: Bearer <token>
Content-Type: application/json
Response structure:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 85
{"id": 123, "name": "John", "email": "john@example.com"}
HTTP Methods:
| Method | Purpose | Idempotent | Safe |
|---|---|---|---|
| GET | Retrieve data | ✅ | ✅ |
| POST | Create resource | ❌ | ❌ |
| PUT | Update/replace | ✅ | ❌ |
| PATCH | Partial update | ❌ | ❌ |
| DELETE | Remove resource | ✅ | ❌ |
HTTP Status Codes (memorize these):
- 2xx Success: 200 OK, 201 Created, 204 No Content
- 3xx Redirection: 301 Moved Permanently, 302 Found, 304 Not Modified
- 4xx Client Error: 400 Bad Request, 401 Unauthorized, 404 Not Found, 429 Too Many Requests
- 5xx Server Error: 500 Internal Server Error, 503 Service Unavailable
HTTP/1.1 vs HTTP/2 vs HTTP/3:
| Feature | HTTP/1.1 | HTTP/2 | HTTP/3 |
|---|---|---|---|
| Protocol | TCP | TCP | QUIC (UDP) |
| Multiplexing | ❌ (1 request/connection) | ✅ (many streams) | ✅ |
| Header Compression | ❌ | ✅ (HPACK) | ✅ (QPACK) |
| Latency | High (head-of-line blocking) | Better | Best |
| Adoption | Universal | Common (50% of web) | Growing |
4. HTTPS (HTTP + TLS/SSL)
HTTPS = HTTP + Encryption
Why HTTPS:
- Encryption: Can't eavesdrop on traffic
- Authentication: Verify server identity via certificates
- Integrity: Detect tampering
Cost: ~100ms extra latency for TLS handshake (first request only)
5. WebSockets
Purpose: Full-duplex (two-way) real-time communication.
Use cases:
- Chat applications (WhatsApp, Slack)
- Live sports scores
- Collaborative editing (Google Docs)
- Real-time dashboards
Alternative: Server-Sent Events (SSE) - one-way (server → client) only, simpler than WebSockets
| Feature | WebSockets | SSE | Polling |
|---|---|---|---|
| Direction | Bi-directional | Server → Client | Client → Server (repeated) |
| Protocol | Custom (ws://) | HTTP | HTTP |
| Overhead | Low (persistent) | Low (persistent) | High (reconnections) |
| Use Case | Chat, gaming | Live feeds, notifications | Simple updates |
6. DNS (Domain Name System)
Purpose: Translate domain names to IP addresses.
DNS Record Types:
| Type | Purpose | Example |
|---|---|---|
| A | Domain → IPv4 | example.com → 93.184.216.34 |
| AAAA | Domain → IPv6 | example.com → 2606:2800:220:1... |
| CNAME | Alias | www.example.com → example.com |
| MX | Mail server | example.com → mail.example.com |
| TXT | Text data | SPF, DKIM for email |
DNS Caching:
- Browser cache: ~1 minute
- OS cache: ~5 minutes
- ISP cache: ~TTL (Time To Live, often 24 hours)
TTL trade-off:
- Low TTL (5 min): Fast to update, higher DNS load
- High TTL (24 hr): Less DNS load, slow to propagate changes
Don't forget DNS when designing! It's a single point of failure. Use:
- Multiple DNS providers (Route53 + Cloudflare)
- Low TTL before migrations
- Health checks to automatically remove failed servers
7. CDN (Content Delivery Network)
Purpose: Cache static content close to users globally.
What CDNs cache:
- ✅ Static files (images, CSS, JS, videos)
- ✅ API responses (with cache headers)
- ❌ Dynamic, user-specific data (without special config)
Benefits:
- Reduced latency: Serve from nearest edge server (50ms → 5ms)
- Lower origin load: Most requests hit cache (90%+ cache hit rate)
- DDoS protection: Absorb malicious traffic at edge
Popular CDNs:
- Cloudflare (free tier, great DDoS protection)
- AWS CloudFront (integrates with AWS)
- Fastly (real-time purging)
- Akamai (oldest, largest network)
8. Load Balancers
Purpose: Distribute incoming traffic across multiple servers.
Algorithms:
| Algorithm | How It Works | Use Case |
|---|---|---|
| Round Robin | Rotate through servers in order | Uniform server capacity |
| Least Connections | Send to server with fewest active connections | Long-lived connections |
| Weighted | More traffic to powerful servers | Heterogeneous capacity |
| IP Hash | Same client → same server (consistent) | Session persistence |
Health Checks:
- Load balancer periodically pings servers (e.g., every 10s)
- If server fails health check (timeout or error), remove from pool
- When server recovers, add back to pool
Layer 4 vs Layer 7 (covered in detail in Chapter 10):
- L4 (Transport): Faster, route based on IP/port
- L7 (Application): Slower, route based on HTTP headers, URL path
Common Interview Questions
Q1: "When would you use UDP instead of TCP?"
Answer:
- Real-time applications where low latency matters more than reliability
- Examples: Live video streaming, VoIP (Zoom, Skype), online gaming
- Reason: Packet loss in video just causes brief glitch; waiting for retransmission would cause stuttering
Q2: "How does HTTPS improve security?"
Answer:
- Encryption: Traffic encrypted with TLS, can't be read by intermediaries
- Authentication: Certificate proves server identity (prevents man-in-the-middle)
- Integrity: Detects if data was modified in transit
Trade-off: Adds ~100ms latency for initial handshake
Q3: "Explain how a CDN works and when to use it."
Answer:
- User requests
cdn.example.com/image.jpg - DNS routes to nearest CDN edge server
- If edge has cached copy → return immediately (cache hit)
- If not → edge fetches from origin, caches, and returns (cache miss)
- Subsequent requests hit cache
Use when:
- Serving static content globally
- High traffic (cost-effective)
- Need DDoS protection
Q4: "WebSockets vs HTTP polling for real-time updates?"
Answer:
| Aspect | WebSockets | Polling |
|---|---|---|
| Latency | Instant (push) | Up to poll interval |
| Overhead | Low (persistent connection) | High (repeated handshakes) |
| Complexity | More (connection management) | Less (simple HTTP) |
| Scalability | Need to manage open connections | Stateless, easier to scale |
Use WebSockets for: Chat, collaborative editing, gaming
Use Polling for: Less frequent updates, simpler systems
Trade-offs
| Decision | Option A | Option B | Trade-off |
|---|---|---|---|
| Protocol | TCP (reliable) | UDP (fast) | Reliability vs latency |
| HTTP Version | HTTP/1.1 (simple) | HTTP/2 (multiplexing) | Compatibility vs performance |
| Real-time | WebSockets (push) | Polling (pull) | Complexity vs simplicity |
| DNS TTL | Low (5 min) | High (24 hr) | Agility vs DNS load |
| CDN | Use CDN (fast) | Direct (simple) | Cost vs latency |
Real-World Examples
Netflix
- Protocol: HTTP/2 for API, adaptive bitrate streaming
- CDN: 90%+ traffic via Open Connect (own CDN)
- Result: Sub-second startup time globally
WhatsApp
- Protocol: Custom protocol over TCP (not HTTP)
- Real-time: Persistent connections (WebSocket-like)
- Result: Instant message delivery, billions of connections
Cloudflare
- Service: Global CDN + DDoS protection
- Scale: 25% of all web traffic passes through Cloudflare
- Result: Protects millions of sites from attacks
Quick Reference Card
Protocols:
- TCP: Reliable, ordered, slow(er) - use for APIs, databases
- UDP: Fast, unreliable - use for streaming, gaming
- HTTP: Request-response, stateless - use for web APIs
- WebSockets: Persistent, bi-directional - use for real-time
HTTP Status Codes:
- 2xx: Success
- 4xx: Client error (your fault)
- 5xx: Server error (their fault)
DNS TTL:
- Low (5 min): Fast updates, high load
- High (24 hr): Slow updates, low load
CDN Benefits:
- Lower latency (serve from edge)
- Reduce origin load (cache hit rate)
- DDoS protection
Further Reading
- MDN Web Docs - HTTP
- WebSockets vs Server-Sent Events
- Cloudflare Learning Center
- High Performance Browser Networking (Free book)
Next: Storage Fundamentals - SQL vs NoSQL, ACID vs BASE.