Chapter 13 of 27 · Part 6: External API and UI

UI patterns: micro frontends and live updates

FTGO's backend is split into services, but its web app is still one big codebase owned by one team, and it only learns about changes when it asks. This chapter shows how to split the user interface along service lines and how to push fresh data to the browser.

key idea / coordinator service / good path in progress / watch out problem / failure neutral

Where we are

In chapter 12 we gave FTGO's web and mobile clients one entry point: an API gateway, one backend per frontend (BFF), and GraphQL for flexible queries. That left one problem open: behind the gateway, the user interface itself is still one big frontend, owned by one team, and it shows data that is stale the moment it loads.

This chapter

How do FTGO's service teams ship their own parts of the screen, and how does a customer see "your order is on its way" without pressing refresh? By the end you can choose between page-based and widget-based micro frontends, design task-based screens, feed each part of a page its data, and push events to browsers.

One frontend team becomes the bottleneck

Gateways exist to serve user interfaces, so start with the people who build those interfaces.

Problem

The Kitchen team wants to show customers an estimate of when their meal will be ready. Kitchen Service already computes it. But the FTGO web app belongs to a separate frontend team, the gateway to a platform team, and the estimate needs a new database column owned by a data team. One small control needs three tickets in three backlogs, and it ships a month later.

Splitting the backend into services gave each team its own code and its own releases. A layered organization, with one team per technical layer, takes that autonomy straight back: every feature crosses every layer, so every feature needs a hand-off.

Layered teams versus stream-aligned teams In a layered organization one new control needs work from three teams. A stream-aligned team owns the whole slice, UI and services together. Layered: hand-offs Stream-aligned: whole slice UI layer team Services team Data team 3 teams, 3 hand-offs Ordering UI + services one team Kitchen UI + services one team Delivery UI + services one team
Solution · Stream-aligned teams own their UI

Give each team a whole slice of the product: its screens, its services and its data. The Kitchen team builds the estimate on the page as well as in Kitchen Service, and ships it without waiting for anyone.

Organizations keep dedicated frontend teams for a few reasons. Each reason has another answer that avoids the hand-offs.

Why dedicated frontend teams exist Teams cite scarce specialists, consistency and hard-to-split single-page apps. Each has a way out that avoids the hand-offs. Reason given Another way Specialists are scarce Embed them, or an enabling team We want a consistent look Style guide, shared components Single-page apps are hard to split Micro frontends Amazon trades a consistent UI for team autonomy: the AWS console shows it.

Scarce specialists can be embedded in a team, split their time across a few teams, or form an enabling team that helps others become self-sufficient. Some companies accept less consistency on purpose: the AWS console varies from service to service because Amazon values team autonomy more. A team that owns its slice end to end also stays close to the customers and restaurants who use it.

Cost

Teams now need frontend skills, and FTGO needs a style guide and shared components to stay coherent. And the ownership is only on paper while the code is one front-end codebase: the Kitchen team still has to edit, test and release the whole web app to change one panel.

Split the frontend: pages, widgets and a shell

Ownership needs code boundaries to match. The UI is often the last monolith left after the backend is split.

Problem

FTGO's web app is one single-page application. The order page shows the menu (Restaurant team), the basket (Ordering team) and the delivery estimate (Delivery team). A change to the delivery estimate means rebuilding and redeploying the whole app, and a broken build from the Restaurant team blocks the Delivery team's release.

Solution · Micro frontends

Split the frontend into parts that different teams build and deploy independently, just as services split the backend. Split either by page or by widget.

Four ways to build the user interface A monolithic frontend is one deployable. Page-based and widget-based decomposition split it up. Both lead to micro frontends. Monolithic frontend All UI state and behavior in one unit. Fine for one team, drifts to silos. Page-based /restaurants/ goes to Restaurant team. Simple. Default for websites. Widget-based A container page plus widgets, each owned by the team behind it. Micro frontends Frontends deployed independently, by page or by widget. Self-contained systems are a related idea: one team owns each web app, no shared UI.

Page-based splitting is the simple one. Each team serves whole pages, and the URL decides who answers: /restaurants/ goes to the Restaurant team's frontend, /orders/ to the Ordering team's. It needs no clever JavaScript and no iFrames. The user follows a link and a new page loads. Choose it by default for a website.

