The Backend for Frontend (BFF) pattern, introduced by Phil Calçado at SoundCloud, is an architectural approach where each client application (web, mobile, desktop) has its own dedicated backend. This pattern addresses the fundamental challenge of serving multiple client types from a single general-purpose API.
The Problem
A single general-purpose API tries to serve all clients equally. This leads to several problems:
Developers end up building workarounds: adding query parameters for field selection, creating composite endpoints, or making multiple round trips. The BFF pattern solves this by providing a dedicated backend for each client.
How BFF Works
Each client type has its own backend service:
[Mobile App] <-> [Mobile BFF] <-> [Shared Services]
|
[Web App] <-> [Web BFF] <-> [Shared Services]
|
[IoT Device] <-> [IoT BFF] <-> [Shared Services]
Each BFF:
Benefits
**Optimized client experience.** Each BFF returns exactly the data the client needs, in the format it prefers. Mobile APIs minimize data transfer. Web APIs support rich interactions. No more over-fetching or under-fetching.
**Simplified client code.** Complex logic moves from the client to the BFF. Data aggregation, transformation, and filtering happen on the server. The client becomes simpler and more focused on presentation.
**Independent evolution.** The web BFF can evolve independently from the mobile BFF. The web team can add endpoints for new browser features without affecting mobile users.
**Security isolation.** Each BFF can implement client-specific security policies. Mobile authentication might use device tokens. Web authentication might use cookie-based sessions.
**Team autonomy.** The web team owns the web BFF. The mobile team owns the mobile BFF. Teams can deploy independently without coordinating API changes.
Real-World Example
Consider an e-commerce platform. A general-purpose API might return:
{
"product": {
"id": "123",
"name": "Wireless Mouse",
"description": "...",
"specifications": { ... },
"reviews": [ ... ],
"related_products": [ ... ],
"inventory": { ... },
"shipping_options": [ ... ],
"price_history": [ ... ]
}
}
The **Mobile BFF** tailors this for a phone:
{
"id": "123",
"name": "Wireless Mouse",
"price": "$29.99",
"image_url": "thumb.jpg",
"in_stock": true
}
The **Web BFF** provides richer data for a desktop browser:
{
"product": {
"id": "123",
"name": "Wireless Mouse",
"price": "$29.99",
"description": "...",
"images": ["img1.jpg", "img2.jpg"],
"reviews": {
"summary": { "average": 4.5, "count": 128 },
"top_reviews": [ ... ]
}
}
}
Each BFF is between 30-40 lines of server-side code, orchestrating calls to downstream services and shaping the response for its specific client.
BFF vs. API Gateway
BFF and API Gateway are complementary patterns, not alternatives:
In practice, a BFF often sits behind an API Gateway. The gateway handles shared infrastructure, and the BFF handles client-specific logic.
When to Use BFF
Use BFF when:
Do not overuse BFF when:
Potential Drawbacks
**Increased operational complexity.** More services to deploy, monitor, and maintain. Each BFF adds infrastructure overhead.
**Code duplication.** Common logic may be duplicated across BFFs. Be disciplined about extracting shared services.
**Larger team responsibility.** The team must maintain both the client application and its BFF.
Summary
The BFF pattern provides dedicated backends for each client application, optimizing data delivery and simplifying client code. Use BFFs when you have multiple distinct clients with different needs. Combine with an API Gateway for cross-cutting concerns. Avoid premature adoption -- for single-client applications, a well-designed general-purpose API is sufficient.