Opscraft
Networking fundamentals · Lesson 4 of 9 · 6 min read

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).
Enlarge diagramSTATEFUL · SECURITY GROUPSTATELESS · NETWORK ACLclientSecuritygroupserver :443clientNACLserver :443request to 443allowedreply to :51000allowedrequest to 443allowedreply to :51000droppedNACL rule needed for the reply: outbound, TCP 1024-65535 (ephemeral ports).
Figure 1. Stateful firewalls (security groups) remember the request and let the reply out automatically. Stateless ones (NACLs) judge every packet alone, so replies need their own rule.
Swipe sideways to see all of it, or tap to open it full screen.

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.

Enlarge diagramFORWARD PROXY · OUTBOUNDREVERSE PROXY · INBOUNDyour serversHTTPS_PROXY is setForward proxyallows, blocks and logsinternet sitesuserson the internetReverse proxy / ALBTLS, routing, cachingyour serverssee the proxy IP; clientIP in X-Forwarded-For
Figure 2. A forward proxy acts for clients going out; a reverse proxy sits in front of servers taking requests in.
Swipe sideways to see all of it, or tap to open it full screen.

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.

Enlarge diagramLAYER 4 · NLBLAYER 7 · ALBclientNLBsees IP:porttarget 1target 2target 3clientALBreads therequestapi service/api/*admin/adminwebeverything else
Figure 3. A layer 4 balancer spreads connections without reading them. A layer 7 balancer reads each HTTP request and routes on host and path.
Swipe sideways to see all of it, or tap to open it full screen.

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.
Enlarge diagramedge in Mumbaiannounces 192.0.2.1edge in Frankfurtannounces 192.0.2.1edge in Virginiaannounces 192.0.2.1users near Mumbaiusers near Frankfurtusers near VirginiaSame address everywhere; the network picks the closest route.
Figure 4. Anycast: many locations announce the same IP address, and internet routing delivers each user to the nearest one. CDNs, DNS root servers and Route 53 use it.
Swipe sideways to see all of it, or tap to open it full screen.

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.

Enlarge diagramFixes for latency: fewer round trips (batching, keep-alive, HTTP/2), caching, a CDN, or moving servers closer tousers. More bandwidth does not help.10 round trips10 × 200 ms = 2 sbatched into 11 × 200 ms = 0.2 s
Figure 5. Bandwidth is how much fits through the pipe; latency is how long one trip takes. A chatty app over a long link is slow no matter how wide the pipe is.
Swipe sideways to see all of it, or tap to open it full screen.