Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
688095a
feat: Add the implementation of Onion-Architecture (#3532)
HattoriHenzo Aug 16, 2026
f1dcddf
build(deps): bump com.fasterxml.jackson.core:jackson-databind (#3533)
dependabot[bot] Aug 16, 2026
7e0ddb0
feat: Implement Fallback design pattern #2846 (#3538)
priyanshuvishwakarma273403 Aug 16, 2026
401c44b
docs: add priyanshuvishwakarma273403 as a contributor for code (#3568)
allcontributors[bot] Aug 16, 2026
f5bdf68
build(deps): bump com.diffplug.spotless:spotless-maven-plugin (#3540)
dependabot[bot] Aug 16, 2026
4769350
docs: Update factory method Java API links (#3539)
arnabnandy7 Aug 16, 2026
a7000fe
docs: add arnabnandy7 as a contributor for doc (#3569)
allcontributors[bot] Aug 16, 2026
5d6ff2f
feat: add rule engine design pattern (#3548)
MohamedMBG Aug 16, 2026
653bced
docs: add MohamedMBG as a contributor for code (#3570)
allcontributors[bot] Aug 16, 2026
d498332
build(deps): bump ch.qos.logback:logback-core in /subclass-sandbox (#…
dependabot[bot] Aug 16, 2026
c298e94
build(deps): bump ch.qos.logback:logback-core in /polling-publisher (…
dependabot[bot] Aug 16, 2026
1673930
feat: Add Backends For Frontends (BFF) pattern (#300) (#3543)
AnveshSrivastava Aug 16, 2026
2944224
docs: add AnveshSrivastava as a contributor for code (#3571)
allcontributors[bot] Aug 16, 2026
8c7a310
build(deps): bump com.fasterxml.jackson.core:jackson-databind (#3549)
dependabot[bot] Aug 16, 2026
a563344
feat: microservice messaging pattern (#3564)
Mukul-Howale Aug 16, 2026
8b079db
docs: add Mukul-Howale as a contributor for code (#3572)
allcontributors[bot] Aug 16, 2026
22a3412
build(deps): bump com.fasterxml.jackson.core:jackson-databind (#3567)
dependabot[bot] Aug 16, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -3728,6 +3728,51 @@
"contributions": [
"translation"
]
},
{
"login": "priyanshuvishwakarma273403",
"name": "Priyanshu",
"avatar_url": "https://avatars.githubusercontent.com/u/222148451?v=4",
"profile": "https://www.priyanshu-portfolio.co.in",
"contributions": [
"code"
]
},
{
"login": "arnabnandy7",
"name": "Arnab Nandy",
"avatar_url": "https://avatars.githubusercontent.com/u/5053648?v=4",
"profile": "https://github.com/arnabnandy-cognizant",
"contributions": [
"doc"
]
},
{
"login": "MohamedMBG",
"name": "BAGHDAD Mohamed",
"avatar_url": "https://avatars.githubusercontent.com/u/123114744?v=4",
"profile": "https://github.com/MohamedMBG",
"contributions": [
"code"
]
},
{
"login": "AnveshSrivastava",
"name": "Anvesh Srivastava",
"avatar_url": "https://avatars.githubusercontent.com/u/170334970?v=4",
"profile": "https://github.com/AnveshSrivastava",
"contributions": [
"code"
]
},
{
"login": "Mukul-Howale",
"name": "Mukul Howale",
"avatar_url": "https://avatars.githubusercontent.com/u/110479646?v=4",
"profile": "https://github.com/Mukul-Howale",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 6,
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=iluwatar_java-design-patterns&metric=coverage)](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
[![Join the chat at https://gitter.im/iluwatar/java-design-patterns](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-409-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-414-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

<br/>
Expand Down Expand Up @@ -607,6 +607,11 @@ This project is licensed under the terms of the MIT license.
</tr>
<tr>
<td align="center" valign="top" width="16.66%"><a href="https://github.com/SrGiuliano77"><img src="https://avatars.githubusercontent.com/u/128543464?v=4?s=100" width="100px;" alt="Giuliano Oliveira"/><br /><sub><b>Giuliano Oliveira</b></sub></a><br /><a href="#translation-SrGiuliano77" title="Translation">🌍</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://www.priyanshu-portfolio.co.in"><img src="https://avatars.githubusercontent.com/u/222148451?v=4?s=100" width="100px;" alt="Priyanshu"/><br /><sub><b>Priyanshu</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=priyanshuvishwakarma273403" title="Code">💻</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://github.com/arnabnandy-cognizant"><img src="https://avatars.githubusercontent.com/u/5053648?v=4?s=100" width="100px;" alt="Arnab Nandy"/><br /><sub><b>Arnab Nandy</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=arnabnandy7" title="Documentation">📖</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://github.com/MohamedMBG"><img src="https://avatars.githubusercontent.com/u/123114744?v=4?s=100" width="100px;" alt="BAGHDAD Mohamed"/><br /><sub><b>BAGHDAD Mohamed</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=MohamedMBG" title="Code">💻</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://github.com/AnveshSrivastava"><img src="https://avatars.githubusercontent.com/u/170334970?v=4?s=100" width="100px;" alt="Anvesh Srivastava"/><br /><sub><b>Anvesh Srivastava</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=AnveshSrivastava" title="Code">💻</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://github.com/Mukul-Howale"><img src="https://avatars.githubusercontent.com/u/110479646?v=4?s=100" width="100px;" alt="Mukul Howale"/><br /><sub><b>Mukul Howale</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=Mukul-Howale" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
148 changes: 148 additions & 0 deletions backends-for-frontends/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
title: "Backends For Frontends Pattern in Java: Tailoring APIs to Client Needs"
shortTitle: Backends For Frontends
description: "Learn the Backends For Frontends (BFF) design pattern in Java. Understand how to give each client type its own dedicated backend service, with real-world examples, code, and diagrams."
category: Architectural
language: en
tag:
- API design
- Architecture
- Client-server
- Decoupling
- Microservices
---

## Also known as

* Backend For Frontend
* BFF Pattern

## Intent of Backends For Frontends Pattern

Provide each client-side application (mobile, desktop, chatbot, and so on) with its own dedicated
backend service, so every client gets an API shaped exactly for its own needs instead of sharing
one general-purpose backend with every other client.

## Detailed Explanation of Backends For Frontends Pattern with Real-World Examples

Real-world example

> Imagine a retail company whose mobile app, desktop back-office tool, and support chatbot all
> need customer, cart, order and supplier data -- but a phone screen wants a short summary while
> the back-office desktop tool wants full order and stock detail. Rather than exposing one shared
> API that every client has to filter or over-fetch from, the company stands up a small BFF service
> for the mobile clients and a separate BFF service for the intranet clients. Each BFF calls only
> the downstream microservices its client needs and returns a payload shaped for that client.

In plain words

> Give every kind of client its own tailor-made backend, instead of forcing all clients through one
> one-size-fits-all API.

Sam Newman, who popularized the pattern, says

> Create separate backend services to be consumed by specific frontend applications or interfaces.

## Architecture Diagram

```
node mobile{
component iosapp as "ios app"
component androidapp as "android app"
}
node intranet{
component desktop as "desktop app"
component chatbot
}
component bff as "BFF server"{
component iosbff as "ios BFF"
component androidbff as "android BFF"
component chatbotbff as "chatbot BFF"
component desktopbff as "desktop BFF"
}
node intranetserv as "intranet services server"{
component ss as "supplier service API"
}
cloud onlypublic as "public cloud"{
component cas as "customer authentication service API"
component cs as "cart service API"
}
cloud cloudserv as "managed cloud"{
component os as "order service API"
}
iosapp -- iosbff
androidapp -- androidbff
chatbot -- chatbotbff
desktop -- desktopbff
iosbff -- cas
androidbff -- cas
iosbff -- cs
androidbff -- cs
iosbff -- os
androidbff -- os
chatbotbff -- os
desktopbff -- os
chatbotbff -- ss
desktopbff -- ss
```

This example implements a simplified version of the diagram above with two client-facing BFFs
instead of four, to keep the demo focused: a **Mobile BFF** standing in for the ios/android BFFs,
and a **Desktop BFF** standing in for the desktop/chatbot BFFs. Both call into the same shared
downstream services (`AuthService`, `OrderService`), while `CartService` is only used by the
Mobile BFF and `SupplierService` is only reachable from the Desktop BFF, matching the fan-out
shown in the diagram.

## Class Diagram

![Backends For Frontends class diagram](./etc/backends-for-frontends.png)

## When to Use the Backends For Frontends Pattern in Java

* Different client types (mobile, web, desktop, voice/chat) need meaningfully different shapes,
granularity, or aggregation of the same underlying data.
* A single shared API has grown a large number of client-specific conditional branches, optional
fields, or query parameters to accommodate every consumer.
* Different client teams need to iterate on their own API independently without coordinating
changes through one shared backend team.
* Some clients (e.g. mobile) need aggressively trimmed payloads for bandwidth/latency reasons,
while others (e.g. an internal desktop tool) need much richer data.

## Benefits and Trade-offs of Backends For Frontends Pattern

Benefits:

* Each client gets an API optimized for its own needs, improving performance and simplicity on
the client side.
* Client teams can evolve their BFF independently, reducing cross-team coordination.
* Downstream microservices stay generic and reusable; client-specific logic lives in the BFF
layer instead of leaking into shared services.

Trade-offs:

* Introduces additional services to build, deploy, and operate.
* Logic that is genuinely shared across clients can end up duplicated across BFFs if not
carefully factored out.
* Adds an extra network hop between the client and the downstream services.

## How to Implement Backends For Frontends Pattern in Java

1. Identify the distinct client types that need meaningfully different data shapes.
2. Define the downstream services each client's data actually depends on (`AuthService`,
`CartService`, `OrderService`, `SupplierService` in this example).
3. Create one BFF per client type, implementing a shared `ClientBff<T>` contract, where each BFF
only depends on the downstream services its client needs.
4. Have each BFF aggregate and reshape the downstream data into a response DTO tailored to its
client (`MobileDashboardResponse`, `DesktopDashboardResponse`).
5. Wire the client applications to call their own BFF rather than the downstream services
directly.

## Source Code

* [Pattern: Backends For Frontends](https://samnewman.io/patterns/architectural/bff/) by Sam Newman
* [Microservices Patterns: With examples in Java](https://www.amazon.com/Microservices-Patterns-examples-Chris-Richardson/dp/1617294543) by Chris Richardson

## References and Credits

* [Building Microservices](https://www.oreilly.com/library/view/building-microservices-2nd/9781492034018/) by Sam Newman
* [Pattern: Backend for frontend (microservices.io)](https://microservices.io/patterns/apigateway.html)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 130 additions & 0 deletions backends-for-frontends/etc/backends-for-frontends.urm.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
@startuml
package com.iluwatar.bff {
class App {
- LOGGER : Logger {static}
- USER_ID : String {static}
- PRODUCT_ID : String {static}
- DEMO_PRICE_USD : double {static}
- DEMO_STOCK_LEVEL : int {static}
+ App()
+ main(args : String[]) {static}
}
}
package com.iluwatar.bff.bff {
interface ClientBff<T> {
+ getDashboard(userId : String) : T {abstract}
}
class DesktopBff {
- authService : AuthService
- orderService : OrderService
- supplierService : SupplierService
+ DesktopBff(auth : AuthService, orders : OrderService, suppliers : SupplierService)
+ getDashboard(userId : String) : DesktopDashboardResponse
}
class MobileBff {
- MAX_RECENT_ORDERS : int {static}
- authService : AuthService
- cartService : CartService
- orderService : OrderService
+ MobileBff(auth : AuthService, cart : CartService, orders : OrderService)
+ getDashboard(userId : String) : MobileDashboardResponse
}
}
package com.iluwatar.bff.dto {
class DesktopDashboardResponse {
- greeting : String
- loyaltyTier : String
- orderStatuses : List<String>
- supplierStockSummaries : List<String>
+ DesktopDashboardResponse(greeting : String, loyaltyTier : String, orderStatuses : List<String>, supplierStockSummaries : List<String>)
}
class MobileDashboardResponse {
- greeting : String
- cartItemCount : int
- cartTotalUsd : double
- recentOrderSummaries : List<String>
+ MobileDashboardResponse(greeting : String, cartItemCount : int, cartTotalUsd : double, recentOrderSummaries : List<String>)
}
}
package com.iluwatar.bff.model {
class CartItem {
- product : Product
- quantity : int
+ CartItem(product : Product, quantity : int)
+ lineTotal() : double
}
class Order {
- id : String
- productName : String
- status : String
+ Order(id : String, productName : String, status : String)
}
class Product {
- id : String
- name : String
- priceUsd : double
+ Product(id : String, name : String, priceUsd : double)
}
class SupplierRecord {
- productId : String
- supplierName : String
- stockLevel : int
+ SupplierRecord(productId : String, supplierName : String, stockLevel : int)
}
class User {
- id : String
- displayName : String
- loyaltyTier : String
+ User(id : String, displayName : String, loyaltyTier : String)
}
}
package com.iluwatar.bff.service {
interface AuthService {
+ getUser(userId : String) : User {abstract}
}
interface CartService {
+ getCart(userId : String) : List<CartItem> {abstract}
}
interface OrderService {
+ getOrders(userId : String) : List<Order> {abstract}
}
interface SupplierService {
+ getSupplierRecords(productId : String) : List<SupplierRecord> {abstract}
}
}
package com.iluwatar.bff.service.impl {
class InMemoryAuthService {
- users : Map<String, User>
+ InMemoryAuthService(userData : Map<String, User>)
+ getUser(userId : String) : User
}
class InMemoryCartService {
- cartsByUserId : Map<String, List<CartItem>>
+ InMemoryCartService(carts : Map<String, List<CartItem>>)
+ getCart(userId : String) : List<CartItem>
}
class InMemoryOrderService {
- ordersByUserId : Map<String, List<Order>>
+ InMemoryOrderService(orders : Map<String, List<Order>>)
+ getOrders(userId : String) : List<Order>
}
class InMemorySupplierService {
- recordsByProductId : Map<String, List<SupplierRecord>>
+ InMemorySupplierService(records : Map<String, List<SupplierRecord>>)
+ getSupplierRecords(productId : String) : List<SupplierRecord>
}
}
DesktopBff ..|> ClientBff
MobileBff ..|> ClientBff
DesktopBff --> "-authService" AuthService
DesktopBff --> "-orderService" OrderService
DesktopBff --> "-supplierService" SupplierService
MobileBff --> "-authService" AuthService
MobileBff --> "-cartService" CartService
MobileBff --> "-orderService" OrderService
InMemoryAuthService ..|> AuthService
InMemoryCartService ..|> CartService
InMemoryOrderService ..|> OrderService
InMemorySupplierService ..|> SupplierService
CartItem --> "-product" Product
@enduml
Loading
Loading