LearnastraTHE DESIGN COURSE

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.

Intermediate1h of deliberate study10 questions

Builds on Load balancing, Functional and non-functional requirements.

By the end, you can
  • 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.

  1. The public entry point receives the TLS-protected request. A balancer selects a healthy gateway instance if a separate gateway fleet is needed.
  2. 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.
  3. The gateway routes /orders to an order server. Choosing a route does not establish permission to change an order.
  4. 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.
  5. 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.

  1. 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.
  2. 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.
  3. 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.
  4. 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

Load balancer versus API gateway — decision flowThe 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. Numbered arrows correspond to the connection key below. All connections are directional.ClientPublic API requestPublic loadbalancerChooses one gatewayinstanceAPI gatewaysVerify identity andshared request limitOrder serviceChecks this caller’sorder permissionSearch serviceHandles the requestedsearch1234Load balancer versus API gateway — decision flowThe 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. Numbered arrows correspond to the connection key below. All connections are directional.ClientPublic API requestPublic loadbalancerChooses one gatewayinstanceAPI gatewaysVerify identity andshared request limitOrder serviceChecks this caller’sorder permissionSearch serviceHandles the requestedsearch1234Load balancer versus API gateway — decision flowThe 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. Numbered arrows correspond to the connection key below. All connections are directional.ClientPublic API requestPublic loadbalancerChooses one gatewayinstanceAPI gatewaysVerify identity andshared request limitOrder serviceChecks this caller’sorder permissionSearch serviceHandles the requestedsearch1234

Follow the numbered arrows

  1. Client → Public load balanceropen the protected public connection
  2. Public load balancer → API gatewayschoose a gateway instance
  3. API gateways → Order servicesend the order request and caller identity
  4. API gateways → Search servicesend the search request
The balancer chooses a gateway; the gateway checks identity and allowance. The order service checks whether this caller may change this order.
Trace the diagram in words
  1. The public balancer chooses a gateway instance for the request.
  2. The gateway verifies identity and checks the shared customer allowance before routing.
  3. 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.

Your self-check
Compare the worked solution
  1. 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.
  2. 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.
  3. The service receiving an operation still checks the caller's permission for its records.
  4. Choose one retrying component and a maximum attempt count within the caller's deadline.
  5. 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?
  1. A balancer selects eligible servers.
  2. A gateway commonly checks credentials, limits and API routes.
  3. Either may support HTTP routing or TLS; verify features before adding another component.
CARD 02Who authorizes a specific order change?
  1. The gateway may verify the login.
  2. The order service checks current ownership and cancellation rules.
  3. A valid login does not permit changing every order.
CARD 03Why coordinate retries and request allowances?
  1. Three attempts at two layers can create nine backend attempts.
  2. Separate full customer allowances can multiply the total.
  3. Choose one retry owner and coordinate the shared allowance.
Practise this topic in the revision deck ↗

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.

1 / 10Green has a fleet-wide 100/s allowance but three gateways each enforce 100/s locally. What may occur?
2 / 10Two layers allow three attempts each. Maximum backend attempts for one action?
3 / 10The gateway verifies a login. Who checks whether Green may cancel order 42?
4 / 10A capable gateway already balances upstreams and applies every required edge rule. Why add a separate balancer?
5 / 10A process health check succeeds while the order database is unavailable. What is the limitation?
6 / 10Which capability can an application-layer load balancer provide?
7 / 10What should protect a retryable order create after an uncertain timeout?
8 / 10A shared gateway-rule update rejects several APIs. Which risk is illustrated?
9 / 10Which metric distinguishes a quota denial from backend failure?
10 / 10Which approach can enforce an exact global quota?
0 of 10 answered

CLOSE THE LOOP

Explain it without the page.

Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.

Go to the source