Notes
HAProxy config
HAProxy is a fast, focused load balancer for TCP and HTTP. Its config has frontends, which accept traffic, and backends, which list servers. Ours balances by least connections and runs an active health check: GET /api /health every second. Two failures mark a server down, two successes bring it back. haproxy minus c validated the file before we started it, like nginx minus t.
frontend web
bind 127.0.0.1:8200
default_backend shop
backend shop
balance leastconn
option httpchk GET
/api/health
default-server inter 1s
fall 2 rise 2
server a :8080 check
server b :8082 check Kill one
Then we killed instance b. HAProxy's health checker noticed on its own, without waiting for user traffic, and logged: server shop slash b is down, reason layer four connection problem, connection refused, one active server left. Six requests afterwards all returned two hundred from instance a. The built in stats page showed a up and b down. That is the difference from nginx's passive checks: failures are found by probes, not by users.
before: 6 requests → 6 × 200 kill ShopRung b log: Server shop/b is DOWN, reason: Layer4 connection problem, "Connection refused". 1 active and 0 backup servers left. after: 6 requests → 6 × 200 stats: a UP, b DOWN
Why HAProxy
HAProxy shines at pure load balancing: layer four and seven in one tool, rich health checks, slow start for newly added servers, queuing and retries, and very detailed statistics and logs. Its runtime API can drain or disable a server live during a deploy. It is a common choice in front of database replicas and as a Kubernetes ingress controller.
- L4 and L7 in one tool
- active health checks, slow start, queues, retries
- detailed stats and logs
- very high connection counts
- runtime API: drain or disable a server live
- used as Kubernetes ingress and in front of databases
Envoy
Envoy was built for microservices. Its model is listeners, routes, clusters and endpoints, and its key feature is xDS: instead of reading a file and reloading, Envoy receives configuration from a control plane over an API, live. It has retries, timeouts, circuit breaking and outlier detection, which ejects misbehaving instances, plus metrics and distributed tracing out of the box. Istio's sidecars and gateways and many API gateways are Envoy underneath. Part ten covers meshes.
- C++ proxy for L4/L7, HTTP/2, gRPC
- listeners → routes → clusters → endpoints
- xDS: a control plane pushes config live, no reload
- retries, timeouts, circuit breaking, outlier detection
- metrics and tracing built in Istio, Envoy Gateway, many API gateways use it
API gateways
An API gateway is a reverse proxy that understands API consumers. It validates API keys or tokens, applies quotas and rate limits per customer or plan, routes and versions APIs, transforms requests, and often provides a developer portal and analytics. Examples are AWS API Gateway, Kong, Apigee and Envoy Gateway. The rule of thumb: a load balancer spreads traffic; a gateway decides who may call what, and how often.
- authentication: API keys, JWT/OAuth validation
- quotas and rate limits per consumer or plan
- routing, versioning
- request/response transforms
- developer portal, analytics
- AWS API Gateway, Kong, Apigee, Envoy Gateway
Service discovery
In dynamic environments, instance addresses change constantly, so services need discovery. Instances register in a registry along with health checks, and clients or proxies ask it for the healthy ones. Consul does this with a registry, DNS interface, health checks and a key value store. Kubernetes does it for you with Services and CoreDNS, from episode nine. Clouds offer Cloud Map and load balancer DNS names. The common rule: never hardcode instance IPs.
- instances register (or are registered) with health
- clients look up healthy ones
- Consul: registry + DNS + health checks + KV
- Kubernetes: Services and CoreDNS do it for you
- cloud: Cloud Map, LB DNS
- never hardcode instance IPs
Which one?
Which one? nginx when you also serve static files or want a simple, familiar ingress. HAProxy for pure, high performance load balancing with rich health checks. Envoy for dynamic configuration, gRPC and service meshes. An API gateway when external consumers need keys, quotas and a portal. And in the cloud, start with the managed load balancers before running your own.
- web server + proxy, static, simple ingress → nginx
- pure LB, TCP + HTTP, rich checks → HAProxy
- dynamic config, gRPC, mesh → Envoy
- external API consumers, keys, quotas → gateway
- managed: ALB, NLB first
Common mistakes
Mistakes. Stacking proxy after proxy with no clear job for each. Retries configured at every layer, which multiply into retry storms during an outage. Timeouts that do not nest, so an outer proxy gives up before the inner one. Hardcoded backend addresses. And a gateway deployed as a single instance, a single point of failure for every API.
- running four proxies in a row with no reason
- retries at every layer (retry storms)
- timeouts that do not nest (outer < inner)
- hardcoded backend IPs
- gateway as a single point of failure
Say it like this
In the interview: a load balancer spreads traffic across healthy backends, like HAProxy or an ALB. An API gateway sits at the edge and adds consumer concerns: authentication, API keys, quotas and versioning. A service mesh puts a proxy, usually Envoy, beside every service for mutual TLS, retries and telemetry inside the cluster, configured by a control plane that uses service discovery.
- "A load balancer spreads traffic
- across healthy backends. An API
- gateway adds consumer concerns: auth, keys, quotas, versions. A mesh puts a proxy like Envoy
- beside each service for mTLS, retries and telemetry, fed by service discovery."