An API gateway is one front door for all your backend services. Mobile apps, web front ends and partners call a single host, such as api.example.com. The gateway decides which service handles each request and applies the rules every request needs: authentication, rate limiting, timeouts, logging and TLS termination. Without one, every service implements those rules itself, each slightly differently.
What a gateway does
- Routing.
/orders/*goes to the order service and/payments/*to payments, based on path, host, header or method. - Authentication. It validates API keys or OAuth 2.0 access tokens before a request reaches a service, then passes the verified identity on in a header.
- Rate limits and quotas. Per API key, user or IP address, returning
429 Too Many Requestswhen a client goes over. - Request shaping. Header changes, request and response transformation, and protocol translation, for example JSON over HTTP from clients and gRPC to internal services.
- Observability. One place to log every call, measure latency and attach a trace ID.
- Aggregation. One client call can fan out to several services, with the gateway combining the results. Building one gateway per client type (mobile, web, partner) is called the backend-for-frontend pattern.
Amazon API Gateway supports REST, HTTP and WebSocket APIs. Other common choices are Azure API Management, Kong, and Spring Cloud Gateway for Java teams.
Example: a lending app's gateway
A lending app has three clients (Android, iOS and a partner portal) and several services. A simplified gateway configuration, in a generic format:
routes:
- path: /v1/applications/**
service: loan-origination:8080
auth: oauth2 # validate the JWT, require scope applications:write
rateLimit: 20/minute per user
- path: /v1/repayments/**
service: repayments:8080
auth: oauth2
- path: /partner/v1/**
service: partner-api:8080
auth: api-key
rateLimit: 600/minute per key
timeouts:
upstream: 10s # answer 504 if the service is too slow
A request from the Android app to /v1/applications is checked for a valid token, counted against that user's limit and forwarded. The loan service never sees an unauthenticated call. When the repayments service slows down, the gateway returns 504 Gateway Timeout (RFC 9110, section 15.6.5) after 10 seconds instead of holding connections open. That status is what people usually mean by an "API gateway timeout".
API gateway vs load balancer
A load balancer spreads traffic for one service across its healthy instances. It works at the connection level (layer 4) or does simple HTTP routing (layer 7). A gateway understands your API: who the caller is, which plan they are on and which version of an endpoint they want.
Most production setups use both. The gateway sits at the edge, and a load balancer or Kubernetes service sits behind it for each service. A service mesh, where you have one, handles traffic between services inside the cluster. The gateway handles traffic coming in from outside.
Common mistakes
- Business logic in the gateway. Pricing rules or eligibility checks in gateway plugins are hard to test and version. Keep the gateway thin.
- Trusting everything behind it. Services should still check that the caller may access the specific record, in line with zero trust.
- A shared gateway nobody owns. Route changes queue up behind one central team. Keep the configuration in Git and let service teams change their own routes through review.
- No timeouts or circuit breakers on upstream calls, so one slow service ties up the gateway for everyone.
Choosing the API style behind the gateway is covered in REST vs GraphQL vs gRPC.