Video in progress
When do you need an API gateway and a BFF (backend for frontend), and how do they turn into a problem?
A gateway is for shared cross-cutting jobs: TLS, authentication, rate limiting, routing. A BFF gathers and shapes data for one kind of client (mobile, web). The trouble starts when they become a "new monolith" where business logic piles up.
📖 Answer
Without a gateway, every service verifies tokens itself, sets its own limits, configures its own CORS, and each one is a little different. A gateway pulls that into one place. But there must be no domain logic inside it: the day "if the order amount is over 1 million..." gets written in the gateway is the first step in the wrong direction.
A BFF solves a different problem. If a mobile app sends 7 requests to 7 services for one screen, on a slow network that's seconds. The BFF calls them in parallel on the server side and returns the fields mobile needs in a single response. Web gets a different BFF with a different shape.
The rules I give teams:
- The client team owns the BFF. The mobile team can change its own BFF without waiting for the backend team. If a central team owns the BFF, it becomes a queue and a bottleneck.
- A BFF only gathers and adapts. Business rules stay in the domain services; otherwise web and mobile work with different rules.
- The gateway is simple and very reliable. It's the single point for all traffic; one wrong configuration stops everyone. Configuration changes go out with a canary too.
- Timeouts and partial results. If one service is slow, the BFF must not hold up the whole screen: it returns that block empty.
GraphQL is often used in place of a BFF. It gives the client flexibility, but it puts the work of controlling query cost (depth limits, complexity accounting) and solving the N+1 problem with a DataLoader on the server.
Sign up to mark studied questions and save your progress.