Catalog My learning Blog
System Design Roadmap: 100-Day Course for Engineers · Phase 2 · Lesson 18 · 4 min read

API Gateway vs Backend for Frontend (BFF)

On this page
  1. Why microservices need an API gateway
  2. What an API gateway does
  3. The backend for frontend (BFF) pattern
  4. Worked example: a product page through a gateway and BFFs
  5. Keep gateways and BFFs thin
  6. Gateway authentication isn't service authorization
  7. Check yourself

Why microservices need an API gateway

In a monolith, authentication, rate limiting and request logging live in one codebase. Split into ten services and each concern now exists ten times, implemented ten slightly different ways. Clients also need to know where each service lives, and a mobile product page might make six calls to six services over a slow network.

Two patterns address this: the API gateway for shared concerns, and the Backend-for-Frontend (BFF) for client-specific needs.

What an API gateway does

It's the single entry point for all external traffic, and it typically handles:

  • Routing: /orders/* to the order service, /catalog/* to the catalog service.
  • TLS termination and authentication: validate the token once at the edge, pass the user's identity inward.
  • Rate limiting and quotas per client (Day 20).
  • Logging, metrics and request ids for tracing (Day 88).
  • Transformations: header changes, simple protocol translation such as REST to gRPC.

Products include Kong, NGINX, Envoy-based gateways and managed services like Amazon API Gateway and Azure API Management.

The backend for frontend (BFF) pattern

Different clients want different things from the same services. The web storefront wants rich product data, all reviews and recommendations. The mobile app on a cellular network wants one small response with the price, stock and three reviews. A partner integration wants a stable, versioned contract and nothing else.

A BFF is a thin service per client type that calls the backend services, combines and trims the results, and returns exactly what that client needs. The name comes from Phil Calçado at SoundCloud, and Sam Newman's write-up is the standard reference (Sam Newman). A useful rule: the team that owns the client owns its BFF.

In .NET, YARP (Microsoft's reverse proxy library) is a common base for both: routing and policy as configuration, with C# where you need it.

One API gateway at the edge, with a separate BFF per client type shaping calls into the shared services behind it.
One API gateway at the edge, with a separate BFF per client type shaping calls into the shared services behind it.

Worked example: a product page through a gateway and BFFs

Behind the scenes: product, pricing, reviews and inventory services.

Code
Mobile app  ->  Gateway  ->  Mobile BFF  ->  product, pricing, inventory (in parallel), reviews?limit=3
                                             returns one compact response

Web app     ->  Gateway  ->  Web BFF     ->  product, pricing, inventory, reviews, recommendations
                                             returns a rich response

The gateway handles TLS, checks the token once, applies a per-customer rate limit and routes by path or client. Each BFF calls services in parallel, with timeouts and fallbacks: if reviews are slow, the page still renders without them.

Keep gateways and BFFs thin

Gateways and BFFs should stay thin: routing, policy, combining and shaping. Business rules, such as discount calculation or stock reservation, belong in the services. Once business logic creeps into the gateway, every team needs to change the gateway, and it becomes the new monolith.

Also watch for the gateway as a single point of failure. Run several instances behind a load balancer, and keep its configuration in version control like any other code.

Gateway authentication isn't service authorization

A gateway that validates the token and passes the user id inward in a header, such as X-User-Id: 42, works only if nothing else can reach the services. If any service is reachable directly, from another compromised service or a misconfigured network, a caller can set that header itself and become user 42.

So keep services off the public network, pass the signed token inward rather than a plain id, and let each service still check that this user may touch this object. The gateway answers "who is this?"; the service answers "may they do this?" (Days 81 and 84).

Check yourself

Anyone who can reach a service directly can forge the header and act as any user. Forward the signed token, keep services private, and authorize in each service.

All traffic enters through the gateway, which authenticates, rate-limits and routes. Web and mobile traffic goes to their own BFFs, which combine service calls into client-shaped responses. Partners hit a stable, versioned public API.

The BFF makes those calls in parallel inside the data center, where latency is low, and returns one compact response, so the phone makes a single round trip.

It should handle cross-cutting concerns for all traffic: routing, TLS, authentication, rate limiting and request logging. It shouldn't contain business logic or screen-specific shaping. Business rules belong in services and screen-specific aggregation belongs in BFFs.

Different clients need different data shapes, call patterns and release schedules. A BFF per client lets each frontend team change its backend quickly, while a shared one accumulates special cases and becomes a bottleneck.