Highlights

Contents

BACKEND ENGINEERING / UNIT XI / FIELD GUIDEProxies, loadbalancers, networksFrom the edge of the data centre to the socketproxya tunnel through two NATsProxiesNginx, headers, bufferingLoad balancingL4, L7, algorithmsDiscovery and meshesnames, gateways, sidecarsProduction networkingsockets, ports, NAT, cloud
Before we start

How to read this chapter

Between a user’s phone and Wren’s code sit half a dozen machines that each open a connection, read a little of the request, decide where it should go and open another connection. Most of them are invisible until one runs out of something, and then the whole service is down while the CPU graph says everything is fine.

The unit follows a request inwards. Parts I and II cover reverse proxies in general and Nginx in particular, from forwarded headers to worker processes and zero-copy file serving. Parts III and IV cover load balancing, the difference between balancing connections and balancing requests, the algorithms, and what layer 4 balancers do to packets. Part V finds services by name instead of address, and Part VI adds API gateways and service meshes. Part VII builds private networks with WireGuard and Tailscale, including how two machines behind separate NATs connect directly. Part VIII goes down to sockets, queues, ports and NAT tables, the limits that fail before CPU does, and Part IX maps all of it onto cloud building blocks. Part X follows one request across every hop.

Each section starts from a Wren situation, explains the mechanism, works the numbers and then shows a figure. Picture this boxes give analogies, notes add detail, Watch out names real mistakes, and Interview lens gives an answer to say aloud. In one breath closes each part.

By the end you should be able to explain why Nginx needs no thread per connection, choose between L4 and L7 balancing, explain why new pods sometimes receive no traffic, describe UDP hole punching, and debug a service that fails at 20% CPU.

The running system

Meet Wren

Wren is the illustrative food-ordering service from earlier units. These are the numbers this unit uses. All are assumptions chosen for readable arithmetic, not measurements.

Setting Value What it controls
Peak traffic 2,000 requests per second Connection rates
Ingress Nginx, 8 workers, worker_connections 10,000 Proxy capacity
App instances 4 normally, 20 at peak, as Kubernetes pods Load balancing
Ephemeral port range 32768 to 60999, 28,232 ports Port exhaustion
TIME_WAIT 60 s on Linux Socket reuse
NAT idle timeout 350 s Dropped idle connections
TCP keepalive default 7,200 s idle before the first probe Keepalive tuning
Accept queue somaxconn 4,096 Listen backlog
Mesh about 400 pods with sidecars at 50 MB each (illustrative) Mesh overhead
Public address api.wren.example, 203.0.113.10 Examples

The main request is GET /orders/123 from user 42’s phone at 198.51.100.7. Part X follows it through DNS, the CDN, the cloud load balancer, Nginx, a sidecar and the orders pod, and on to the payments service by name.

The map

What is inside

Follow a request from the edge of the data centre to a socket, through every box that terminates, inspects and forwards it.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Reverse proxies2Nginx internals3Load balancing4TCP load balancers5Service discovery6Gateways and servicemeshes7Private networks andTailscale8Sockets and connectionsin production9Cloud building blocks10A request across thenetwork
  1. 01Reverse proxiesSections 1 to 3. We put a server in front of Wren's servers that speaks to the internet on their behalf.
  2. 02Nginx internalsSections 4 to 6. We open Nginx and see how a handful of processes serve tens of thousands of connections.
  3. 03Load balancingSections 7 to 10. We spread requests across many instances so that none is overwhelmed and a failed one is skipped.
  4. 04TCP load balancersSections 11 to 13. We look at what layer 4 balancers do to packets and why that matters to the application.
  5. 05Service discoverySections 14 to 16. We let services find each other by name instead of by address.
  6. 06Gateways and service meshesSections 17 to 20. We look at two kinds of proxy layer that organisations add as they grow, one at the front door and one between every pair of services.
  7. 07Private networks and TailscaleSections 21 to 23. We connect machines that sit behind different firewalls and NATs with encrypted, direct links.
  8. 08Sockets and connections in productionSections 24 to 27. We go down to the kernel objects every backend depends on and that dashboards rarely show.
  9. 09Cloud building blocksSections 28 to 30. We map everything so far onto the vocabulary every cloud uses.
  10. 10A request across the networkSections 31 to 32. We follow GET /orders/123 from user 42's phone through every network hop, then replay three incidents that each started in one of this unit's parts.
  11. +Interview prepInterview question bank, graded exercises and worked solutions.