Micro Frontend Architecture
- til
- architecture
- micro-frontends
What micro frontends are, the three ways to stitch them together, and the problems you sign up for when you use them.
Contents
Big frontends get painful. Once enough teams share one app, every deploy waits on everyone else and a change in one corner breaks something in another. Micro frontends are one way out. Here's what I've learned about them.
What they are
Take the idea behind microservices and apply it to the UI. Instead of one big app, you split the frontend into smaller pieces. Each piece can be built, tested and deployed on its own, and usually one team owns it.
On an e-commerce site, for example, the product catalog, the reviews and the cart could each be their own micro frontend, owned by three different teams.
What you get
- Teams work on their own. One team can ship without waiting for, or breaking, another.
- Mixed tech is possible. One part can be React and another Vue, as long as they fit together on the page. I wouldn't go out of my way to do this, but it helps during a migration.
- Separate deploys. A release only touches one piece, so a bad one breaks less.
- It scales with headcount. Adding a team means adding a piece, not more people fighting over one repo.
Three ways to put them together
1. In the browser
The browser loads each micro frontend and assembles the page at runtime. It's the most flexible option, but users download more and the first load is slower.
2. On the server
The server builds one HTML response out of all the pieces before sending it. First load is faster. The cost is more logic on the server.
3. At the edge
The CDN stitches the pieces together close to the user. You get some of the speed of server composition and some of the flexibility of client composition.
The problems you sign up for
Wiring it all together
Getting separate apps to behave like one is the hard part. single-spa and Webpack's Module Federation do a lot of the heavy lifting.
CSS leaking between pieces
Two teams will eventually both write .button. Use CSS Modules, Shadow DOM or some other scoping from the start.
Performance
Several apps on one page means several copies of everything unless you're careful. Lazy load what you can and share the big libraries like React.
What I'd do
- Split by business area. Each piece should own a clear feature, like "cart", not a technical layer.
- Share as little as possible. Every shared component is a dependency between teams.
- Use a design system. It's what keeps four teams' work looking like one product.
- Automate deploys from day one. Without CI/CD, independent deploys don't happen in practice.
Micro Frontend Architecture
How micro frontends map to teams. Source: Maruti Techlabs
When it's worth it
- Several teams work on different features of one big app.
- Those teams are spread out and need to move on their own.
- Parts of the app need to ship on different schedules.
If you're one team with one app, you probably don't need any of this. A well-organised monolith is easier.