Nguyen Le Phong

What Belongs in Your API Gateway (and What Doesn't)

What an API gateway should own (routing, token checks, rate limits, tracing), when a BFF earns its keep, and how to keep the gateway from becoming your next monolith.

It's Tuesday morning and you're debugging the customer profile page. The network tab shows eleven requests before anything renders: identity, billing, permissions, feature flags, plus two more that only exist because an old version of the mobile app wants a different response shape. Nothing is broken yet. It just feels like every client has to carry a map of your backend in its pocket.

That's usually when someone says "let's put a gateway in front of this." Fair enough. Chris Richardson describes the API gateway as the single entry point for all clients: it either routes a request to one service or fans it out to several (microservices.io). The pattern itself is simple. The hard part is deciding what gets to live there, because a gateway that takes on everything ends up as a second application nobody owns. Let's go through the jobs that fit, the jobs that don't, and how to tell them apart.

Two engineers look into a customer profile bug at a desk while a dashboard of service calls fills up beside them.
Eleven calls for one profile page is often the moment a team starts talking about a gateway.

The jobs that belong at the edge

Here's a quick test for any gateway job: without a gateway, would every service have to do this, and would they all do it the same way? Microsoft's architecture guide calls this gateway offloading: you move shared work like TLS termination, authentication and throttling out of each service and into one place (Gateway Offloading pattern). In practice the list looks like this:

  • Routing. /api/orders goes to the order service, /api/profile goes to identity. Clients never learn internal service names, so you can split or move a service behind the gateway and keep the public URL the same.
  • Checking who's calling. Verify the token, reject expired credentials and pass a trusted identity (user, tenant, claims) to the services behind it.
  • Rate limits and request size limits, so one client stuck in a retry loop can't take everyone else down with it.
  • TLS termination, header cleanup and a trace ID on every request.

The same Microsoft page has a one-line rule worth copying: never offload business logic to the gateway. Keep the routing table dull and easy to read. If you have to open the gateway config to find out how a partner's prices get calculated, your map has turned into a maze.

Two backend engineers sketch request routes from an API gateway to the services behind it on a whiteboard.
A routing table should read like a map. When product rules start hiding in it, move them out.

The gateway asks who you are, the service asks what you may do

If you remember one thing from this post, make it this split. The gateway can answer "who is calling?": user 42, tenant A, these scopes. It usually can't answer "may user 42 read order 9001?", because only the order service knows who owns order 9001.

Skip that second check and you get the bug OWASP ranks first in its 2023 API Security Top 10: broken object level authorization. The caller is logged in and allowed to hit the endpoint, swaps the ID in the URL and reads someone else's record. OWASP's fix is to check, in every function that uses client input to access a record, that the logged-in user is allowed to do that action on that record (OWASP API1:2023). A gateway can't do that for you. Treat the identity it passes down as an input, and keep the ownership check next to the data.

Two more details from Microsoft's offloading guide: don't treat forwarded headers as proof of identity unless they've been verified, and configure backends to accept traffic only through the gateway. Otherwise a client can call a service directly and skip the auth, limits and logging you just set up.

Rate limits that explain themselves

A rate limit protects you from accidental loops, scrapers and partner bugs. The decision that actually matters is what you count by: IP, user, tenant, API key or route. A public search endpoint, a payment provider's callback and an admin export need three different rules. Give them one shared rule and sooner or later you'll throttle the wrong caller.

When you do reject a request, say so. HTTP has a status code for exactly this: 429 Too Many Requests, which can carry a Retry-After header telling the client how long to wait (RFC 6585). The RFC deliberately doesn't define how you identify the caller or count requests. That part is your design.

HTTP/1.1 429 Too Many Requests
Retry-After: 30
Content-Type: application/json

{"error": "rate_limited", "limit": "100 requests per minute per API key"}

Log which key crossed the line as well. A throttle that drops requests with no clear status and no log line looks exactly like an outage to whoever is on the other end. For algorithms and the user-facing side, see Rate Limiting and Throttling Without Punishing Real Users.

Aggregation and backends for frontends

Some gateways do more than forward. A mobile dashboard that needs five services makes one call, and the gateway fans out and returns a single response shaped for that screen. Microsoft calls this gateway aggregation and recommends it when a client needs several backends for one operation, especially over high-latency networks such as cellular (Gateway Aggregation pattern). The same page lists the costs: the gateway can become a single point of failure and a bottleneck, and if the aggregation needs real domain logic, it should move into a dedicated service behind the gateway.

Once a gateway knows the shape of a screen, it really belongs to the team that builds that screen. Sam Newman's Backend for Frontend pattern makes this explicit: one backend per user experience, usually owned by the same team as the UI. Newman credits the name to Phil Calçado, then at SoundCloud, and passes on a rule of thumb from Stewart Gleadow: "one experience, one BFF" (Sam Newman, Backends For Frontends). He also warns against a single general-purpose aggregating edge service, because it tends to bloat.

A BFF earns its keep when clients really differ: a mobile app on a patchy connection, a partner API with strict contracts, a web app doing server-side rendering. It's a poor fit when the only reason is "changing the service takes too long." Then you've just added another layer to patch, and the web and mobile BFFs will drift apart unless someone owns their contracts.

Gateway or service?

Fine at the gateway Keep in the owning service
Routing /api/orders to the order service Deciding whether an order can still be cancelled
Verifying a token and passing user and tenant downstream Checking that this user owns order 9001
100 requests per minute per API key Pricing, discounts and eligibility rules
Trace IDs, access logs, TLS termination Joining data across services to compute a business result
Shaping one screen's response (in a BFF owned by that screen's team) Anything the next client would have to copy to get the same answer

