CMD Guide
HomeSystem DesignSystem Design Trade-offs

Load Balancer vs API Gateway

Load balancing is a function — spread traffic across a pool — and it comes in two builds: an L4 balancer routes on the connection's IP/port 5-tuple without reading the request, while an L7 balancer (ALB, NGINX) is a connection-terminating reverse proxy that can route on the request itself. An API gateway is an L7 application that goes further still: it terminates the connection, parses the HTTP method, path, headers, and token, and runs per-route policy before forwarding. This page contrasts the L4 archetype with the API gateway because that pair marks the two ends of the inspection-depth axis — the L7 balancer sits between them, and the ALB pitfall below shows exactly where it falls short of a gateway. Everything else (auth, rate limiting, aggregation) follows from that one axis: you cannot make a decision on data you never read.

The mechanism: depth of inspection

Picture a request as a sealed envelope inside a shipping box. The address on the box is the TCP/IP 5-tuple: source IP+port, destination IP+port, protocol. The letter inside the envelope is the HTTP request: GET /api/v2/orders/8842, an Authorization header, a body.

They are not rivals. In production the standard shape is an L4 LB in front of a horizontally-scaled gateway fleet: the LB gives the fleet a single VIP and spreads connections across gateway instances for availability; each gateway then does the L7 work. The LB scales the thing that does the thinking.

diagram
diagram

Trace one request through the whole stack

A phone app calls GET https://api.shop.com/api/v2/orders/8842 with header Authorization: Bearer eyJhbGciOiJSUzI1Ni.... Here is exactly what each hop sees and does — note where the request stops being opaque.

HopWhat it can seeWhat it does~cost
DNShostname api.shop.comResolves to the LB's VIP 203.0.113.100 (cached)
L4 LB (NLB)5-tuple only: src 198.51.100.7:51322 → dst 203.0.113.10:443, TCP. TLS bytes are opaque.Hashes the 5-tuple, picks gateway gw-2 at 10.0.1.24, forwards the flow's packets. Does not decrypt.0.2 ms
Gateway: TLSNow the plaintext HTTP requestTerminates TLS using the api.shop.com cert. From here the request is readable.1 ms
Gateway: authnmethod GET, path /api/v2/orders/8842, the Bearer tokenVerifies the RS256 signature against cached JWKS, checks exp, extracts sub=user_5567, scope=orders:read. Missing/expired → 401, request never reaches a service.3 ms
Gateway: rate limitthe caller identity user_5567Token bucket for user_5567 (100 rps): 42 used → allow. Over limit → 429.0.1 ms
Gateway: routethe path prefixRule /api/v2/orders/**orders-service. Strips prefix → GET /orders/8842; injects X-User-Id: 5567 so the service need not re-parse the token.0.1 ms
Internal LB5-tuple to the service poolRound-robins across 3 orders-service pods → 10.0.3.9:8080. Private network, no auth (already done at the edge).0.2 ms
orders-servicethe rewritten requestReads row 8842, returns 200 + JSON.8 ms

The response flows back through the gateway (which adds CORS headers and gzip) over the TLS connection it already terminated. The load balancers touched only the address; every decision that needed the request's contents happened at the gateway.

Where should TLS termination and auth live?

This is the question the two components actually settle. Auth must live at the L7 gateway — it is the shallowest hop that can read the token. Pushing it onto the L4 LB is physically impossible (no visibility into the payload), and pushing it into every microservice duplicates security-critical logic and guarantees drift.

TLS termination is a genuine choice with two common answers:

Pitfalls

When to use which — and the trade-offs

Reach for a plain load balancer (no gateway) when: traffic goes to one homogeneous service or a monolith, there is no per-consumer policy, and you just need to spread load and fail over dead instances. You gain the lowest possible latency (sub-millisecond at L4) and dead-simple operations; you give up any ability to enforce an API contract at the edge.

Reach for an API gateway when: many backend services must present one public surface, and you need centralized authn/authz, per-key rate limiting, request/response transformation, versioning, or response aggregation. You gain a single enforcement point and a stable client contract; you pay a few milliseconds per request, add a component that must itself be scaled and made highly available, and risk over-centralizing.

Versus the nearest alternative, an L7 reverse proxy (NGINX / AWS ALB): the proxy gives you TLS termination and path/host routing cheaply and with tiny latency. It does not give you first-class API management — no key issuance, no quotas, no token introspection, no composition. Choosing the proxy trades away policy features for simplicity and speed; choosing the gateway trades latency and a heavier operational surface for governance.

Crisp rule: choose an L4 load balancer when raw throughput, connection scale, and simple fan-out dominate; choose an API gateway when policy and a unified contract dominate; choose an L7 proxy when you only need routing plus TLS, not API management. And remember they compose — an L4 LB in front of a gateway fleet, with internal LBs behind it, is the default production topology, not an either/or.

Takeaways


Re-authored and deepened for this guide, drawing on the NGINX documentation (L4 vs L7 proxying), the AWS Elastic Load Balancing docs (Network Load Balancer vs Application Load Balancer), the Kong and AWS API Gateway feature references, Envoy's listener/filter model, and Sam Newman's "Building Microservices" (2nd ed.) on edge gateways and where cross-cutting concerns belong.

🤖 Don't fully get this? Learn it with Claude

Stuck on Load Balancer vs API Gateway? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Load Balancer vs API Gateway** (System Design) and want to truly understand it. Explain Load Balancer vs API Gateway from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Load Balancer vs API Gateway** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Load Balancer vs API Gateway** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Load Balancer vs API Gateway** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes