Transport, proxies, load balancers and edge services
Ports and sockets
Layer 4 adds ports so many applications share one IP. A connection is identified by the 5-tuple. Well-known ports: 22 SSH, 25/587 SMTP, 53 DNS, 80 HTTP, 443 HTTPS, 3306 MySQL, 5432 PostgreSQL, 6379 Redis, 9092 Kafka, 2379 etcd, 6443 Kubernetes API, 10250 kubelet. (TCP vs UDP is its own topic.)
Firewalls: stateful vs stateless
- Stateless filters evaluate each packet alone — you must explicitly allow return traffic (usually on ephemeral ports 1024–65535). AWS NACLs work this way.
- Stateful firewalls track connections: allow the outbound request and the reply is allowed automatically. AWS security groups, iptables with conntrack, most host firewalls.
- Next-generation / L7 firewalls inspect application content (AWS Network Firewall with Suricata rules, WAFs for HTTP).
Forward vs reverse proxies
- Forward proxy — acts for clients going out: corporate egress proxies, Squid; controls and logs outbound access. Configured in clients via
HTTP_PROXY/HTTPS_PROXY/NO_PROXY— a classic source of "works on my laptop, not on the server" (or the reverse) when proxy settings differ. - Reverse proxy — sits in front of servers for incoming requests: TLS termination, routing, caching, compression, rate limiting, auth. Nginx, HAProxy, Envoy, Traefik; ALBs and ingress controllers are reverse proxies too.
Reverse proxies terminate the client connection and open a new one to the backend, so the backend sees the proxy's IP. The client IP is passed in X-Forwarded-For (and X-Forwarded-Proto, X-Forwarded-Port) for HTTP, or with Proxy Protocol (v2 on NLBs) for raw TCP.
Layer 4 vs layer 7 load balancing
| Layer 4 | Layer 7 | |
|---|---|---|
| Sees | IPs, ports, TCP/UDP | Full HTTP: host, path, headers, cookies, methods |
| Routing | By connection (5-tuple hashing) | By request content (/api → service A, admin.example.com → B) |
| TLS | Passthrough or termination (NLB TLS listeners) | Terminates TLS, may re-encrypt |
| Features | Very high throughput, low latency, static IPs, client IP preserved | Redirects, header rewrites, auth (OIDC/Cognito), WAF, sticky cookies, gRPC/HTTP2 per-request balancing |
| AWS | NLB | ALB |
Load-balancing algorithms: round robin, least outstanding requests (ALB option), least connections, hashing (source IP, 5-tuple, consistent hashing for caches).
Health checks remove unhealthy targets. L4 checks prove only that a TCP handshake completes; HTTP checks prove the app responds correctly — prefer them.
CDN, anycast, VPN
- CDN (CloudFront, Akamai, Cloudflare) — edge servers caching content near users, terminating TLS close to them, absorbing DDoS. Cache-Control headers decide what's cached.
- Anycast — the same IP announced via BGP from many locations; routing delivers each user to the nearest. Used by DNS root servers, CDNs, AWS Global Accelerator, Route 53.
- VPN — an encrypted tunnel (IPsec, WireGuard, OpenVPN) between networks (site-to-site) or a client and a network. AWS Site-to-Site VPN; AWS Client VPN.
- BGP — the routing protocol between networks on the internet, also used by Direct Connect and VPN dynamic routing.
Latency vs bandwidth
Latency is the delay for one trip (ms); bandwidth is how much data per second the pipe can carry. A 1 Gbps link with 200 ms latency still makes chatty protocols (many round trips) slow. Most "slow app" problems over long distances are latency, solved by fewer round trips, caching, CDNs, connection reuse and moving compute closer to users.