Skip to content
Ameen Abozaid
MID-035

API gateways

  • 6 Lessons
  • 2.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Routing
  • authentication integration
  • gateway responsibilities

Learn first

2.5 h of practice (estimate)

Learning outcomes

  1. Describe gateway responsibilities: routing, TLS termination, authentication, rate limiting, request shaping.
  2. Configure Spring Cloud Gateway (or an equivalent) in front of Shop's services.
  3. Decide what belongs in the gateway vs in services, and avoid business logic at the edge.
  4. Explain the backend-for-frontend pattern.

Lessons

  1. 01Why a gateway
  2. 02Routing and filters
  3. 03Authentication at the edge
  4. 04What not to put in a gateway
  5. 05Backend for frontend
  6. 06Demo: one entry point for four services

Lab and exercises

Lab

MID-035-LAB1 · Shop gateway

Outcomes: 1–4 · Time: ~2.5 h Build a Spring Cloud Gateway (or an equivalent) as the single public entry point for catalog, orders, inventory, and payments. Validate JWTs at the edge, route by path, add a correlation id, and apply rate limits (MID-034). Remove published ports from the services so they are reachable only through the gateway on the Compose network, and keep token validation in services.

Acceptance: services are not reachable directly from outside; requests without a valid token get 401 at the gateway; a request with a forged identity header does not gain access.

Exercises (assessment: responsibility-placement exercise)

IDOutcomeTask
MID-035-EX011List five responsibilities of an API gateway with one sentence each.
MID-035-EX022Write the route configuration for catalog and orders with path predicates and prefix stripping.
MID-035-EX032Configure JWT validation so /api/products/** is public and everything else needs a token.
MID-035-EX043Responsibility placement: for 10 concerns (TLS, CORS, discount calculation, order ownership, rate limit, JWT signature check, response aggregation, request logging, retry of payment, feature flags per customer), decide gateway, service, or BFF.
MID-035-EX053A team wants to add stock-based pricing rules to the gateway. Write a short rejection with the better place.
MID-035-EX064Describe when Shop needs a BFF and sketch a mobile BFF response for the home screen.
Show solutions
  • EX01 Routing, TLS termination, authentication, rate limiting, request shaping (headers, rewrites, correlation id); also CORS and logging.
  • EX02 Two routes with Path=/api/products/** to http://catalog:8080 and Path=/api/orders/** to http://orders:8080, filter StripPrefix=1.
  • EX03 Resource-server config with permitAll() for /api/products/** and authenticated() otherwise; configure issuer or JWK set URI.
  • EX04 Gateway: TLS, CORS, rate limit, JWT signature check, request logging. Service: discount calculation, order ownership, payment retry (with idempotency). BFF: response aggregation. Feature flags per customer: service (or a flag service), not the gateway.
  • EX05 Pricing is business logic; it would couple the gateway to inventory data and force gateway releases for every rule. Put it in catalog or a pricing component.
  • EX06 A BFF is justified when mobile needs aggregated, smaller payloads (featured products, cart count, order status) in one call, different from web; it calls catalog and orders and returns one tailored JSON.

Quiz Q09 and Q10 also cover outcome 4.

Quiz

Outcomes: O1 responsibilities · O2 Spring Cloud Gateway · O3 gateway vs services · O4 BFF.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Which is a typical gateway responsibility?O1
  2. 2A gateway gives clients:O1
  3. 3In Spring Cloud Gateway a predicate:O2
  4. 4StripPrefix=1 does what?O2
  5. 5Gateway filters should:O2
  6. 6"Customer may only see their own orders" belongs in:O3
  7. 7A gateway full of business rules becomes:O3
  8. 8Services should:O3
  9. 9A BFF is:O4
  10. 10The BFF differs from the gateway because it:O4

Practical part

Six exercises MID-035-EX01 to EX06 including the responsibility-placement exercise, plus MID-035-LAB1. Pass: at least five exercises, correct placement of at least 8 of 10 concerns, and a lab where services are unreachable except through the gateway.

Common mistakes

Gateway as a new monolith; services trusting any request with no token.

Assessment

Responsibility-placement exercise.

Career relevance

API management (ARC-025).

Type to search levels, phases, modules and lessons.