API gateway
One front door for many backend services: every client calls the gateway's address, never the services behind it. Before a request goes on, the gateway checks its API key (which client is calling, though a key built into an app can be pulled out of it) and its token (proof of who is calling), counts it against that client's rate limit, and routes it by its path, so requests that fail a check stop there. Run at least 2 gateway instances so it is not a single point of failure, and let each service still verify the token too, in case a gateway route is wrong or bypassed.
The key says which client is calling; the token proves who. The gateway checks both, then the limit (5 per second per key), then routes by path. Send with no key or token, or send 10 at once to pass the limit. Each partner press starts a new second.
Reached Orders: 0 · Reached Users: 0 · 401 at the gateway: 0 · 429 at the gateway: 0
Every caller uses one address, the gateway. The Orders and Users services sit behind it.
Say it in a prompt
Put an API gateway in front of the Orders and Users services, and run at least 2 gateway instances behind a load balancer so it is not a single point of failure. Route /orders/* to the Orders service and /users/* to the Users service. On every request check the x-api-key header, which says which client is calling, and the Authorization bearer token (a signed JWT that has not expired), which proves who is calling; answer 401 without calling any service when either is missing or invalid. Limit each key to 5 requests per second and answer 429 with a Retry-After header above that. Keep both services on a private network that only the gateway can reach, and verify the token again inside each service. Vague vs precise prompt
Vague prompt
let our partners call our services Typical resultGives each service its own public address and adds a key check to the one the partner asked about. Partners must know every address, the other services accept any request, and one partner's runaway script can flood them all.
Precise prompt
Put 2 API gateway instances in front of the Orders and Users services: route /orders/* and /users/* by path, check the x-api-key header and the bearer token and answer 401 at the gateway when either is missing or invalid, limit each key to 5 requests per second with 429 and Retry-After, and keep the services on a private network only the gateway can reach. Typical resultPartners call one address, and one gateway instance can fail without taking the API down. Requests with no valid key or token, or over the limit, stop at the gateway with 401 or 429, so the services only see allowed traffic, and moving a service only changes one route. Each service still verifies the token, in case a request gets past the gateway.
Seen on
- Azure Architecture Center: An API gateway is a central entry point that acts as a reverse proxy and routes client requests to the right services. It can also take on shared jobs such as authentication, SSL termination and rate limiting.
- Azure Architecture Center, Gateway Routing pattern: Clients call one endpoint and the gateway routes each request by its URL to the right service. It warns that the gateway can become a single point of failure, and suggests making the services reachable only through it.
- microservices.io: The API gateway pattern: a single entry point for all clients that routes requests to the right service or fans out to several, and can check that the client is allowed to make the request.
- Amazon API Gateway: Usage plans use API keys to tell clients apart and throttle each key's requests. The docs warn not to use API keys alone for authentication or authorization.
You might describe it as
- one place to check keys, tokens and limits for every API
- stop requests without a key before they reach our services
- send /orders to one service and /users to another
Not to be confused with
- Load balancer
An API gateway adds per-client checks in front of your services, such as keys, tokens and rate limits; a load balancer's main job is spreading requests over healthy copies of a service, though many can also route by path.
- Rate limiting
An API gateway is a place where rate limiting often runs, next to checking keys and routing by path; rate limiting is that one job, capping how many requests each client may make.