Make the right trade-off / CHAPTER 30
Load balancer versus API gateway
Separate distributing traffic from applying API policies while recognizing that products can do both.
Builds on Load balancing, Functional and non-functional requirements.
- Explain transport and application routing.
- Place authentication and rate limits deliberately.
- Avoid redundant hops and bypass paths.
01Responsibilities are more useful than product names
A shop exposes order and search APIs through several application servers. Someone must choose a server for each request, and someone must verify credentials, limit abusive traffic and route to the right API.
These responsibilities lead to two common roles:
- A load balancer distributes connections or requests among servers configured for that service and passing its readiness checks. A transport-layer balancer uses network addresses and ports. An application-layer balancer can also inspect HTTP hostnames, paths and headers.
- An API gateway commonly checks credentials, applies API limits, routes versions, transforms requests or assembles responses from services.
The roles overlap. A gateway may balance requests; a load balancer may understand HTTP and terminate TLS. Draw the work you need first, then decide whether one component or two should do it. Neither product name guarantees every feature or the correctness of an order.
02Place policy at the right boundary
Follow an order request from Green, a customer organization whose users share one request allowance. Such an organization is called a tenant.
- The public entry point receives the TLS-protected request. A balancer selects a healthy gateway instance if a separate gateway fleet is needed.
- The gateway verifies the caller's credential. This establishes identity. It also checks request size and the allowance shared by Green's users across all gateways.
- The gateway routes
/ordersto an order server. Choosing a route does not establish permission to change an order. - The order service checks the actual record. Does the verified caller belong to Green, own the order and meet its cancellation rules? This operation-specific decision is authorization.
- Each service uses the time remaining before the caller's deadline. Choose one component to retry temporary connection failures within a maximum attempt count. Before retrying a write, it must reuse the request key whose saved result prevents a repeated order. Independent retry loops can repeat the same backend work many times.
| Responsibility | Possible placement | What that does not prove |
|---|---|---|
| Select a healthy backend | A balancer or a capable gateway can do this. | A healthy process may still lack its required database. |
| Route by HTTP host or path | An application balancer or gateway can inspect these fields. | Path routing does not grant order permission. |
| Terminate TLS | Either component may support it. | A separate backend connection still needs protection where required. |
| Apply account limits or API versions | A gateway commonly manages these policies. | Verify the actual product and configuration. |
| Accept an order transition | The order service checks current business records. | A valid login alone is insufficient. |
03Worked example: order and search APIs
Assume 2,000 incoming requests/s, three gateway instances and a shared Green allowance of 100 requests/s.
- Give each gateway an independent 100/s counter. Green could then send 100/s through each: about 300/s in total. Matching tenant names do not make separate counters shared.
- For an exact 100/s total, coordinate spending. A shared store checks and deducts remaining allowance in one indivisible operation, or each gateway spends a reserved portion whose combined total stays within the limit. If local limits intentionally approximate the total, state the accepted overshoot.
- Check nested retries. Three attempts at the outer balancer, each causing three gateway attempts, can produce nine backend attempts. That is nine attempts, not proof of nine committed orders.
- Protect uncertain writes. Save the caller's stable request key and order result with the order change. On a retry, look up that key; return its saved result instead of creating another order. Reject reuse with different request content.
Now check health and placement:
- A listening-port check shows the process responds, often called liveness. Readiness asks whether it can do the required work, such as reaching the order database. Match the check to that route.
- An optional recommendation failure should not remove all otherwise usable order servers from routing.
- If the gateway already provides required balancing, another balancer needs a specific additional benefit. Every extra component adds a call, capacity limits and configuration to maintain.
Load balancer versus API gateway — decision flow
Follow the numbered arrows
- Client → Public load balanceropen the protected public connection
- Public load balancer → API gatewayschoose a gateway instance
- API gateways → Order servicesend the order request and caller identity
- API gateways → Search servicesend the search request
Trace the diagram in words
- The public balancer chooses a gateway instance for the request.
- The gateway verifies identity and checks the shared customer allowance before routing.
- The order service reads the order and checks whether that caller may change its state.
04Evaluate the whole request path
Allocate responsibilities explicitly:
- Distribute traffic where equivalent healthy backends need selection.
- Centralize common API rules where this reduces repeated credential, version or quota work across services.
- Keep order-specific rules in the order service. Two implementations of cancellation eligibility can drift apart.
- Block or equally protect bypass routes. A direct backend route must not silently skip a mandatory request limit or access check.
- Release common policy gradually. Several gateway instances can all reject requests after the same bad rule update; retain rollback.
- Measure different outcomes separately. A valid quota denial, invalid credential, saturated gateway and failed order database need different responses.
In the interview, trace one request and one retry. Name who selects a server, checks identity, spends allowance and changes the order record. A justified division of work is more useful than a mandatory pair of product names.
DRAW. CALCULATE. EXPLAIN.
Assign the order entry path
20 minutes · guided practice
Two public services need TLS, healthy routing and one 200/s allowance per tenant. Four gateway instances are available. Design responsibilities, explain the risk of independent counters, and choose retry ownership.
Compare the worked solution
- A capable gateway fleet can terminate TLS, apply API policy and balance requests. Add a separate balancer only for another needed connection or fleet responsibility.
- Four independent 200/s allowances could admit 800/s for one tenant. Use one shared indivisible counter update or allocate portions whose total stays within 200/s.
- The service receiving an operation still checks the caller's permission for its records.
- Choose one retrying component and a maximum attempt count within the caller's deadline.
- For writes, save a stable request key and result with the change, then return that saved result on repetition instead of applying it again.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01How do balancers and gateways overlap?
- A balancer selects eligible servers.
- A gateway commonly checks credentials, limits and API routes.
- Either may support HTTP routing or TLS; verify features before adding another component.
CARD 02Who authorizes a specific order change?
- The gateway may verify the login.
- The order service checks current ownership and cancellation rules.
- A valid login does not permit changing every order.
CARD 03Why coordinate retries and request allowances?
- Three attempts at two layers can create nine backend attempts.
- Separate full customer allowances can multiply the total.
- Choose one retry owner and coordinate the shared allowance.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.