We use machine learning technology to do auto-translation. Click "English" on top navigation bar to check Chinese version.
Backends for Frontends Pattern
In this blog post, we describe how you can improve end-user customer experience on your User Interfaces (UI) by implementing the Backend for Frontend pattern and providing real-time visual updates when your
The solution proposed combines two patterns: 1) the
To explain the solution to both
The BFF pattern
According to
Traditionally, the approach to accommodating more than one type of UI is to provide a single, server-side API, and add more functionality as required over time to support new types of mobile interaction. The following challenges may result from this approach:
- Mobile devices make fewer calls, and want to display different (and probably less) data than their desktop counterparts – this means that API backends need additional functionality to support mobile interfaces.
-
Modern application UIs are increasingly adopting reactive strategies to provide real-time feedback to end-users (for example viaWebSockets ), and different devices may implement different technology stacks to support it. - API backends are, by definition, providing functionality to multiple, user-facing applications – this means that the single API backend can become a bottleneck when rolling out new delivery, due to the many changes being made to the same deployable artifact.
To address these challenges, Sam
Following Sam’s suggestion, the BFF pattern has been adopted by companies like Netflix, where their Android team
Components of an event-driven BFF on Amazon Web Services
Assuming that you already have an
The diagram below presents a high-level view of the architecture and its message flow. Representing the domain publishers on the right, each with its own domain-specific aggregate database; and the BFF subscribers on the left, each with its own user-experience-specific projection database. In the middle, there’s an event bus propagating domain state changes, allowing publishers and subscribers to remain decoupled.

Figure 1. Message flow diagram.
The event-driven BFF solutions described in this blog post rely on a technology-specific API, in addition to the following common components:
- A
NoSQL database to store domain projection tables, which also supportsChange Data Capture (CDC). Here, we useAmazon DynamoDB – a fast, flexible NoSQL database service for single-digit millisecond performance at any scale. - A
compute-layer to process requests and to integrate the CDC stream with the API. Here, we useAmazon Web Services Lambda – a serverless, event-driven compute service that lets you run code without thinking about servers or clusters. - An authentication and authorization mechanism to protect the API. Here, we use
Amazon Cognito – a simple and secure service for user sign-up, sign-in, and access control.
Building an event-driven REST BFF using API Gateway
The following architecture diagram describes, using

Figure 2. Diagram of RESTful BFF using API Gateway.
Implementation steps:
- Catch the events from your application with purpose-built BFF event consumers. These are responsible for updating denormalized data projections in
Amazon DynamoDB for frontend consumption. - On UI load, frontend clients authenticate with
Amazon Cognito , then query the data by invoking the BFF API built withAmazon API Gateway . The data is then fetched in DynamoDB, either directly by API Gateway or via a BFF query handler built withAmazon Web Services Lambda . - Frontend clients subscribe for any subsequent data changes by connecting to a BFF
WebSocket endpoint provided by API Gateway, which triggers the update of the “connected clients” table. - Continue to consume and process all relevant events from your application using the BFF event consumers. These consumers continuously update the
denormalized frontend data view in the BFF database in real time. -
Subscribe to all events resulting from data changes in the BFF database using Amazon DynamoDB Streams, then register atrigger in Amazon Web Services Lambda to asynchronously invoke a BFF stream-handler Lambda function when it detects new stream records. - Your BFF stream handler then pushes notifications to clients connected to API Gateway’s WebSockets.
- When the change notification from API Gateway is received by the frontend clients, they can refresh the UI content.
Building an event-driven GraphQL BFF using AppSync
We see organizations increasingly choosing to build APIs with GraphQL because it helps them develop applications faster, by giving frontend developers the ability to query multiple databases, microservices, and APIs with a single GraphQL endpoint.
The following architecture diagram describes, using

Figure 3. Diagram of GraphQL BFF using AppSync.
Implementation steps:
- Catch the events from your application with purpose-built BFF event consumers. These are responsible for keeping a denormalized view of data in
Amazon DynamoDB for frontend consumption. - On UI load, frontend clients
authenticate withAmazon Cognito , then query the data with GraphQL by invoking the BFF API built withAmazon Web Services AppSync . The data is then fetched in DynamoDB, either directly by Amazon Web Services AppSync or via a BFF query handler built withAmazon Web Services Lambda . - Frontend clients
subscribe for any subsequent data changes using Amazon Web Services AppSync subscriptions over WebSockets. - Continue to consume and process all relevant events from your application using the BFF event consumers. These consumers continuously
update thedenormalized frontend data view in the BFF database in real-time. -
Subscribe to all events resulting from data changes in the BFF database using Amazon DynamoDB Streams, then register atrigger in Amazon Web Services Lambda to asynchronously invoke a BFF stream-handler Lambda function when it detects new stream records. - Your BFF stream handler then invokes an empty
mutation on the Amazon Web Services AppSync GraphQL schema, purposely created to force the subscription to betriggered , thus sending a notification to clients. - When the change notification from Amazon Web Services AppSync is received by the frontend clients, they can refresh the UI content.
Amazon Web Services AppSync provides a simplified WebSockets experience with real-time data, connections, scalability, fan-out and broadcasting which are all handled by the Amazon Web Services AppSync service, allowing developers to focus on the application use cases and requirements instead of dealing with the complexity of infrastructure required to manage WebSockets connections at scale.
The Amazon Web Services Lambda stream handler function in step 6 is triggered by new items being inserted into the Amazon DynamoDB table. It reads these items and updates Amazon Web Services AppSync, alerting it to the new data. A sample of this code, written in node.js, is available
The code below shows the entry point to the Lambda function. It receives an event from DynamoDB, triggered by new data in the stream. For each new entry, if it is data that has been inserted (rather than updated or deleted), it will parse the data and call the executeMutation
function.
The code below is the executeMutation
function that mutates Amazon Web Services AppSync with the new data received from the DynamoDB stream. Using the third-party library
The mutation is generated using the following GQL code:
Conclusion
The BFF pattern refers to having one backend per user experience, instead of having only one general-purpose API backend.
By implementing the BFF pattern in an event-drive architecture, you can improve end-user customer experience on your UI by providing near-real-time visual updates when your microservices raise events about mutations in domain aggregates.
In this blog post, we explained how developers can apply the BFF pattern to their
You can download the detailed reference architectures in PDF for Amazon API Gateway
About the authors:
The mentioned AWS GenAI Services service names relating to generative AI are only available or previewed in the Global Regions. Amazon Web Services China promotes AWS GenAI Services relating to generative AI solely for China-to-global business purposes and/or advanced technology introduction.