How a gateway turns into the next monolith

It starts small: one header rewrite, one fallback for a partner, one temporary field mapping. Each change is reasonable, and the gateway is the fastest place to make it. Six months later, pricing rules, permission exceptions, data joins and A/B logic all live in gateway config. From the outside it still looks like microservices. The product logic has moved to the edge, where it's hard to test and no on-call rotation really owns it.

Thoughtworks put this on Hold in their Technology Radar under the name "overambitious API gateways": gateway products that keep adding features until business logic and process orchestration end up in middleware, which makes designs harder to test and deploy. Their view is that gateways are fine for concerns like authentication and rate limiting, while data transformation and rule processing belong in services (Thoughtworks Technology Radar). The entry first appeared in 2015 and was last assessed in 2018.

A sentence you can paste into your team's README: the gateway protects and routes, and it never decides a business outcome. The best gateways are a bit boring, and that's a compliment.

It's on every request, so plan for failure

Richardson names two costs up front: one more moving part to build, deploy and run, and one more network hop (microservices.io). The bigger worry is that the gateway now sits on every critical path. If it goes down, every service behind it can be perfectly healthy and your users still see an outage.

So decide partial failure ahead of time. The profile page aggregates five calls and billing times out: do you hide the billing widget, serve cached data or fail the whole page? Microsoft's aggregation guide says to make that choice explicit: return a partial response when missing data is acceptable, and fail the request when the result has to be complete and consistent. Put a timeout and a circuit breaker on each downstream call. This post on timeouts, retries and circuit breakers goes deeper.

Then make it observable. Give each request a trace ID at the gateway and pass it along. The W3C Trace Context standard defines the traceparent header so different tools agree on the format (W3C Trace Context). Log the route, caller type, status, latency, which backend answered, the rate-limit decision and any timeout reason. Track error rate and p95 latency per route. With that in place, the next time someone says "the gateway is slow," you can show whether it's the gateway or the service it's waiting on.

An on-call engineer and a teammate check gateway dashboards with latency charts and traces while one backend is failing.
When the gateway sits on every request, traces and rate-limit logs are how you work out what's going on during an incident.

Try this week

  • Open your gateway config and list every rule that isn't routing, auth, limits or headers. Each one is a candidate to move into a service.
  • Pick one endpoint that takes a record ID from the client and confirm the service checks ownership, not only the gateway.
  • Trigger your own rate limit and look at what the client receives. If it isn't a 429 with a Retry-After, fix that first.
  • For one aggregated endpoint, write down what the user sees when each downstream call times out.

Still deciding whether you need separate services at all? Start with When to Split a Monolith Into Microservices.

What did you think?

Frequently asked questions

What is an API gateway?
An API gateway is the single entry point for all clients of a set of services. It either routes a request to one service or fans it out to several and combines the results. It's also a common place for shared work like TLS termination, token checks, rate limits and trace IDs, as Chris Richardson describes on microservices.io.
Should authorization happen in the API gateway?
Authentication, yes: the gateway can verify the token and pass a trusted identity downstream. Object-level authorization, such as whether this user may read this order, belongs in the service that owns the data. Skipping that check leads to broken object level authorization, the top risk in the OWASP API Security Top 10 for 2023.
What is the difference between an API gateway and a BFF?
A general API gateway serves every client through one entry point. A Backend for Frontend is a gateway built for one user experience, such as the mobile app, and usually owned by the team that builds that UI. Sam Newman's rule of thumb, credited to Stewart Gleadow, is one experience, one BFF.
What should not go in an API gateway?
Business rules: pricing, discounts, eligibility, data joins that compute a business result, and process orchestration. Microsoft's Gateway Offloading guidance says never to offload business logic to the gateway, and Thoughtworks put overambitious API gateways on Hold in its Technology Radar for the same reason.
What should a gateway return when a client hits a rate limit?
Return HTTP 429 Too Many Requests, defined in RFC 6585, ideally with a Retry-After header saying how long to wait. Also log which key (IP, user, tenant or API key) crossed the limit, so the person on call can tell throttling apart from an outage.