Microservice Patterns
UI Composition
Build pages from fragments owned by different teams — assembled on the server or in the browser — and give optional fragments their own deadlines, so one slow team cannot make the whole page slow.
What you will learn
- Compare server-side page-fragment composition with client-side composition (micro-frontends).
- See how one slow fragment delays an all-or-nothing page.
- Apply per-fragment timeouts, fallbacks and streaming.
- Keep a consistent look and shared dependencies under control.
Before you start
Overview
A newspaper's front page has sections from different desks. If the sports desk is late, the paper still goes to print — with a placeholder or yesterday's results — rather than holding the whole edition.
When backend teams own business capabilities end to end, the UI can follow: each team owns fragments (product details, recommendations, reviews) that are composed into one page. Server-side composition assembles HTML fragments in a page service or at the edge (SSI, ESI, a composition layer); client-side composition loads team-owned components into a shell in the browser (micro-frontends, module federation, web components).
Run it
A product page is composed from four fragments; recommendations becomes slow at t = 10 s. Compare page latency when the composer waits for everything (A) and when recommendations has a 150 ms deadline and a fallback (B).
Server-side vs client-side
| Server-side fragments | Client-side (micro-frontends) | |
|---|---|---|
| First paint / SEO | Complete HTML, good SEO | Needs SSR or a fast shell |
| Slow fragment | Must be handled with deadlines or streaming | Only its area of the page waits |
| Team independence | Fragment services deploy independently | Bundles deploy independently |
| Risks | Composer becomes a bottleneck | Duplicate dependencies, inconsistent UX, larger downloads |
Deadlines per fragment
public Page render(long productId) {
var header = fragments.fetch("header", productId); // required
var product = fragments.fetch("product", productId); // required
var recs = fragments.fetch("recommendations", productId)
.completeOnTimeout(Fragment.placeholder("recommendations"), 150, MILLISECONDS)
.exceptionally(e -> Fragment.placeholder("recommendations"));
var reviews = fragments.fetch("reviews", productId)
.completeOnTimeout(Fragment.placeholder("reviews"), 200, MILLISECONDS);
return Page.of(header.join(), product.join(), recs.join(), reviews.join()); // all run concurrently
}Trade-offs
| Benefits | Costs | |
|---|---|---|
| Ownership | Teams ship UI for their capability without a central frontend bottleneck. | Shared design system, routing and state need clear governance. |
| Resilience | Optional fragments can fail without breaking the page. | Only if every fragment has a deadline and a fallback. |
| Performance | Fragments can be cached and rendered independently. | Many fragments mean many calls; client-side composition can bloat bundles. |
Production considerations
- Classify fragments as essential or optional, and give optional ones placeholders.
- Share a design system and a small set of runtime dependencies across micro-frontends.
- Cache fragment output at the edge where it is not personalised (CDN).
- Monitor each fragment's latency and error rate separately.