Module 2 · Backend architectureLesson 12 of 886 minBeginner

API gateways

An API gateway is the single front door for all your services. It handles the shared chores so each service doesn't have to.

Try it

Route a normal call, then try "No login" to see the gateway reject it, and "Home screen" to see one request fan out to three services.

With microservices, a mobile app might need data from ten services. Should the app know all ten addresses? Should each service check logins on its own? An API gateway solves both.

What a gateway does

  • Routing: /users/* goes to the user service, /orders/* to the order service. Clients only know one address.
  • Authentication: checks the login token once, then passes the user's identity along.
  • Rate limiting: stops one client from flooding the system.
  • TLS termination: handles HTTPS so services can talk plainly inside a private network.
  • Logging and metrics: one place to see every request.
  • Aggregation: turns one client request into several internal calls and combines the answers. This matters on phones, where each round trip is slow.

Backend for frontend

Different clients need different shapes of data. A watch app wants a tiny summary; a web dashboard wants everything. Some teams run a separate gateway per client type, called a backend for frontend (BFF), each tuned for its screens.

Things to watch

  • It's on every request, so it must be fast and highly available. Run several copies behind a load balancer.
  • Don't let it grow a brain. If business rules creep into the gateway, it becomes a new monolith that every team has to change.
  • Popular choices include Kong, AWS API Gateway, Envoy and NGINX.

Quick check

A request without a valid login token arrives. Where is the best place to reject it?