Make the right trade-off / CHAPTER 31
API gateway versus direct service exposure
Decide whether clients coordinate service calls directly or a gateway assembles them, including partial failures and deadlines.
Builds on Functional and non-functional requirements, Load balancing.
- Compare central API policies with distributed ownership.
- Explain public coupling and client request aggregation.
- Design secure routing and gradual change.
01Two public contracts
A mobile home screen needs a profile, recommendations and an unread count. There are two ways to expose those services:
- Direct endpoints: the mobile client calls the profile, recommendation and count APIs separately. Each service team maintains its own public request fields, responses, errors, access checks and compatibility.
- Gateway: the client calls one entry point, such as
/home. The gateway calls those internal services and returns a documented combined result.
The behavior clients rely on is the API contract. A gateway can keep that contract stable while internal addresses or service boundaries change.
Direct exposure still needs encryption, load balancing, authentication and resource permissions. A gateway does not remove service-side permission checks either. The decision is where public coordination and shared policy are maintained, not whether security exists.
02Centralization has both benefits and costs
Define the combined home response before implementing it:
- List required and optional data. In this example the profile is required; recommendations and unread count can have explicitly defined partial behavior.
- The gateway verifies the caller. It sends backend calls in parallel where their work is independent.
- Give the whole operation one deadline. Each backend receives only its remaining share of time, including time needed to assemble and return the result.
- Apply the agreed fallback. Omit unavailable recommendations. Use a cached list only if the product specifies its maximum age and the gateway verifies that it belongs to this viewer. Mark an unavailable count as unknown; a failed lookup is not evidence of zero.
- Return a response whose fields tell the truth. A partial display page must not claim that an unrelated purchase succeeded.
| Concern | Direct service calls | Gateway call |
|---|---|---|
| Client coordination | The client understands several API responses and partial failures. | The gateway assembles one response but must define its failure rules. |
| Network waiting | Independent calls can run in parallel. | One client call can lead to parallel internal calls. |
| Compatibility | Each public service maintains old-client behavior. | The gateway can hide internal reorganization behind a stable public API. |
| Shared policies | Teams repeat or coordinate their implementation. | A common implementation reduces repetition but a bad rollout can affect many routes. |
| Operating work | Each exposed service needs public controls. | The extra gateway fleet needs capacity, monitoring and recovery. |
Tools securing service-to-service traffic do not by themselves specify what fields and errors an old mobile client expects.
03Worked example: a mobile home screen
Assume a mobile network round trip adds 80 ms before service work.
- Three sequential direct calls add about 240 ms of network waiting. That is a possible implementation, not the fairest automatic baseline.
- Independent direct calls can overlap. Compare their actual longest required path with the gateway path before claiming that aggregation saves 160 ms.
- Set a 300 ms overall deadline for
/home. The gateway requests profile, recommendations and count in parallel. - Handle required profile failure. Return the documented failure or an explicitly designed guest state; do not fabricate a signed-in profile.
- Handle optional recommendation delay. Stop waiting when its allotted time expires. Omit the recommendation field before the overall deadline. A cached list is an alternative only when its age and viewer match the product’s stated reuse rules.
- Handle count failure honestly. Return unavailable, not a verified zero.
- Preserve the original deadline. A backend started after 100 ms has at most 200 ms left, including response assembly. It cannot receive a fresh 300 ms timeout while the client is already waiting.
The gateway reduces client coordination and can hide service changes. It also becomes responsible for these partial-failure decisions. Keep state-changing purchases with the purchase service: being able to return half a page does not make half a business transaction acceptable.
API gateway versus direct service exposure — decision flow
Follow the numbered arrows
- Mobile client → Home API gatewayrequest GET /home
- Home API gateway → Profile servicerequest the required profile
- Home API gateway → Recommendation servicerequest within the remaining time
- Home API gateway → Unread-count servicerequest the optional unread count
Trace the diagram in words
- The client calls the documented home-screen operation.
- The gateway requests independent backend data in parallel within one remaining deadline.
- Return the required profile and label or omit optional unavailable fields under the declared policy.
04Choose by client and team needs
Choose using clients and operations:
- Direct APIs can fit a few trusted clients, stable service boundaries and teams able to maintain equivalent public controls.
- A gateway can fit many public clients, common policy and slow mobile release cycles. Its stable interface can reduce client updates when internal services move.
- Keep business decisions with their records. The order service decides whether an order may be cancelled. Repeating that rule in the gateway creates a second copy that can disagree.
- Migrate one endpoint at a time. Compare fields, errors, response time and permissions. Preserve a rollback route and support existing clients during a contract change.
- Measure the whole path. Fewer external calls do not remove internal waiting, shared dependencies or gateway failures.
Explain one required result, one optional timeout and the returned fields. That trace shows whether the chosen public boundary actually works.
DRAW. CALCULATE. EXPLAIN.
Assemble a useful page
20 minutes · guided practice
A mobile product page needs required product data, optional recommendations and an approximate badge count. The complete response must arrive within 400 ms. After 150 ms, recommendations remain slow. Specify direct or gateway calls, remaining time, and partial behavior.
Compare the worked solution
- Compare a gateway with parallel direct calls, not only an unnecessarily sequential mobile client.
- At 150 ms into a 400 ms deadline, at most 250 ms remains, including assembly and return time.
- Return the required product data when available. Omit slow recommendations. If proposing a cached fallback, state its maximum age and check that it is for this viewer. Neither path restarts the deadline.
- Mark a failed badge lookup unavailable rather than reporting a confirmed zero.
- The purchase service still checks price and eligibility. Optional page assembly cannot decide or weaken that operation.
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 01What does a gateway change for clients?
- The client calls one documented API.
- The gateway calls internal services and assembles the response.
- Those calls and failures still exist, and services still check permissions.
CARD 02How should an assembled response fail?
- Name required and optional fields.
- Use one overall deadline.
- Omit failed optional data, or label an older value with its known age.
An unavailable unread-count lookup is not evidence of zero unread items.
CARD 03When are direct service endpoints reasonable?
A few trusted clients and stable APIs may need less common coordination. Each exposed service still needs:
- Encryption and identity checks.
- Resource permission and request limits.
- Compatible responses and recovery behavior.
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.