Widget-based splitting suits a rich single-page app where one screen mixes several teams' work. A shell, or container page, owns navigation and layout. Each team ships a fragment that the shell places on the page, and each fragment talks to its own service or read model.

One shell, many owned fragments Each team ships its own part of the page. A shell composes them. Page shell Basket owned by ordering team Menu owned by restaurant team Delivery estimate owned by delivery team Each fragment talks to its own service or read model and deploys independently.

Widgets must not call each other directly, or they would couple just like services that share code. They talk the way services do: one widget raises a custom browser event, and the others subscribe and react.

Widgets talk with events In a widget-based page the restaurant list widget raises a custom browser event. Other widgets subscribe and react, like events between services. Page container: navigation and layout Restaurants list widget RestaurantChosen Menu Delivery estimate Watch page weight: each widget may bring its own copy of a framework.
PatternPick it whenWatch out
Monolithic frontendOne team owns UI and servicesSilos if many teams share it
Page-basedYou are building a websiteCross-cutting features fit badly
Widget-basedA rich SPA with several teamsBundle size, clashing dependencies
API calls onlyA feature does not fit a page or widgetEach client re-implements it

Self-contained systems take the page-based idea furthest: each team owns a complete web app with its own UI, logic and data, and the apps link to each other instead of sharing any UI code.

Cost

Widgets can bloat the page, because each may bring its own copy of a framework, and their dependencies can clash. Features that cut across pages, such as a basket badge in every header, fit neither split well. And splitting the code does not fix screens that were designed around database tables instead of what users do.

Screens that say what the user means

Once a team owns its screens, it can also change how those screens talk to its service.

Problem

FTGO's "edit order" screen is a form with every field of the order and one Save button. Order Service receives UpdateOrder with the whole order. Did the customer change the delivery address, or cancel? Order Service has to diff the fields to guess, and the events it publishes say only "order updated", so Kitchen Service and Delivery Service have to guess too.

Solution · Task-based UI

Design screens around the tasks users want to do, such as "change address" or "cancel order". Each task sends one small command, and the service publishes an event that says exactly what happened.

CRUD form versus task-based UI Task buttons map to commands, so the event says what happened. CRUD: edit every field Save order (all fields) UpdateOrder: what changed? Task-based: act on intent Change address Cancel order AddressChanged event Task-based screens produce small, meaningful commands, which fits events and CQRS (lessons 10 and 11).

A CRUD screen (create, read, update, delete) edits fields and hides the business meaning. A task-based screen exposes intent, and that intent maps cleanly to the commands and domain events from chapter 3, to an event-sourced history (chapter 10) and to CQRS views (chapter 11).

Cost

There are more commands and screens to design, and the team must understand the user's tasks, not just the table. The commands handle writes well. Reads are still awkward: the order page needs data from several services at once.

Feed each part of the screen its data

Commands go to one service. A screen usually reads from several.

Problem

The "My orders" page shows each order's status (Order Service), the restaurant name (Restaurant Service) and the courier's position (Delivery Service). If the browser calls all three for every order, it must know every service's address and API, a slow mobile connection makes dozens of round trips, and the page breaks whenever one service changes.

The browser should not know about all those services. Hand it one answer instead: joined at request time, or prepared ahead of time from events.

Three ways to feed a screen Call each service, add an aggregating layer, or read a prepared view. UI one call Aggregating layer one entry point Many services order, restaurant UI one call Read model one prepared view Events from the services Top: the layer calls services at request time. Bottom: events fill a view ahead of time.
Solution · One call per screen part

Give each page or widget a single source: an aggregating layer or BFF that composes service calls (chapter 12), or a read model that events keep up to date (chapter 11). The team that owns the widget usually owns its source too.

OptionGood forWatch out
Aggregating layerOne entry point that hides many services.Can become a central coupling point.
Backend for frontendOne tailored API per client (web, mobile).More code to own and keep in sync.
API compositionJoining a few small responses.Slow and fragile for large joins, filtering or paging.
Read modelSearch, filter and sort over many services, such as a restaurant catalog.Eventually consistent; chapter 11 shows how.

Composition at request time stops scaling as a screen grows. The page waits for the slowest service, and one service that is down fails the whole page.

