Topic 148 / Advanced
Backend for frontend (BFF)
Chapter 9.3 · Communication Topologies
What and why
Different client types — a mobile app, a web app, a partner integration — often need different shapes of the same data, and a shared general-purpose API ends up either over-fetching for one client or growing client-specific parameters that leak client concerns into domain services. A Backend for Frontend is a thin aggregation layer, one per client type, that calls the domain services (→ 147) and shapes the response for that one client — keeping client-specific logic out of the services themselves. Builds on: → 147 (inter-service communication).
Diagram
Mobile app ──► [Mobile BFF] ──┐
├──► Catalog Service
Web app ──► [Web BFF] ───┤──► Inventory Service
└──► Pricing Service
Each BFF composes the same backend services differently:
Mobile BFF: small payload, fewer fields, pre-aggregated for one screen
Web BFF: richer payload, supports client-side filtering/sorting
How it works
- Identify client types whose needs genuinely diverge — payload size, call patterns, auth model.
- Give each client type its own BFF, ideally owned by the team that owns that client, not by the domain teams.
- The BFF composes calls to domain services (→ 147) — often several in parallel — and returns one client-shaped response, instead of the client making several calls itself.
- Keep business logic out of the BFF: it aggregates and reshapes; it does not own data or implement domain rules — those stay in the domain services, so two BFFs never implement the same rule twice.
- Version and evolve each BFF independently of the domain services and of the other BFFs — that independence is the entire point.
Practical example
(illustrative). A product page needs price, stock, and three recommended items. The web BFF calls all three services in parallel and returns one JSON payload with every field the rich web UI uses. The mobile BFF calls the same three services but drops fields the mobile UI never renders and compresses the recommended items to just IDs and thumbnails — same backend calls, a payload roughly a third the size for a client on a metered connection.
When to use it
- Use when: two or more client types have genuinely different data or interaction needs, and a shared API is growing client-specific conditionals to serve both.
- Avoid when: there is one client type — a plain API (→ 31) in front of the domain services is simpler and needs no client-specific owned layer.
- Failure scenarios: a BFF that accumulates business logic becomes a second home for rules that belong in the domain service, duplicating them when a second BFF needs the same rule; a BFF that calls domain services sequentially instead of in parallel adds up every hop's latency for no reason.
- Simpler alternative: one shared API with optional fields or a sparse-fieldset query parameter (→ 37) often serves two client types well enough without a new owned service.
Practice
(Pattern kind — implement minimal; 20 min, two small processes). Build two
tiny BFFs (two processes or two endpoints) in front of one fake "product" domain service: a
"web" BFF that returns the full record, and a "mobile" BFF that returns only id, name,
price. Measure the payload size difference on the same underlying call.
Done when the mobile BFF's payload is measurably smaller and no business logic (e.g., price calculation) exists in either BFF — only shaping.
Key takeaway
A BFF is a per-client aggregation layer that keeps client-shaping concerns out of domain services — it composes, it does not decide business rules. Done when you can: justify when a second BFF is worth owning, versus a shared API with optional fields. Used next in → 149 (sidecar — the next layer of cross-cutting concerns).
References
A pattern language for microservices — see references.md.
Source directory