Backend for Frontend
BFF
Escrito por Darjus Kazakevic · revisado el
The client team owns its API, so a screen change waits for no one.
One general API for web, mobile and TV serves none of them well. The phone downloads fields that only the web shows, and every screen change needs the API team. A Backend for Frontend (BFF) gives each client type its own thin backend, owned by the team that builds that client.
The BFF calls the services it needs in parallel and returns one response per screen. The cost is some duplicated code between BFFs. That is usually cheaper than the coupling it removes, as long as each BFF stays thin and holds no business rules.
En esencia — Give each client type (web, mobile, TV) its own thin backend that calls the services and returns exactly the shape its screens need.
Cuándo recurrir a él
Recurre a él cuando
- Web and mobile need different data shapes, and the phone over-fetches
- Every screen change waits on the team that owns the shared API
- A screen needs data from several services in one round trip
Reconsidéralo cuando
- One client, or several that show the same data the same way
- No client team exists to own and run its own backend
- A GraphQL API already lets each client ask for its own shape
Conceptos clave
- One BFF per client type
- Split by client experience, not by page or feature: one BFF for web, one for mobile, maybe one for partners. Each one changes at the speed of its own client.
- One endpoint per screen
- A BFF endpoint matches a screen. GET /screens/order/42 calls orders, shipping and recommendations in parallel and returns one payload. On a phone with 100 ms round trips, that replaces three chained calls with one.
- Shaping the response
- The BFF trims and formats data for its client: only the fields the screen renders, dates as labels, image sizes for the device. A phone can download 2 KB instead of 40 KB.
- Ownership
- The client team owns its BFF and deploys it with the app. A screen change then needs no meeting with another team. This ownership is the main reason the pattern exists.
Reglas de diseño
- Shape responses, never decide business rules
- A BFF merges and trims data for one client. If it decides whether a discount applies, the web and mobile BFFs will soon disagree. Put that rule in the pricing service.
- The team that owns the client owns the BFF
- The point of a BFF is that a screen change needs no cross-team negotiation. A BFF run by a central API team brings that bottleneck back, one level lower.
- Split by client type, not by screen
- One BFF per experience: web, mobile, partner API. A BFF per page or per feature multiplies deploys and duplicated code, and makes no client simpler.
- Give optional calls a timeout and a fallback
- If recommendations take 2 s, return the order without them. A timeout and an empty fallback on each optional call stop one slow service from breaking the whole screen.
Errores habituales
- A discount rule in the web BFF and a slightly different one in the mobile BFF. Prices differ by app.
- One BFF per screen, so the mobile team now runs twelve services.
- A shared “BFF framework” library that couples all the BFFs together again.
- No timeout on optional calls, so a slow recommendation service blanks the whole order screen.