API gateways
Three backend services, one front door. Seven steps show what an API gateway does with each request: route it, check it, count it, log it, combine answers, and what happens when it fails.
In 30 seconds
An API gateway is a single front door placed before a group of backend services. Clients talk to one address; the gateway passes each request to the service that handles it.
Because every request passes through it, shared chores live there once: checking who is calling, limiting how fast they call, and logging.
It can also combine answers from several services into one response. The price: if the gateway stops, nothing behind it can be reached, so it is run as several copies.
API gateways, step by step
Simplified: real gateways run their checks in a configurable order, and services often still verify what a caller is allowed to do. The limit of 100 requests a minute is an invented example.
- Step 1: Many services, many doors. A shop’s backend is split into three services: users, orders and products. If the app talks to each one directly, it must know three addresses, and every service has to repeat the same chores: check who is calling, limit how fast they call, write the logs. Add a fourth service and all of it is built again.
- Step 2: One front door. An API gateway is a server placed in front of all the services. The client now knows a single address and sends every request there. The services move onto a private network that only the gateway can reach, and the shared chores move into the gateway, where they exist once.
- Step 3: Routing. The gateway’s first job is routing: looking at each request and passing it to the service that handles it. Usually the start of the path decides. GET /orders/7 begins with /orders, so it goes to the orders service. The client never learns where that service lives, so services can be moved or split without the client changing.
- Step 4: Checks in one place. Before routing, the request passes three stations. Authentication checks the token that proves who is calling. Rate limiting counts the caller’s requests: this is number 37 of the 100 allowed this minute. Logging records the request. The services behind no longer need their own copies of these chores.
- Step 5: Turned away at the door. A request that fails a check is answered by the gateway itself. A missing or invalid token gets 401 Unauthorized. The 101st request in a minute, with a limit of 100, gets 429 Too Many Requests. Neither reaches a service, so the services spend no effort on callers that should not get in.
- Step 6: One call, several services. The app’s home screen needs the user’s name, their recent orders and some suggested products. Instead of three requests from a phone over a slow connection, the app sends one. The gateway calls the three services over the fast private network, combines their answers and returns a single response. This is called aggregation.
- Step 7: A single point of failure. The price of one front door: every request depends on it. If the gateway stops, the whole API is unreachable, even though each service behind it is healthy. So gateways are run as several identical copies, and kept simple. A gateway also adds one more hop to every request, and can become a bottleneck if given too much to do.
Try it yourself
Learn this first
Learn next
- Load balancersOne address, many servers: how requests are shared out and dead servers skipped. Beginner
- Circuit breaker patternA switch that stops calls to a failing service so the failure cannot spread. Intermediate
- Kubernetes ingressOne public door into a cluster, with host and path rules that pick the service. Intermediate
Spotted a mistake on this page?