Micro-frontends at scale.
One portal, thirty lines of business. At CRED the merchant portal and the internal tools ran as micro-frontends, one per line of business. The rules that kept them working as one product, what it cost, and when I would not do it.
- Published
- 20 Mar 2024
- Length
- 6 min read
- Roles
- Architecture, Frontend, Product
- From the work on
- CRED
At CRED, a merchant dealt with several of CRED's businesses at once, and each line of business ran on its own tool, some of them third-party. We had somewhere between twenty and thirty lines of business.
We replaced that with one Merchant Portal, and built the internal tools the same way. Each line of business became its own micro-frontend, loaded into a shared shell with module federation. The third-party tools went away, and merchant score went up by 21%.
This is what I learned about running that many micro-frontends as one product.
What it buys you
A micro-frontend buys you one thing: a team can ship its part of the product without waiting for anyone else. Its own repository or package, its own pipeline, its own release.
You pay for it at runtime. Code that used to be linked together at build time is now stitched together in the browser, and everything a bundler used to check for you (one copy of React, matching versions, types that line up) becomes something you have to design for.
So the first question is whether you have the problem. With two or three areas and one team, you do not. With thirty lines of business, each with its own owners and its own release calendar, you do. A single build would have meant thirty teams queueing behind one deploy.
The shape
There is a shell, also called the host. It owns the frame: sign-in, navigation, layout, the design system and error handling. Then there are remotes, one per line of business. The shell loads a remote when the user navigates to it.
The rules that held up
1. The shell owns the frame and nothing else
The shell does authentication and session, the navigation, the page layout, the design system provider, the top-level error boundary and telemetry. It has no business logic. The moment a line of business needs a change in the shell to ship a feature, independent deploys are gone.
A useful test: if every remote disappeared, the shell should still sign you in and show an empty, working portal.
2. A remote is a route
Each line of business owns a URL space, and the shell mounts the remote when the route matches. One remote on screen at a time.
Composing several remotes into one screen looks tempting and is where most of the pain lives: two teams' loading states, two teams' errors and two release schedules, sharing a single view. When a screen needs data from two lines of business, one of them owns the screen and calls the other's API.
3. Share very little, and share it as singletons
Every shared dependency is a contract between all remotes. Keep the list short: React, the router, the design system, and the small client the shell uses to hand over the session.
const deps = require("./package.json").dependencies;
new ModuleFederationPlugin({
name: "payouts",
filename: "remoteEntry.js",
exposes: { "./App": "./src/App" },
shared: {
react: { singleton: true, requiredVersion: deps.react },
"react-dom": { singleton: true, requiredVersion: deps["react-dom"] },
"react-router-dom": { singleton: true, requiredVersion: deps["react-router-dom"] },
"@acme/design-system": { singleton: true, requiredVersion: deps["@acme/design-system"] },
},
});singleton means one copy at runtime, whoever asks. requiredVersion makes a mismatch a warning you can see instead of a bug you find later. Everything that is not on the list gets bundled into the remote that uses it, and that is fine. Two remotes carrying their own date library is a few kilobytes. Two remotes carrying their own React is a broken page.
4. Resolve remotes at runtime
If the shell's build knows the URL of every remote, then shipping a remote means rebuilding the shell, and you are back to one deploy queue.
Keep a registry instead: a small JSON file, or an endpoint, that maps each line of business to the current entry file of its remote. The shell reads it at startup and loads from there.
import { init, loadRemote } from "@module-federation/enhanced/runtime";
type Registry = Record<string, { entry: string }>;
export async function setupRemotes() {
const registry: Registry = await fetch("/remotes.json", { cache: "no-store" }).then((r) => r.json());
init({
name: "shell",
remotes: Object.entries(registry).map(([name, { entry }]) => ({ name, entry })),
});
}
export function loadLineOfBusiness(name: string) {
return loadRemote<{ default: React.ComponentType }>(`${name}/App`);
}Now a remote's pipeline ends by updating one line in the registry. Rolling back is changing that line back. The shell never rebuilds.
5. A remote that fails must fail alone
Thirty remotes means that on any given day one of them has a bad deploy. The portal has to survive that.
Wrap every remote in its own error boundary. Put a timeout on the load. Show a plain fallback in the content area, with the navigation still working, so the merchant can go and do something else.
const Remote = lazy(() =>
Promise.race([
loadLineOfBusiness(name),
new Promise<never>((_, reject) => setTimeout(() => reject(new Error(`${name} timed out`)), 8000)),
]),
);
<ErrorBoundary fallback={<Unavailable name={name} />} onError={report}>
<Suspense fallback={<PageSkeleton />}>
<Remote />
</Suspense>
</ErrorBoundary>;6. No shared state between remotes
A shared store ties releases together. The first time one remote changes the shape of a slice another remote reads, both have to ship on the same day.
Remotes get what they need from three places: the URL, the context the shell hands down (who the user is, what they are allowed to see), and their own API calls. For the rare case where one remote needs to tell another that something happened, a small typed event on the window is enough.
7. The design system is the real shared dependency
React changes major versions every few years. The design system changes every week, and every remote depends on it.
Treat it like a public library. Additive changes ship freely. A breaking change ships as a new major version with a migration window, during which the shell can serve both. If the design system team cannot make a change without coordinating thirty releases, you are back to one release train.
8. Test the contract in CI
Each remote exposes a module with a known shape. The shell expects that shape. That contract deserves a test on both sides: the remote's pipeline checks that what it exposes still type-checks against the shell's expectation, and the shell's pipeline loads every registered remote in a headless browser and checks that it mounts.
Module Federation 2.0, announced in April 2024, helps here. It can generate types for a remote's exposed modules and pull them into the host during development, so a renamed prop is a red squiggle before it is a production incident.
9. Keep local development simple
Nobody should have to run thirty apps to change one. A developer runs their remote locally and points a deployed shell at it with an override, usually a query parameter or a local storage key that the shell honours outside production. The reverse works too: run the shell locally against deployed remotes.
10. Watch the waterfall
Federation adds requests before first paint: the registry, the remote's entry file, then its chunks. Three things keep that cheap.
- Entry files are small and never cached for long. Chunks are content-hashed and cached forever.
- Start loading a remote's entry when the user hovers its navigation item, before they click.
- Look at what each remote ships. Shared dependencies that were meant to load once but load twice show up here first.
What it cost
It was more infrastructure: a pipeline per remote, a registry, and tooling to see which version of which remote was live. Debugging crossed boundaries, so error reports had to say which remote and which version they came from. Onboarding took longer, because a new engineer had to learn the frame before the feature. And versions drifted. Without someone watching, remotes end up on four different versions of the design system.
All of that was worth it for thirty lines of business. It would not be worth it for five.
When I would not do this
- One team, or a handful of areas. A monorepo with packages and route-level code splitting gives you the boundaries without the runtime.
- A site that needs server rendering from the Next.js App Router. The module federation plugin for Next.js does not support the App Router, and its maintainers said in late 2024 that they intend to retire it. For public, SEO-heavy pages I would compose at the route level on the server instead.
- Teams that release together anyway. If every line of business ships on the same day each week, independent deploys solve a problem you do not have.
What I'd carry forward
- Split by ownership. One line of business, one team, one remote, one route.
- Keep the shell thin enough that it rarely needs a release.
- Share React, the router and the design system. Bundle everything else.
- Resolve remotes at runtime from a registry, so rollback is a one-line change.
- Give every remote its own error boundary and a timeout.
- Version the design system like a product, because it is one.