Why composing in the API stops scaling Joining many service responses on every request costs latency and breaks on one failure. Request list orders Call 3 services wait for the slowest One is down whole page fails A prepared read model answers from one store and stays up when a source service is down.
Cost

An aggregating layer can become a central point that every team must change. A read model is eventually consistent and is one more store to run. And either way the page is a snapshot: once "My orders" has loaded, it does not know that the courier just picked up the meal.

Stop polling: push events to the browser

The services already publish events when an order changes. The browser just never hears them.

Problem

To show live order status, the FTGO web app polls Order Service every 5 seconds. Say 10,000 customers have the page open at dinner time: that is 2,000 requests a second, and almost every answer is "nothing changed". When the courier does pick up the meal, the customer still sees the old status for up to 5 seconds.

Polling wastes calls and still shows stale data. The obvious fix is to let the browser listen to the order events, but a browser cannot join a Kafka consumer group: it is not a trusted server, it comes and goes, and it must see only its own customer's orders.

Solution · Client manager with server push

Put a small service between the broker and the browsers. It consumes the events, keeps track of which browsers are connected and for which customer, filters the events per user, and pushes each one down an open connection.

A client manager between broker and browsers A small service consumes events and pushes them to connected clients. Broker event stream Client manager knows who is connected Browser A Browser B The manager filters events per user, so each browser sees only its own data.

The connection to the browser can use one of a few techniques:

TechniqueHow it worksUse when
WebSocketsTwo-way, long-lived connection.The UI also sends frequent messages.
Server-sent eventsOne-way stream from server to browser over HTTP.The UI only needs updates, like FTGO's order status.
WebHooksThe server calls a URL you register.Notifying another system, such as a restaurant's own software, not a browser.

Now the event contract reaches all the way to the screen, so describe it as carefully as a REST API. AsyncAPI is the event-driven counterpart of OpenAPI: it documents channels and messages, so teams can generate documentation and code from one description.

Cost

The client manager holds thousands of long-lived connections, must authenticate each one, and must cope with browsers that disconnect and miss events. One page now depends on micro frontends from several teams, their data sources, the broker and the push channel. Each part can work alone and the page can still break where they meet.

Patterns in this chapter

PatternProblem it solvesWhat it costs
Stream-aligned teams own their UIEvery feature needs hand-offs between UI, service and data teamsTeams need frontend skills; consistency needs a style guide and shared components
Page-based micro frontendsOne frontend codebase couples every team's releasesCross-cutting features fit badly
Widget-based micro frontendsOne rich screen mixes several teams' workPage weight, clashing dependencies, a shell to maintain
Self-contained systemsTeams still share UI codeLess consistency across apps
Task-based UICRUD forms hide what the user meant, so events say only "updated"More commands and screens to design
Aggregating layer or BFF per screenThe browser would call many services per pageA coupling point; slow and fragile for big joins
Read model for the UISearch and lists that join data from many servicesEventually consistent; another store to run
Client manager with server pushPolling wastes calls and still shows stale dataLong-lived connections, per-user filtering, missed events on reconnect

Recap

Quick check

Why can a browser not simply consume the Kafka topic of order events?
Show answer

It cannot take part in a consumer group, it is not trusted with every customer's events, and it connects and disconnects at will. A client manager consumes the events, filters them per user and pushes them over WebSockets or server-sent events.

FTGO's order page is one single-page app. The Delivery team wants to release a better delivery estimate panel today, but the Restaurant team's menu changes broke the shared build. What is wrong, and which pattern fixes it?
Show answer

The frontend is still a monolith, so every team's release is tied to every other team's build. Widget-based micro frontends fix it: a shell composes independently deployed fragments, so the Delivery team ships its panel on its own. The panel gets its data from one source owned by the Delivery team, and talks to other widgets only through browser events.

The "edit order" form sends the whole order with every save. What do downstream services lose, and what is the alternative?
Show answer

They lose the intent: Order Service can only publish "order updated", so Kitchen and Delivery must diff fields to learn whether the address changed or the order was cancelled. A task-based UI sends specific commands such as ChangeAddress, which produce specific events such as AddressChanged.

Still open

FTGO now has many services, many independently deployed frontends, gateways, read models and a push channel. Each team can change its part on its own schedule, so how does anyone know the whole thing still works after a change?

Next: 14 Testing strategy and unit tests →