BFF
Backend for Frontend
A dedicated backend server for each frontend. Provides APIs optimized for client needs.
Backend for Frontend
A dedicated backend server for each frontend. Provides APIs optimized for client needs.
BFF (Backend for Frontend) is an architecture pattern that provides a dedicated backend layer specialized for each frontend client (web, mobile, IoT, etc.). Proposed by Sam Newman in 2015 in the context of microservices, it improves frontend development efficiency by providing APIs tailored to each client's requirements instead of a general-purpose API.
The origin of BFF lies in the limits of the assumption that "all clients use the same API." Web apps need rich data, but mobile wants only a minimal set of fields to save bandwidth. Matching both with a general-purpose API causes over-fetching/under-fetching and requires unnecessary data processing on the frontend.
BFF sits between the frontend and the backend microservices, aggregates data from multiple services, and transforms it into the form the client needs. It also handles authentication/authorization, caching, and error handling, which simplifies frontend code.
In practice, BFF is implemented with Next.js API Routes, Remix loader/action, or a separate Node.js/Go server. Large companies such as Netflix, Spotify, and SoundCloud operate separate BFFs for mobile, web, and TV, and in many cases GraphQL takes over the role of the BFF.
// Next.js API Routeλ₯Ό BFFλ‘ νμ©
// pages/api/dashboard.ts (λλ app/api/dashboard/route.ts)
import type { NextApiRequest, NextApiResponse } from 'next';
// μ¬λ¬ λ§μ΄ν¬λ‘μλΉμ€μμ λ°μ΄ν° μ§κ³
async function fetchUserProfile(userId: string) {
const res = await fetch(`${process.env.USER_SERVICE}/users/${userId}`);
return res.json();
}
async function fetchOrders(userId: string) {
const res = await fetch(`${process.env.ORDER_SERVICE}/users/${userId}/orders?limit=5`);
return res.json();
}
async function fetchNotifications(userId: string) {
const res = await fetch(`${process.env.NOTIFICATION_SERVICE}/users/${userId}`);
return res.json();
}
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const userId = req.headers['x-user-id'] as string;
// λ³λ ¬λ‘ μ¬λ¬ μλΉμ€ νΈμΆ
const [profile, orders, notifications] = await Promise.all([
fetchUserProfile(userId),
fetchOrders(userId),
fetchNotifications(userId),
]);
// ν΄λΌμ΄μΈνΈμ νμν ννλ‘ λ³ν
res.json({
user: {
name: profile.name,
avatar: profile.avatarUrl,
},
recentOrders: orders.map(o => ({
id: o.id,
total: o.totalAmount,
status: o.status,
})),
unreadCount: notifications.filter(n => !n.read).length,
});
}
"Calling 5 APIs on the dashboard page is inefficient. If we create a single BFF endpoint and aggregate on the server, the client code gets simpler, and network round trips are reduced, so loading should be about 300ms faster."
"The BFF pattern means having a dedicated backend for each frontend. If web and mobile requirements differ, you create a separate BFF for each. It handles data aggregation, format conversion, and authentication, which lowers frontend complexity while letting the backend APIs remain general-purpose."
"I'd like us to move this data transformation logic out of the client and into the BFF. The mobile app will probably need the same logic too, and on the server you only have to write it once."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free
The BFF should handle only aggregation and transformation. Keep business logic in domain services. If the BFF becomes bloated, it becomes hard to maintain.
If the web BFF and the mobile BFF are 90% the same code, there is no point in separating them. Consider extracting a common library or adopting GraphQL.
Keep the BFF thin, and let the frontend team own it. You can respond quickly to changes in client requirements without depending on the backend team.