Microservice Patterns
Backend for Frontend (BFF)
Give each client experience — web, mobile, partners — its own thin backend that shapes data for that screen, so clients make one call instead of many over slow networks.
What you will learn
- See why round trips over mobile networks dominate latency.
- Design a BFF that aggregates and trims data for one client type.
- Decide who owns a BFF and what logic it may contain.
- Implement parallel aggregation with Spring WebFlux.
Before you start
Overview
A restaurant has a dine-in menu and a delivery menu. Same kitchen, but the delivery menu leaves out dishes that travel badly and bundles combos so you order once. A BFF is a delivery menu for a specific app: the same backend services, packaged for what that screen needs.
A Backend for Frontend is a small service owned by a client team (web, iOS/Android, partner API). It calls the domain services, aggregates and trims their data, and exposes exactly the endpoints its client needs — one call per screen instead of one call per service.
The problem: one API for every client
- Chatty clients: a mobile home screen needs users, orders and catalog data — three round trips over a network with 100–300 ms latency each.
- Over-fetching: general endpoints return everything every client might need; phones download and parse fields they never show.
- Conflicting needs: web wants rich data, mobile wants small payloads, partners want stability — one API team becomes a bottleneck.
Run it
Both scenarios use the same services. Only the number of slow mobile round trips changes. Raise the mobile network latency to 250 ms and watch the gap grow.
Implementation with Spring WebFlux
@RestController
class MobileHomeController {
private final WebClient users, orders, catalog;
MobileHomeController(WebClient.Builder builder) {
this.users = builder.baseUrl("http://users").build();
this.orders = builder.baseUrl("http://orders").build();
this.catalog = builder.baseUrl("http://catalog").build();
}
@GetMapping("/mobile/home")
Mono<HomeScreen> home(@AuthenticationPrincipal Jwt jwt) {
String userId = jwt.getSubject();
Mono<UserCard> user = users.get().uri("/users/{id}/card", userId).retrieve().bodyToMono(UserCard.class);
Mono<List<OrderSummary>> recent = orders.get().uri("/orders?customer={id}&limit=3", userId)
.retrieve().bodyToFlux(OrderSummary.class).collectList()
.timeout(Duration.ofMillis(300)).onErrorReturn(List.of()); // optional section
Mono<List<ProductTile>> featured = catalog.get().uri("/featured?size=6").retrieve()
.bodyToFlux(ProductTile.class).collectList();
// The three calls run in parallel inside the data centre.
return Mono.zip(user, recent, featured).map(t -> new HomeScreen(t.getT1(), t.getT2(), t.getT3()));
}
}Rules of thumb
- One BFF per user experience, owned by the team that builds that client.
- Presentation logic only: aggregate, trim, rename, paginate. Business rules belong in domain services.
- Protect each downstream call with timeouts and decide which sections are optional (fallbacks).
- Share cross-cutting edge concerns (TLS, auth, rate limits) in an API Gateway in front of the BFFs.
Trade-offs
| Benefits | Costs | |
|---|---|---|
| Performance | One round trip and a small payload per screen. | An extra hop inside the data centre. |
| Team autonomy | Client teams change their API without waiting for others. | More services to deploy and run; duplicated code across BFFs. |
| Design | APIs shaped by real screens. | Business logic tends to leak into BFFs if nobody watches. |
Production considerations
- Measure per-screen latency from real devices, not just server time.
- Version mobile BFF endpoints: old app versions stay in the wild for months.
- GraphQL can replace several BFF endpoints, but the ownership and logic rules still apply.