API Gateway
Escrito por Darjus Kazakevic · revisado el
Clients see one API, however many services run behind it.
With 11 services, a client that calls them directly needs 11 hostnames, 11 auth integrations and 11 CORS setups. An API gateway gives it one. The gateway authenticates once at the edge, applies rate limits, routes each path to a service, and can combine several calls into one response.
The cost is an extra hop on every request, usually 1–5 ms, and a shared point of failure. Keep business logic out of it. A gateway that decides whether an order can be cancelled is a distributed monolith with a router in front.
En esencia — Put one entry point in front of many services that checks identity and rate limits once, routes each path and can merge responses.
Cuándo recurrir a él
Recurre a él cuando
- Clients call many services and each one repeats auth, CORS and rate limits
- You want to authenticate once and forward a trusted identity
- You need to split or move services without changing client URLs
Reconsidéralo cuando
- One service or a modular monolith, with nothing behind it to route
- As a place to settle a business rule that two services disagree about
- Only TLS and load balancing are needed, which a reverse proxy already does
Conceptos clave
- Routing
- A table that maps a path, host or header to a service, such as /api/orders/* → orders. It hides the internal layout: you can split or move a service without changing any client.
- Authentication at the edge
- The gateway checks the token (for example a JWT) once and forwards a trusted identity header, such as x-user-id. Services behind it read that header and do not parse tokens themselves.
- Rate limits and quotas
- A limit per API key or user, such as 600 requests per minute, stops one client from using all the capacity. Extra requests get 429 Too Many Requests with a Retry-After header.
- Aggregation
- One client call fans out to several services in parallel and returns one merged response. On a mobile network with 100 ms round trips, three chained calls become one.
- Gateway vs reverse proxy
- A reverse proxy (NGINX, HAProxy) forwards traffic, ends TLS and balances load. A gateway adds API concerns: who the caller is, their quota, versions and aggregation. Kong and Envoy can do both.
Reglas de diseño
- Keep business logic out of the gateway
- Authenticate, limit, route and forward identity: things true of every request. The first business rule in a gateway is never the last, and soon every feature needs a gateway deploy.
- Overwrite identity headers from clients
- If services trust x-user-id, the gateway must replace any x-user-id the client sent. If it only adds its own, anyone can act as any user by sending one header.
- Treat it as a shared point of failure
- Every request crosses it. Run at least two instances behind a load balancer, with their own health checks and capacity. If every team must change it to ship, it becomes a queue.
- Set a timeout on every route
- A slow service must not hold gateway connections forever. Give each route a timeout shorter than the client's, such as 3 s against 10 s, and fail fast so the client can retry or degrade.
Errores habituales
- Business logic in gateway plugins. Soon a pricing change needs a gateway deploy.
- Trusting an x-user-id header the client sent. Anyone can act as any user.
- No per-route timeouts. One slow service holds every gateway connection, and all routes fail.
- One gateway shaped for every client. Web and mobile fight over the same aggregated responses: time for a BFF.