BFF(Backend for Frontend) 패턴은 각 프론트엔드(웹, 모바일, IoT)마다 전용 백엔드 레이어를 두는 아키텍처 패턴이다. 다양한 클라이언트의 상이한 요구사항을 처리하고, 여러 마이크로서비스를 클라이언트에 맞게 집계한다.
아키텍처
모바일 앱 → Mobile BFF ──┐
웹 앱 → Web BFF ──┤→ User Service
TV 앱 → TV BFF ──┘ Product Service
Order Service
문제: 없을 때
범용 API만 있을 때:
1. 모바일: 작은 화면 → 필드 5개만 필요 → 30개 필드 수신 (over-fetching)
2. TV: 큰 화면 → 많은 데이터 필요 → 여러 번 API 호출 (under-fetching)
3. 웹: SEO 필요 → 서버사이드 렌더링 지원 필요
→ 하나의 API로 모두 만족시키기 어려움
BFF 구현 (Node.js)
typescript
// web-bff/src/routes/product.ts
@Controller('products')
export class ProductController {
constructor(
private productService: ProductService,
private reviewService: ReviewService,
private inventoryService: InventoryService,
) {}
// 웹 전용: 풍부한 데이터 집계
@Get(':id/detail')
async getProductDetail(@Param('id') id: string) {
const [product, reviews, inventory] = await Promise.all([
this.productService.getProduct(id),
this.reviewService.getTopReviews(id, 5),
this.inventoryService.getStock(id),
]);
// 웹 클라이언트에 최적화된 응답 형태
return {
...product,
reviews,
inStock: inventory.count > 0,
seoMeta: { title: product.name, description: product.description },
};
}
}
graphql
# BFF에서 여러 서비스를 하나의 쿼리로 통합
query ProductPage($id: ID!) {
product(id: $id) {
id
name
price
reviews(limit: 5) {
rating
comment
}
inventory {
inStock
}
}
}
관련 개념