Networking for Modern Web Apps
Networking for Modern Web Apps
CDN Architecture
CDN edge nodes (PoPs) in dozens of cities. User traffic routed to nearest PoP via Anycast (multiple IPs announce the same address; BGP routes to closest).
What CDNs cache: static assets (set Cache-Control: public, max-age=31536000, immutable for versioned files), HTML, API responses.
Cache invalidation: TTL-based (simple, allows stale), Purge API (explicit on deploy), Surrogate keys / cache tags (invalidate all pages tagged with a key, e.g., all product pages for a category).
CDN for security: WAF rules at the edge; volumetric DDoS absorbed at CDN before reaching origin.
Real-Time Communication
Polling: client asks "anything new?" every N seconds. Simple but wasteful.
Long polling: server holds connection until data arrives. Less wasteful.
WebSocket handshake: HTTP/1.1 Upgrade request → 101 Switching Protocols → bidirectional frame exchange. Either side sends frames anytime.
Frame opcodes: text (0x1), binary (0x2), ping/pong (0x9/0xA), close (0x8).
SSE (Content-Type: text/event-stream): server pushes events as text lines; EventSource in browser auto-reconnects. Simpler than WebSocket for one-directional streams. Used by OpenAI, Anthropic for token streaming.
HTTP/3 and QUIC
TCP head-of-line blocking: one lost packet stalls all subsequent packets in the connection. HTTP/2 multiplexes over TCP — but one TCP-level loss stalls all HTTP/2 streams.
QUIC (UDP-based): per-stream reliability — loss in stream 1 doesn't block stream 2.
Additional QUIC benefits:
- 0-RTT resumption: cached session parameters allow data in first packet
- Built-in TLS 1.3: encryption not optional
- Connection migration: connection ID in QUIC header, not 5-tuple — IP change (WiFi → cellular) doesn't disconnect
HTTP/3: ~30% of web traffic (2024). Cloudflare, Google, Vercel support it.
Edge Computing
Run computation at the CDN edge node closest to the user instead of a central data center.
Use cases: JWT verification at edge, geo-redirect without origin RTT, A/B test variant assignment, bot detection, request/response transformation.
Edge runtimes (Cloudflare Workers, Vercel Edge Functions): V8 isolates start in ~0.5ms vs container cold starts (~500ms). Limitation: no Node.js APIs, limited I/O, short execution time.
Edge + origin pattern: edge handles routing/caching/auth; origin handles stateful computation and database queries.
Load Balancers
L4 (TCP/IP): routes by IP/port; doesn't inspect HTTP payload. Fast; for high-throughput UDP/TCP.
L7 (HTTP): path routing (/api → API servers), header-based canary routing, session affinity.
Algorithms: round-robin, least connections, weighted, consistent hashing (same key → same server; cache locality; minimal disruption when servers added/removed).
Service Mesh
Sidecar proxy (Envoy) alongside each service container — all traffic flows through sidecars. Provides: mTLS between services (automatic cert issuance), traffic management (retries, timeouts, circuit breaking, canary splits), observability (traces, metrics without code changes).
Circuit breaker: if downstream error rate exceeds threshold, immediately return errors for subsequent requests rather than waiting for timeouts. Prevents cascading failures.