Load Balancers, Proxies and API Gateways

Distribute traffic across servers with load balancing algorithms, understand L4 vs L7, health checks, reverse proxies and API gateways.

Intermediate · about 4 hours · difficulty 3/5

Learn first: Scalability, Latency and Back-of-the-Envelope Estimates

A load balancer (LB) spreads requests across servers so no single one is overwhelmed and failed servers are bypassed. It is the first box you draw in almost every design.

Algorithms

  • Round robin and weighted round robin: simple and fair when requests are similar.
  • Least connections or least response time: better when requests vary in cost.
  • IP hash / consistent hashing: the same client or key goes to the same server, useful for caches and sticky sessions.
  • Random with two choices (optional): surprisingly effective.

Layers

  • L4 (transport) balancers route by IP and port without reading the request. Very fast.
  • L7 (application) balancers read HTTP (path, headers, cookies) and can route /api and /images to different pools, terminate TLS and rewrite requests. Nginx, HAProxy and cloud ALBs do this.

Reliability features

  • Health checks remove unhealthy servers automatically.
  • The LB itself must not be a single point of failure: run it in active-passive or active-active pairs, or use a managed cloud LB.
  • Sticky sessions are a crutch; prefer stateless servers.

Proxies and gateways

  • A forward proxy acts for clients (corporate proxies, VPN-like use).
  • A reverse proxy sits in front of servers: TLS termination, compression, caching and hiding internal topology.
  • An API gateway is a reverse proxy with API concerns: authentication, rate limiting, request routing to microservices, and logging.
  • DNS load balancing and global server load balancing route users to the nearest region.

Good enough to move on: you can pick an LB algorithm for a given workload, explain L4 vs L7, and describe what happens when a backend server dies mid-traffic.

Key concepts

  • Round robin and least connections
  • Hash-based routing
  • L4 vs L7 load balancing
  • Health checks and failover
  • Forward vs reverse proxy
  • API gateways
  • Global and DNS load balancing

Mini project

Run three copies of a small web server with Docker, put Nginx in front as a reverse proxy with round robin and then least connections, add a health check, kill one server during a load test with a tool like hey or k6, and record what clients observe.

Check yourself

Why would you choose an L7 load balancer over an L4 one for a web application with separate API and static file servers?

Free resources