How to Implement a Backend for Frontend
Add a BFF per client: shape the response for one client, aggregate the services, keep the client simple, and own the client-specific logic.
On this page
Introduction
A general API serves all the clients, so it tends to be either a compromise or a superset: it returns more than any client needs, and each client does the shaping and the aggregation. A backend for frontend changes that: each client type gets its own backend, which aggregates the services and returns exactly the shape that client needs, in one request. The client stays simple, and the client-specific logic lives in the BFF.
This tutorial implements a BFF: one per client type, aggregating and shaping the downstream responses, owning the client-specific logic, and tested for the shape and the aggregation. The examples use an HTTP aggregation, and the pattern applies to any client with different needs.
Give each client type its own backend
Create a backend per client type, such as one for the web and one for the mobile app, each owned by the team that builds that client. The web BFF returns the shape the web needs, and the mobile BFF returns the lighter shape the mobile needs, so neither does the shaping and the aggregation itself. The client’s team owns the BFF, so the client-specific logic is theirs to shape.
The services stay general and serve the BFFs. They do not know the client shapes; they expose the domain resources, and the BFFs combine them. This keeps the services stable as the clients evolve, because a client change is a BFF change, not a service change, and the service does not accumulate client-specific endpoints.
Aggregate and shape in one request
The BFF calls the downstream services, combines their responses, and returns the shape the client needs, so the client makes one request instead of several. A screen that needs the user, the orders, and the recommendations is one BFF call that fans out to three services and merges the results. This removes the round trips and the over-fetching from the client, which is the point.
The fan-out is parallel where the calls are independent, and the BFF handles a partial failure: if the recommendations are down, it returns the user and the orders and a default for the recommendations, rather than failing the whole screen. The BFF is the place for the graceful degradation of a screen, because it knows what the client can do without.
async function getDashboard(userId: string) {
const [user, orders, recs] = await Promise.all([
userService.get(userId),
orderService.recent(userId),
recommendationService.for(userId).catch(() => []), // degrade gracefully
]);
return { // the client's shape, in one response
name: user.name,
recentOrders: orders.map((o) => ({ id: o.id, total: o.total, date: o.date })),
recommendations: recs.slice(0, 5),
};
}
Own the client-specific logic
The BFF holds the logic specific to one client: the formatting, the aggregation, the field selection, and the screen-level rules. This is what keeps the client simple and the services general. The client does not compute a derived field or merge two responses; the BFF does, in the shape the client wants.
Keep the domain logic in the services, and put only the client-specific shaping in the BFF. A BFF that reimplements the order rules duplicates the domain and drifts from the service. The boundary is that the service owns the domain invariants and the truth, and the BFF owns the presentation and the aggregation for one client. The BFF calls the service for the domain decisions, not reimplements them.
Manage the duplication across BFFs
Two BFFs for two clients share some logic: the same service calls, the same aggregation, the same error handling. Duplicating it in each BFF drifts and doubles the work. Share the common part as a library between the BFFs, or a shared client for the services, so each BFF holds only its client-specific shaping. The duplication that remains is the genuinely client-specific part.
Do not merge the BFFs back into one to remove the duplication, because that recreates the general API the BFF was meant to replace. Keep the BFFs separate for the independence, and share the code via a library. The team owns its BFF and shapes its client’s API, while the shared library holds the common service access.
Test the shape, the aggregation, and the partial failure
Test that the BFF returns the shape the client expects, by asserting the response’s fields and structure. Test that it aggregates the downstream services, by a stub that records which services were called and asserting the response combines them. Test the partial failure, by failing one service and asserting the screen still returns with a default for the failed part.
# the BFF returns the client shape and aggregates the services in one call
curl -s http://bff-web/dashboard/1 | jq 'keys' # [name, recentOrders, recommendations]
# fail the recommendations service: the dashboard still returns with a default
kill recommendations-service
curl -s http://bff-web/dashboard/1 | jq '.recommendations' # [] not a 500
Common pitfalls
Merging the BFFs into one general API, reimplementing the domain logic in the BFF instead of calling the service, a serial fan-out that makes the request slow, failing the whole screen when one downstream is down, and not sharing the common code so the BFFs drift are the recurring mistakes. A backend per client, the domain in the service, a parallel fan-out, a graceful partial failure, and a shared library address them.
Conclusion
A BFF shapes the API for one client. Give each client type its own backend owned by the client’s team, aggregate and shape the services in one request with a parallel fan-out and a graceful partial failure, keep the domain logic in the services, and share the common code across the BFFs. Self-check by asserting the client shape and the aggregation, and by failing one service and asserting the screen still returns. Then the client is simple and the services stay general.
Sources: Sam Newman: Backend for Frontend, Microsoft: Backend for Frontend pattern, SoundCloud: BFF (the original post).
FAQ
- How is a BFF different from an API gateway?
- A gateway is generic: it routes and applies cross-cutting policy to any client. A BFF is per-client: it shapes the response, aggregates the services, and holds the logic for one client type. A gateway is shared; a BFF is owned by the client's team.
- Does a BFF duplicate logic across clients?
- It can, if each BFF repeats the same aggregation. Keep the shared domain logic in the services, and let the BFFs hold only the client-specific shaping and aggregation. Where the shaping is common, share a library between the BFFs, so the duplication is the client-specific part only.
Report a problem with this articleOpens a pre-filled issue on GitHub.