In brief
An API gateway is a single entry point between clients and services. It acts as a reverse proxy: it routes requests to the right services and takes over shared concerns such as authentication, TLS, rate limiting, logging and caching. Services stop repeating the same logic, and clients know one address. The most common mistakes are a gateway that can be bypassed, business logic in gateway rules, badly configured limits and retries of requests that are not idempotent.
What an API gateway does
The Microsoft Azure Architecture Center describes an API gateway as a centralized entry point that acts as a reverse proxy and can perform cross-cutting tasks. Without a gateway, a client has to know the addresses of many services, handle their failures and make many network round trips, and every public service handles authentication and limits on its own.
Typical gateway responsibilities:
- routing: `/policies/*` to the policy service, `/claims/*` to the claims service, API versions in the path or a header;
- authentication and coarse authorization: checking an OAuth 2.0 token, an API key or a client certificate (mTLS);
- rate limiting and quotas for partners, with the HTTP 429 Too Many Requests response defined in RFC 6585;
- TLS termination, compression, response caching;
- logging, metrics and a correlation ID passed on to services;
- transformations: header changes, simple protocol translation, masking fields in responses.
Products in this category include Azure API Management, Amazon API Gateway, Kong and Apigee, as well as reverse proxies such as NGINX or HAProxy with the right configuration. In Kubernetes, external traffic is handled by Gateway API, the Kubernetes project's standard for layer 4 and layer 7 routing.
Three patterns: routing, aggregation, offloading
Microsoft describes the gateway through three design patterns:
| Pattern | What it does | When it helps | Risk |
|---|---|---|---|
| Gateway routing | one address, layer 7 routing to many services | clients should not know how services are split | routing rules grow without structure |
| Gateway aggregation | one client request, several service calls, one response | a mobile app needs data from many services on one screen | the gateway starts to hold business logic |
| Gateway offloading | TLS, authentication, limits and logs in one place | many services repeat the same handling | services stop checking permissions themselves |
When aggregating for different clients (a mobile app, a partner portal, an internal console), the backend for frontend pattern often works better: a separate thin layer for each client type, owned by that client's team. The gateway stays simple, and screen logic does not mix with security policy.
API gateway, load balancer and service mesh
These three often sit side by side, but they do different jobs:
- a load balancer spreads traffic across instances of one service and checks their health; it knows nothing about partners, keys or quotas;
- an API gateway handles north-south traffic, from clients and partners into the system, and manages the API as a product: versions, documentation, keys, limits;
- a service mesh handles east-west traffic between services inside the platform: mTLS between services, retries, telemetry.
A smaller platform usually needs only a gateway. A mesh makes sense with many services and a requirement to encrypt internal traffic. Whether splitting a system into services pays off at all is covered in the article on microservices.
Common misconfigurations
These mistakes keep coming up in security tests and architecture reviews:
- A gateway that can be bypassed. Services behind the gateway have public addresses or are reachable from the whole internal network, so an attacker calls them directly, without authentication or limits. Services should accept traffic only from the gateway (private network, mTLS or a signed header).
- Checking only the token signature. The gateway verifies the JWT signature but not the audience, expiry or scope. A token issued for a different application gets through. Details are in the article on OAuth 2.0 and OpenID Connect.
- Authorization only at the gateway. The gateway checks that the client is logged in, but the service does not check whether that client may read the policy with a given number. That is BOLA, first on the OWASP API Security Top 10.
- Limits that do not work. A counter kept separately on each gateway instance, a limit only per IP address (partners behind one NAT block each other) or no limit on request size. OWASP describes this as unrestricted resource consumption.
- Retrying POST requests. The gateway retries after a timeout, while the service has already recorded the payment. Only idempotent operations, or operations with an idempotency key, should be retried.
- Inconsistent timeouts. The gateway waits 30 seconds, the service 60 and the client 10. The client retries, the gateway keeps waiting, the service computes the same thing three times. Timeouts should shrink from the client inward.
- Tokens and personal data in logs. Full headers and request bodies in gateway logs are a leak that is hard to clean up later.
- Forgotten versions. An old API version stays published with nobody watching it. OWASP calls this improper inventory management.
- Caching responses with customer data without including identity in the cache key, so one customer receives another customer's data.
A broader list of API threats is covered in the article on API security.
How to test the gateway and its configuration
Gateway configuration is code and should go through the same steps as code: repository, code review, tests, deployment through a pipeline. Things worth checking:
- routing: every path reaches the right service and version, and unknown paths get a 404 rather than a default service;
- authentication: no token, an expired token, a token for another audience, a token without the required scope;
- limits: a load test shows that the limit applies across all gateway instances combined and returns 429 with a `Retry-After` header;
- bypass: an attempt to call the service directly, skipping the gateway;
- contracts: the gateway does not change responses in a way that breaks clients, which API contract testing helps catch.
We combined tests like these with penetration testing in the project on testing a new API for third-party providers in open banking.
Questions and answers
What is the difference between an API gateway and a load balancer?
A load balancer spreads traffic across service instances and checks that they are up. An API gateway understands the API: it routes requests by path and version, checks tokens and keys, applies limits per partner and collects per-client metrics. In practice they often sit one behind the other: a load balancer in front of the gateway instances, and the gateway in front of the services.
Does an API gateway replace authorization in services?
No. The gateway can check that the client is authenticated and has a general scope, but only the service knows whether this client may see a specific record. Object-level authorization has to stay in the service. The gateway is the first line of defense, not the only one.
Do I need an API gateway for a handful of services?
If the API is public or shared with partners, usually yes: limits, keys, logs and a single address are useful from the first partner. For a few internal services a reverse proxy or a Kubernetes ingress is often enough. Start with your cloud provider's managed service before building your own gateway.
Is having several API gateways a mistake?
Not necessarily. A common and sensible setup is one gateway for external traffic and another for internal traffic, or separate gateways for different product groups. Problems start when every team configures its own gateway without shared rules for authentication, logging and limits.