Modern applications often support multiple types of clients, such as web applications, mobile apps, tablets, and even smart TVs.
Although these clients may use the same backend services, they often have different requirements.
For example, consider an e-commerce platform that supports both a web application and a mobile application.
The web application might display detailed product information, customer reviews, recommendations, and related products on a single page.
The mobile application, however, might show only the product name, price, image, rating, and availability to keep the interface simple and minimize network usage.
If both clients use the same general-purpose backend APIs, they may need to make multiple requests or receive significantly more data than they actually need.
This is where the Backend-for-Frontend (BFF) pattern becomes useful.
A BFF is a backend layer designed specifically for the needs of a particular frontend application or group of clients.
It sits between the frontend and the underlying backend services, providing APIs optimized for that frontend's requirements.
Backend-for-Frontend is an architectural pattern in which different frontend applications have dedicated backend layers.
Instead of forcing every client to interact directly with the same collection of backend services, each frontend communicates with a backend designed around its particular needs.
For example, an e-commerce platform might have:
Each BFF communicates with the underlying backend services but exposes APIs tailored to its consumers.
Each frontend has a dedicated BFF while sharing underlying business services.
The important idea is that BFFs are organized around frontend needs, while backend services are organized around business capabilities.
For example, the Product Service remains responsible for product information, regardless of whether the request comes from the web or mobile application.
The BFF decides how that information should be collected, transformed, and presented to its frontend.
A BFF primarily addresses two common problems: over-fetching data and excessive client-to-server requests.
Over-fetching happens when an API returns more data than the client needs.
Suppose a product API returns:
A desktop application might use most of this information.
However, a mobile product listing might need only the product ID, name, price, and rating.
Returning the full response wastes bandwidth and increases the amount of data the mobile application must process.
A Mobile BFF can return a smaller response:
This gives the client exactly what it needs without requiring the underlying Product Service to support every frontend-specific response format.
A frontend often needs information from multiple backend services to display a single screen.
For example, an order details page might require:
Without a BFF, the frontend may need to call four different APIs.
This creates additional network requests and requires the frontend to understand how different backend services work.
With a BFF, the frontend can make a single request:
The BFF fetches the required information from the backend services and combines it into one response.
This is particularly useful for mobile clients, where network latency and connection quality can significantly affect performance.
Combining requests does not automatically make them faster. The BFF still calls each underlying service, so the response time depends on how efficiently it makes those calls, for example in parallel rather than one after another.
A BFF typically handles several frontend-specific concerns.
Different clients may need the same underlying information in different formats.
For example, a web application might display detailed order information, while a mobile application displays a simplified summary.
Each BFF shapes the same underlying data for its own interface.
Some logic is specific to how a frontend presents information.
For example, a mobile application might require a simplified checkout summary, while a desktop application displays a detailed breakdown.
The BFF can handle presentation-oriented decisions, such as selecting fields, organizing sections, and formatting response structures.
The BFF should not independently calculate taxes, determine product eligibility, or decide whether a payment is valid. Those rules belong in shared backend services so they stay consistent across all clients.
A BFF can also simplify authentication for browser applications.
In one common pattern, the BFF manages OAuth tokens on the server and exposes a secure, HTTP-only session cookie to the browser.
This reduces the need to expose access tokens directly to frontend JavaScript.
However, the BFF must still implement appropriate protections, including CSRF defenses for cookie-authenticated state-changing requests.
Backend services should continue enforcing their own authorization requirements.
A BFF can cache data that is frequently requested by its frontend.
For example, a Mobile BFF might cache product categories or frequently accessed product summaries.
Caching can reduce latency and lower the load on backend services.
However, cache policies must account for data freshness and user-specific information. Sensitive personalized responses must not be shared across users.
Let's walk through an example involving an order details screen.
The mobile application makes the following request:
The Mobile BFF receives the request and performs these steps:
The Mobile BFF aggregates information from multiple backend services.
The frontend now makes one request instead of coordinating multiple backend calls.
The simulation below uses a mobile home screen instead of the order details screen. On one side, the phone calls the user, orders, recommendations, and promotions services itself over the mobile network. On the other, it makes one call to a mobile BFF, which makes the four calls inside the datacenter and trims the response. Compare the totals: four round trips and 48 KB in 1.2 s against one round trip and 6 KB in 0.32 s.
BFFs and API Gateways are related concepts, but they solve different problems.
An API Gateway primarily manages incoming API traffic and enforces common policies.
A BFF focuses on providing frontend-specific APIs and response structures.
These components can work together.
For example:
The API Gateway handles shared policies, while each BFF provides APIs optimized for its clients.
A BFF does not necessarily require a separate API Gateway. In smaller systems, the BFF itself may handle routing, authentication, and other entry-point responsibilities.
The BFF pattern is particularly useful when different frontend applications have significantly different data requirements.
Consider using a BFF when:
However, a BFF is not always necessary.
If the application has only one frontend and a simple backend, introducing a dedicated BFF may add unnecessary complexity.
Similarly, if multiple clients have nearly identical requirements, they may be able to share the same API without dedicated backend layers.
The goal is not to create a BFF for every frontend automatically, but to introduce one when the benefits justify the additional service.
Although BFFs simplify frontend development, they introduce their own challenges.
Every BFF is another service that needs deployment, monitoring, scaling, and maintenance.
If an organization has many frontend applications, managing multiple BFFs can increase operational overhead.
Different BFFs may implement similar aggregation or transformation logic.
Some duplication is acceptable because each BFF should be independently adaptable.
However, shared business rules should not be copied across BFFs. They belong in backend services or shared domain components.
A single frontend request can trigger several downstream calls.
For example, one dashboard request might result in five backend requests.
At high traffic volumes, this can create significant load on shared services.
BFFs should use efficient batching, caching, and aggregation strategies where appropriate.
A BFF often depends on multiple backend services.
If one service becomes slow, it can affect the entire response.
Use appropriate timeouts, parallel requests, and controlled fallback behavior.
Optional information such as recommendations can be omitted during a partial failure. Critical information should never be silently replaced with a misleading value, such as an empty order list when the Order Service times out.
A BFF works best when the team responsible for the frontend can also influence or own its BFF.
This allows the API contract to evolve alongside the user interface.
Without clear ownership, a BFF can become another centralized backend service that slows down frontend development.
Backend-for-Frontend is an architectural pattern that creates frontend-specific backend layers.
It helps applications reduce unnecessary network requests, avoid over-fetching, and provide APIs that match the needs of different clients.
BFFs are especially valuable in microservices architectures where a single frontend screen may require information from several backend services.
However, they introduce additional infrastructure, potential code duplication, and new failure points.
A good BFF keeps frontend-specific concerns close to the frontend while preserving core business logic and data ownership in shared backend services.
10 quizzes