What is the Backend for Frontend (BFF) pattern?
Topic: microservices, Infrastructure. Level: Intermediate.
Answer
A BFF is a dedicated gateway/backend per client type (web, iOS, Android), instead of one general API. Each BFF aggregates and shapes data specifically for its frontend — trimming fields, batching calls, adapting to the device's constraints — so clients make one tailored call instead of many. Benefits: frontend teams own their BFF, faster iteration, and no bloated shared API. Cost: some duplicated aggregation logic across BFFs.
```java
// mobile-bff: one call returns exactly what the mobile screen needs
@GetMapping("/mobile/home")
HomeDto home(Principal user) {
var profile = userClient.get(user.getName());
var orders = orderClient.recent(user.getName(), 3); // only 3 for small screen
var promos = promoClient.forUser(user.getName());
return HomeDto.compactForMobile(profile, orders, promos); // trimmed payload
}
// web-bff would return richer data for the desktop layout.
```