Why Use an API Gateway in Microservices Setups
When you split a monolith into microservices, you also split the problem of talking to your services. Clients — web apps, mobile apps, other services — now need to know where each service lives, how to authenticate against it, how to handle its failures, and how to version its API. An API gateway exists to absorb all of that into one layer so individual services don't each have to reinvent it.
The short answer to "why an API gateway in microservices" is: without one, every client has to handle service discovery, auth, retries, and cross-cutting concerns on its own, and every service has to duplicate that logic too. A gateway centralizes it, giving you a single, consistent entry point instead of dozens of inconsistent ones.
The problem it solves: N clients × M services
Picture five services — users, orders, payments, inventory, notifications — each with its own host, port, auth scheme, and versioning quirks. Now picture three clients calling them: a web app, a mobile app, and a partner integration.
Without a gateway, that's up to 15 direct connections, each one needing:
- Service discovery (where is this service right now?)
- Authentication and authorization
- Rate limiting per client
- Retry and timeout logic
- Request/response logging for debugging
Multiply that by every new service you add, and the coordination cost grows faster than the services themselves. This is the core reason teams introduce a gateway early rather than waiting until it hurts.
What an API gateway actually does
An API gateway sits between clients and your services and handles:
Routing — mapping a public path like /api/orders to the internal orders-service:8080, without the client knowing internal topology.
Authentication and authorization — validating API keys, JWTs, or OAuth tokens once, at the edge, instead of in every service.
Rate limiting and quotas — protecting backend services from being overwhelmed by a single noisy client, enforced in one place.
Aggregation — combining calls to multiple services into one response when a client needs data that spans services.
Observability — a single point to log requests, track latency, and emit metrics across the whole system, instead of stitching together logs from ten services.
Protocol translation — exposing a clean REST or JSON API externally while services internally use gRPC, message queues, or different protocols.
A minimal gateway routing config often looks like this:
routes:
- path: /api/orders/*
upstream: http://orders-service:8080
auth: required
rate_limit: 100/min
- path: /api/users/*
upstream: http://users-service:8081
auth: required
rate_limit: 200/min
That's the whole idea in one file: one entry point, consistent rules, services that stay simple.
Why not just let clients call services directly?
You technically can, and small systems often do. The gateway becomes necessary once any of these become true:
- You have more than a handful of services and clients need a stable contract that doesn't change when internal architecture does
- You need consistent auth across services, especially if some are internal-only and others are public-facing
- You're getting hit with abuse or unpredictable traffic and need centralized rate limiting
- You want to version APIs without forcing every service to implement versioning independently
- You need one place to see what's actually happening across the whole request path, for debugging and incident response
If none of these apply yet, a gateway is premature. If several do, the lack of one is usually already costing you in duplicated code and inconsistent behavior across services.
A concrete example: applying the same pattern to AI APIs
The gateway pattern isn't unique to internal microservices — it shows up anywhere multiple clients need consistent, controlled access to a shared backend. SubToAPI is an example applied to Claude access: instead of every internal tool or app authenticating against a shared account directly, you generate scoped application API keys (sub_live_...), route all requests through one HTTPS endpoint, and get centralized usage metadata and rate limiting per key — the same problems a microservices gateway solves, just for LLM access instead of internal services.
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-3-5-sonnet-latest",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Summarize this in 3 bullet points."}]
}'
Each internal app or team gets its own key, so you can see exactly which service is consuming what, revoke access without breaking other clients, and enforce limits per key rather than per shared credential. See the quickstart or messages docs for the full request/response shape, or streaming if your clients need token-by-token output.
Trade-offs to be honest about
An API gateway isn't free. It adds:
- A new point of failure — if the gateway goes down, everything behind it becomes unreachable. Plan for redundancy.
- Latency — an extra network hop, usually a few milliseconds, but it adds up under load.
- Operational surface — someone has to run, monitor, and scale the gateway itself.
- A temptation to overload it — business logic creeping into gateway config is a common anti-pattern; keep it to routing and cross-cutting concerns, not domain logic.
These are manageable trade-offs, but they're real ones. The right framing isn't "gateways are always correct" — it's "the coordination cost of not having one usually exceeds the operational cost of running one, past a certain number of services and clients."
Practical guidance
- Start without a gateway if you have one or two services and one client type.
- Introduce one once you have multiple client types, need centralized auth, or are duplicating rate-limiting logic across services.
- Keep the gateway thin: routing, auth, rate limiting, logging. Push business logic back into services.
- Version your gateway config alongside your services so routing changes are reviewable and reversible.
FAQ
Does every microservices architecture need an API gateway? No. Small systems with one or two services and a single client can skip it. It becomes valuable once you have multiple clients or services and are duplicating auth, routing, or rate-limiting logic across them.
Is an API gateway the same as a load balancer? No. A load balancer distributes traffic across instances of one service. A gateway routes across many different services and typically adds auth, rate limiting, and aggregation on top — though some gateways include load balancing as one of their features.
Can an API gateway become a bottleneck? Yes, if it's not scaled or made redundant. Because all traffic passes through it, it needs horizontal scaling and health checks like any other critical service — treat it as production infrastructure, not an afterthought.