Course contents

11. Microservices Lesson 5 of 10

Backend for Frontend

Give each client type its own backend: shaped payloads, one composed call instead of many, independent releases and failure isolation, and the cost of duplication.

13 minIntermediate9 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • A shared API forces one response shape and one release queue on clients with different needs.
  • A BFF is a backend per client experience, owned by the team that builds that client.
  • It composes calls in parallel near the services and returns exactly what its screens render.
  • Separate BFFs are separate failure and release domains; a BFF can also degrade a screen gracefully.
  • Keep BFFs thin: shared business rules belong in services, or clients drift apart.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

A Backend for Frontend (BFF) is a server-side component built for one kind of client experience, such as the web app, the mobile app or a partner integration, and usually owned by the team that builds that client. It sits between the client and the general-purpose services, composes the calls a screen needs and returns data in exactly the shape that client renders.

Why it exists

Different clients want different things. A desktop page shows rich detail on a fast connection; a phone shows a compact view over a slow, high-latency link; a smart TV or partner wants something else again. One general-purpose API has to pick a compromise: either large responses that over-serve small clients, or fine-grained endpoints that make them chatty. Worse, every client change becomes a request to the team that owns the shared API, and different client teams ask for conflicting things. A BFF lets each client team build the API its client needs, at its own pace.

How it works

One BFF per experience. Typically one for web and one for mobile (iOS and Android usually share one, because their screens and needs match). Each sits behind the edge gateway, which still handles TLS, authentication and rate limiting; in small setups the BFF may be the gateway route for its client.

Composition. For a screen request the BFF calls the services it needs, in parallel where possible, over the fast internal network, and combines the results. The client makes one round trip instead of several over its slow link.

Shaping. The BFF trims fields, picks image sizes, formats values for display, chooses page sizes, translates errors into what the client can show, and may speak a different protocol to the client (REST or GraphQL) than to the services (often gRPC).

Graceful degradation. Because the BFF knows what its screens need, it can treat some calls as optional: if recommendations time out, return the rest of the screen without them, with a short timeout on optional calls.

Ownership and release. The client team owns the BFF and ships it together with client changes. Mobile releases add one constraint: old app versions stay installed for months, so the mobile BFF must keep supporting the versions still in use.

When to use it

  • Clients with clearly different screens, payload needs or network conditions.
  • Separate teams per client, blocked by a shared API team.
  • Mobile clients that would otherwise make several sequential calls per screen.
  • Third-party or partner APIs that need a stable, different contract from your own apps.

When not to

  • One team builds every client, or the clients need nearly the same data: one well-designed API is simpler.
  • When a flexible query layer (GraphQL with a good schema) already lets clients ask for exactly what they need, a BFF may be redundant, or the GraphQL server effectively is a shared BFF.
  • Never as a place for core business rules.

Trade-offs

Choice You gain You pay
BFF per client type Tailored payloads, fewer round trips, team autonomy More services to run, monitor and secure
Composition in the BFF One client round trip; parallel internal calls BFF latency is the slowest required dependency
Partial responses Screens survive optional failures Clients must handle missing sections
Logic in the BFF Fast client-specific changes Duplication and drift between BFFs if the logic is shared
GraphQL instead of BFFs Clients choose fields themselves Query cost control, caching and schema governance

Alternatives

  • One general-purpose API (behind an API gateway): simplest when clients are similar.
  • Gateway aggregation: composition in a shared gateway; fine for a few routes, but it couples client teams to one component.
  • GraphQL or federation: one schema, clients select fields; resolvers do the composition.
  • Server-driven UI: the backend sends screen layouts, so new screens need less client release work; a heavier commitment.

Failure modes

  • Duplicated business logic: the same rule implemented in two BFFs drifts apart and clients show different results.
  • BFF sprawl: one BFF per device or per screen; the number of services grows faster than the teams that can own them.
  • Fan-out latency: a screen waits for its slowest dependency; without timeouts on optional calls, one slow service slows every screen.
  • Hidden coupling: BFFs that call each other, or that depend on internal service details, break when services change.
  • Old mobile versions: removing a BFF field breaks installed apps that still use it.

In production

