DEVOPS, SRE AND CLOUD · PART 2 · NETWORKING FOR DEVOPS · TRAFFIC · 45 OF 187

HAProxy, Envoy, API Gateways and Consul Explained

The proxies beyond nginx and when to choose each: HAProxy with active health checks, tested on two ShopRung instances (after we killed one, HAProxy logged "Server shop/b is DOWN, reason: Layer4 connection problem" and all requests still returned 200), Envoy and its dynamic xDS config behind service meshes, API gateways for auth, keys and quotas, and service discovery with Consul and DNS.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • HAProxy active checks: b DOWN, all 200
  • Envoy + xDS
  • gateways: keys, quotas
  • discovery: Consul, k8s DNS

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.

haproxy.cfg (ours)
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.

Our run
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.

Typical choice when you need
  • 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.

Envoy in one screen
  • 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.

Gateway jobs
  • 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.

How services find each other
  • 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.

Rough guide
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1When would you choose HAProxy over nginx?

For pure TCP/HTTP load balancing that needs rich active health checks, queues, slow start, detailed stats and a runtime API to drain servers.

Q2What is Envoy's xDS?

A set of discovery APIs (listeners, routes, clusters, endpoints) that let a control plane push configuration to Envoy dynamically without restarts.

Q3What is the difference between an API gateway and a load balancer?

A load balancer distributes traffic to healthy backends; an API gateway adds authentication, API keys, quotas, routing/versioning and analytics for API consumers.

Q4What is service discovery?

A mechanism for services to find healthy instances by name: a registry (Consul, Kubernetes Services/CoreDNS, Cloud Map) with health checks.

Q5What is outlier detection in Envoy?

Automatically ejecting backend hosts that return errors or time out too often from the load balancing pool for a period.

Q6Why are retries at every layer dangerous?

They multiply: three layers retrying three times can turn one request into dozens during an outage, overloading the failing service further.