Keep BFFs thin and stateless; scale them horizontally like any service. Give each call a timeout and decide per screen which calls are required and which are optional. Propagate trace context through the BFF so a slow screen can be traced to the service behind it. Track app versions in requests and keep old contracts until usage drops. Put shared rules in the owning services, and review BFFs for logic that has crept in.

Common misconceptions

  • "A BFF replaces the API gateway." Usually it sits behind one; edge concerns stay shared.
  • "One BFF per device." One per distinct experience; similar clients share one.
  • "BFFs make the system simpler." They make clients simpler and teams faster, at the cost of more services.
  • "Aggregation is always faster." Only when it removes round trips on a slow link; inside a fast network an extra hop just adds latency.

Interview angle

Bring up BFFs when a design has very different clients (mobile and web, or public partners) and the question is how to serve them: explain composition, payload shaping, team ownership and failure isolation, and place the BFFs behind the edge gateway. Expect probing on duplication (keep rules in services), latency (parallel calls, timeouts, partial responses), and when a single API or GraphQL would be enough.

Model assumptions

Most of this lesson is a scripted illustration: requests and responses are scripted packets, not simulated traffic, because the engine does not model payload sizes. A response's travel time includes an illustrative transfer time: the web app is 20 ms away on broadband; the mobile app is 60 ms away on a link of roughly 1.5 Mbit/s, so 48 KB adds about 256 ms and 9 KB about 48 ms. Services answer in about 15 ms; internal hops take 2 ms. The isolation and partial-response steps and the sandbox run simulated traffic (Poisson arrivals, 100 web and 150 mobile requests per second); there each BFF calls the three services in parallel and answers when all three have succeeded. In the partial-response step the Mobile BFF answers as soon as any 2 of its 3 calls have succeeded: the model counts answers rather than naming Catalog as the optional call, and it has no per-call timeouts. Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. One API for every client

    The web app and the mobile app call the same general-purpose API. It returns one response shape designed around the desktop page: full descriptions, every field, large image URLs. About 48 KB for the home screen.

    On broadband the web app barely notices. The mobile app, on a cellular link, spends about a quarter of a second just downloading fields it never shows: it uses roughly 9 KB of the 48. *(Scripted illustration; sizes and link speeds are illustrative.)*

    Scene: The web app and the mobile app request the home screen from one general-purpose API. Both receive the same 48 KB response: quick on broadband, slow on the cellular link, and mostly unused by the mobile app.

    Key idea: One response shape for all clients over-serves small screens and slow links.

  2. 2. Fine-grained endpoints, many round trips

    The API team’s answer to "too much data" is fine-grained endpoints: /me, /orders/recent, /products/featured. Now the mobile home screen needs three calls, one after another, each a full round trip over the 60 ms link: about 600 ms in total. The web app makes the same three calls in about 130 ms.

    Every screen change becomes a change request to the API team, and web and mobile ask for opposite things. The shared API turns into a compromise that suits neither client, and a queue of tickets. *(Scripted illustration.)*

    Scene: The mobile app makes three calls one after another to build its home screen, each a slow round trip; the web app makes the same calls much faster on broadband.

    Key idea: A shared API forces a compromise between clients with different needs, and makes every client change wait for another team.

  3. 3. A backend for each frontend

    Each client experience gets its own backend: a Web BFF and a Mobile BFF, each owned by the team that builds that client. The mobile app makes one call to its BFF. The BFF calls Users, Orders and Catalog in parallel over the fast internal network and returns exactly what the mobile screen needs.

    The mobile home screen now loads in about 190 ms instead of 600. The services stay general; the client-specific work moves into a backend the client team controls. *(Scripted illustration.)*

    Scene: A Web BFF and a Mobile BFF replace the general-purpose API. The mobile app makes one call; its BFF queries the three services in parallel and returns one response in about 190 ms.

    Key idea: A BFF is a backend owned by a client team that composes the services into exactly what that client needs.

  4. 4. Each BFF shapes its own payload

    Both apps load their home screen at the same moment. The Web BFF returns a rich response for the desktop layout: full descriptions, large images, recommendations. The Mobile BFF returns a trimmed one: short titles, thumbnail URLs sized for the phone, prices already formatted. About 9 KB.

    On the cellular link the trimmed response arrives in about a fifth of a second less than the shared 48 KB one. Shaping also includes translating errors, choosing pagination sizes and protocols (for example, a BFF that speaks GraphQL to its client and gRPC to the services). *(Scripted illustration.)*

    Scene: The web app and the mobile app load their home screens at the same time. The Web BFF returns a rich response; the Mobile BFF returns a much smaller trimmed one, which reaches the phone far sooner than the shared response did.

    Key idea: Each BFF returns the shape its client renders: rich for desktop, lean for mobile.

  5. 5. Ship without asking the other team

    The mobile team adds a "reorder" card to the home screen. They change the Mobile BFF and the app together and ship both. The Web BFF, the web app and the services are untouched; nobody files a ticket with another team.

    That is the organisational point of the pattern: the client team owns the whole slice from screen to BFF, so client changes move at the client team’s pace. *(Scripted illustration.)*

    Scene: The mobile team deploys Mobile BFF v2 with a new home-screen card. The web app keeps calling the unchanged Web BFF.

    Key idea: A BFF lets a client team change its screen and its backend together, without coordinating with other clients.

  6. 6. A broken BFF only breaks its own client

    Now with real traffic: 100 requests per second from the web app, 150 from the mobile app. Each BFF calls the three services in parallel for every request, as in the scripted steps.

    At 1 s a bad release crashes the Mobile BFF. Every mobile request fails, but web requests keep succeeding: the web app never depended on the mobile backend. With one shared API, the same bad release would have taken down both clients.

    Scene: Web and mobile traffic flow through their own BFFs to the shared services. The Mobile BFF crashes at 1 second: all mobile requests fail while web requests keep succeeding.

    Key idea: Separate BFFs are separate failure domains: a bad mobile release does not take the web down.

  7. 7. Degrade the screen, not the request

    Catalog is down, with the same real traffic as before. The Web BFF composes naively: it needs all three answers, so every web request fails because one of three calls failed. The Mobile BFF knows what its screen can live without: it answers as soon as 2 of its 3 calls have succeeded and leaves out featured products, so every mobile user still sees a working screen with their profile and recent orders.

    That decision belongs in the BFF because it is a product decision for one client: the web page might hide a different section. *(In the simulation the Mobile BFF counts answers, “any 2 of 3”; a real one would name Catalog as the optional call and give it a short timeout.)*

    Scene: Catalog is offline while web and mobile traffic keep flowing. The Web BFF needs all three answers, so every web request fails. The Mobile BFF answers once Users and Orders have succeeded, so every mobile request still gets the profile and recent orders.

    Key idea: A BFF can return a partial response when an optional dependency fails, because it knows what its screen needs.

  8. 8. The cost: logic in two places

    Both BFFs show order totals with a loyalty discount. Each team implemented the rule in its own BFF, and after a policy change only one was updated: web and mobile show different totals for the same order.

    Rules that must agree belong in the services (here, Orders computes the discounted total once), and BFFs stay thin: composition, shaping and client-specific behaviour only. Also avoid a BFF per device variant: iOS and Android usually share one mobile BFF. *(Scripted illustration.)*

    Scene: Both BFFs fetch the same order from Orders but apply their own copy of the discount rule, so web and mobile show different totals. The rule belongs in Orders.

    Key idea: Keep BFFs thin: shared business rules live in services, or the clients will drift apart.

  9. 9. When a BFF is worth it

    Use BFFs when client experiences differ a lot (screens, payloads, network, release cadence) and separate teams own them. Each BFF composes calls, shapes payloads, degrades gracefully and ships with its client. It sits behind (or is part of) the edge gateway, which still handles TLS, authentication and rate limiting.

    Skip it when clients need nearly the same data, or one team builds every client: a single well-designed API (or GraphQL) is simpler. Keep business rules in the services, and turn the simulated traffic back on in the sandbox to try failures yourself.

    Scene: Web and mobile apps each call their own BFF, and both BFFs call the shared Users, Orders and Catalog services. Simulated traffic flows and everything is healthy.

    Key idea: One BFF per distinct client experience, owned by its team, thin, behind a shared gateway.