Module Federation: Divide, Conquer, Share!

Rate this content
Bookmark

Reusing front-end features built by different teams can be challenging depending on the chosen approach. If we use a library, what about versioning and maintenance? If we use a monolith, what about build times? We can find a pragmatic solution in Module Federation, not only for sharing features but also for dividing and conquering. Join me in this talk to discuss the latest updates of Module Federation, type safety, analyze its challenges, and check a real-world case where we applied this solution to share providers, hooks, features, and interesting issues we faced on the way.

This talk has been presented at JSNation US 2024, check out the latest edition of this JavaScript Conference.

FAQ

Tools like Medusa help visualize micro frontend dependencies, and the Chrome Model Federation plugin can assist in debugging by showing how modules are connected and consumed.

The strangle pattern involves incrementally updating or replacing parts of an application by integrating new components using module federation, allowing gradual migration without disrupting the entire system.

Micro frontends can be beneficial when your team or application has grown, requiring separate teams for maintenance. They allow for independently deployable components, improving maintainability and scalability.

State management should aim for decoupling. Use techniques like hooks, prop drilling, or local storage to manage state independently, or consider creating a federated module specifically for state management.

Use a shared UI kit to ensure consistency in styling across different micro frontend components, avoiding divergent styles like using different fonts on different pages.

Module federation is a technique introduced in Webpack 5, allowing parts of a web application, like components or functions, to be shared across applications at runtime without needing to redeploy the host application.

Build-time techniques require redeployment for changes, ensuring version consistency but adding complexity. Runtime techniques, like module federation, allow immediate updates without redeployment but can introduce runtime errors if backward compatibility isn't maintained.

Micro frontends are an architectural approach where a web application is broken down into smaller, independent, and self-contained frontend components that can be developed, deployed, and maintained by separate teams.

Module Federation 2.0 includes a TypeScript plugin that automatically extracts and shares type definitions from remote modules, ensuring type safety and enabling features like auto-complete in the host application.

To ensure high availability, serve assets from reliable sources like CDNs or industry-standard storage solutions, and implement error boundaries to handle runtime failures gracefully.

Nataly Rocha
Nataly Rocha
20 min
21 Nov, 2024

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Using front-end features can be challenging. Model federation is an approach to implement micro frontends. Micro frontends can be built-in or use runtimes. Two micro frontends need to update together to ensure consistency. Other techniques for micro frontends include web components, system.js, iframes, and monorepos. Model federation in Webpack 5 decouples runtime from build tools, allowing flexibility in model loading. High availability and error boundaries are important for Model Federation. Full-federated sites enable fast development cycles and easy testing. Importing components from a federated model requires dynamic imports, dependency sharing, and performance considerations. Debugging tools like Medusa and the Model Federation plugin for Chrome help identify and resolve issues. Consistent styling can be achieved by using the same UI kit. Routing can be handled by a shell application or frameworks like Single SPA. Decoupling components using specific federated models and considering tradeoffs when designing application growth strategies. Determine if Microfrontends are necessary based on the need for independent modular components in a growing product.

1. Introduction to Micro Frontends

Short description:

Using front-end features can be challenging. Model federation is an approach to implement micro frontends. Micro frontends can be built-in or use runtimes. Built-in requires redeployment while runtimes provide immediate changes. Both options have trade-offs.

We're using front-end features built by different teams sometimes can become challenging depending on the chosen approach. For example, if we use a library, what about versioning and maintenance? If we use a monolith, what about the pipelines and the build times? We can find a pragmatic solution with the model federation for 10 times not only for sharing the features but also for dividing and conquering.

So let's go. Hello there. My name is Nathalie Rochan. I'm a tech lead at Static Builders. I am really glad to be part of JS Nation US and basically have the chance to share some experiences and concepts with you. So, let's start first with micro frontends. And model federation is just one approach of many to implement micro frontends. But why in the first place you would consider implementing micro frontends, right? It could be for many reasons. For example, your team has grown, your application has grown in a way that maintenance requires separate teams or just it makes sense to have our project divided in different parts so that it can be more maintainable. And it is also self-contained and independently deployable, which is one of the main features or the key feature of a micro frontend. Right.

So we can divide the micro frontend techniques, implementation techniques, in two types. The first one would be built-in and the other one would be runtimes. Right. So built-in means that micro frontends are kept in separate models, but they are still available at compile time. So they must be redeployed for a change to be effective. One example of this can be NPM packages or libraries where you actually have to bump the version and redeploy your application so that the change is affected. Right. On the other hand, it means that these are consumed at runtime, as the name says. When you deploy something, for example, using model federation, which is a way to achieve runtime dependencies, ECMAScript or System.js are another. The host or consumer applications don't need to bump the version or be redeployed to have this change. Once a producer application makes the change, immediately you can see a life change in the host applications because this is consumed at runtime in the browser. OK. So here we start to see that both options have their trade-offs. Right. More like trade-offs, things that you might want to consider when choosing them. So for example, on build-time, let's say that you have four micro frontends. Right.

2. Model Federation and Micro Frontends

Short description:

Two micro frontends need to update together to ensure consistency. Model federation in micro frontends enables runtime updates for all teams. However, if break-in changes occur or applications are tightly coupled, issues may arise. Other techniques for micro frontends include web components, system.js, iframes, and monorepos. Model federation in Webpack 5 decouples runtime from build tools, allowing flexibility in model loading.

And one of them released a new version. Depending on how these interact, let's say that two micro frontends need to update. So how would you make sure that both are using the same version? Let's say that one team actually got the Slack message saying, hey, we bumped the version or an alert or anything. They remembered it, but the other team didn't. So you could end up with different versions for the user, which kind of diminishes the experience of one application only. But you can enforce this using different tools. But we start adding complexity to enforce this, as you can see. Instead, runtime, for example, with micro frontends, with model federation, for example, you solve the issue because everyone will have the same version as soon as the other team deploys. Right. But what happens if they introduce a break-in change? What happens if some applications were tightly coupled? Right. So the application could break. In this case, we might want to ensure that we have total decoupling or that we have backward compatibility, or we might end up in the adding version again. Right. So as you can see, both have their tradeoffs. Here are also some other techniques that you can do, you can use for micro frontends, like web components, system.js, even iframes, monorepos with NX frameworks like single spot or bit. But we are going to be focusing on model federation. So let's see how it works.

Model federation was introduced in Webpack 5 and this year the 2.0 version was released. The way it works is by enabling the plugin in the build tool. In this case, we are using Webpack. But in the 2.0 version, it decouples the runtime from the build tools, increasing the flexibility of model loading. So as you can see here, we define which are our remote dependencies and the elements that we want to expose. We can expose anything from a component to a function provider. For our use case, we use something called or something that we can call the strangle pattern. So we can use models from an application developed by other teams. This is because the application that the other team was developing, we already knew that it was kind of going to be deprecated, but we needed a way to actually start using it fast and that we can complete our host application. So what we did is to use model federation to basically import hooks, import some providers that we needed to obtain information from the other app, and also the main component that we used to complete our application. So in this case, I'm going to represent our use case with a movie theater application where the seat beaker and the user profile were being developed by other teams. So as you can see, we are exposing the components from both. And on the host application, we consume them.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced 2022React Advanced 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Speeding Up Your React App With Less JavaScript
React Summit 2023React Summit 2023
32 min
Speeding Up Your React App With Less JavaScript
Top Content
Watch video: Speeding Up Your React App With Less JavaScript
Mishko, the creator of Angular and AngularJS, discusses the challenges of website performance and JavaScript hydration. He explains the differences between client-side and server-side rendering and introduces Quik as a solution for efficient component hydration. Mishko demonstrates examples of state management and intercommunication using Quik. He highlights the performance benefits of using Quik with React and emphasizes the importance of reducing JavaScript size for better performance. Finally, he mentions the use of QUIC in both MPA and SPA applications for improved startup performance.
React Concurrency, Explained
React Summit 2023React Summit 2023
23 min
React Concurrency, Explained
Top Content
Watch video: React Concurrency, Explained
React 18's concurrent rendering, specifically the useTransition hook, optimizes app performance by allowing non-urgent updates to be processed without freezing the UI. However, there are drawbacks such as longer processing time for non-urgent updates and increased CPU usage. The useTransition hook works similarly to throttling or bouncing, making it useful for addressing performance issues caused by multiple small components. Libraries like React Query may require the use of alternative APIs to handle urgent and non-urgent updates effectively.
The Future of Performance Tooling
JSNation 2022JSNation 2022
21 min
The Future of Performance Tooling
Top Content
Today's Talk discusses the future of performance tooling, focusing on user-centric, actionable, and contextual approaches. The introduction highlights Adi Osmani's expertise in performance tools and his passion for DevTools features. The Talk explores the integration of user flows into DevTools and Lighthouse, enabling performance measurement and optimization. It also showcases the import/export feature for user flows and the collaboration potential with Lighthouse. The Talk further delves into the use of flows with other tools like web page test and Cypress, offering cross-browser testing capabilities. The actionable aspect emphasizes the importance of metrics like Interaction to Next Paint and Total Blocking Time, as well as the improvements in Lighthouse and performance debugging tools. Lastly, the Talk emphasizes the iterative nature of performance improvement and the user-centric, actionable, and contextual future of performance tooling.
How React Compiler Performs on Real Code
React Advanced 2024React Advanced 2024
31 min
How React Compiler Performs on Real Code
Top Content
I'm Nadia, a developer experienced in performance, re-renders, and React. The React team released the React compiler, which eliminates the need for memoization. The compiler optimizes code by automatically memoizing components, props, and hook dependencies. It shows promise in managing changing references and improving performance. Real app testing and synthetic examples have been used to evaluate its effectiveness. The impact on initial load performance is minimal, but further investigation is needed for interactions performance. The React query library simplifies data fetching and caching. The compiler has limitations and may not catch every re-render, especially with external libraries. Enabling the compiler can improve performance but manual memorization is still necessary for optimal results. There are risks of overreliance and messy code, but the compiler can be used file by file or folder by folder with thorough testing. Practice makes incredible cats. Thank you, Nadia!
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
Watch video: Debugging JS
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.

Workshops on related topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured WorkshopFree
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Building WebApps That Light Up the Internet with QwikCity
JSNation 2023JSNation 2023
170 min
Building WebApps That Light Up the Internet with QwikCity
Featured WorkshopFree
Miško Hevery
Miško Hevery
Building instant-on web applications at scale have been elusive. Real-world sites need tracking, analytics, and complex user interfaces and interactions. We always start with the best intentions but end up with a less-than-ideal site.
QwikCity is a new meta-framework that allows you to build large-scale applications with constant startup-up performance. We will look at how to build a QwikCity application and what makes it unique. The workshop will show you how to set up a QwikCitp project. How routing works with layout. The demo application will fetch data and present it to the user in an editable form. And finally, how one can use authentication. All of the basic parts for any large-scale applications.
Along the way, we will also look at what makes Qwik unique, and how resumability enables constant startup performance no matter the application complexity.
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
Next.js 13: Data Fetching Strategies
React Day Berlin 2022React Day Berlin 2022
53 min
Next.js 13: Data Fetching Strategies
Top Content
WorkshopFree
Alice De Mauro
Alice De Mauro
- Introduction- Prerequisites for the workshop- Fetching strategies: fundamentals- Fetching strategies – hands-on: fetch API, cache (static VS dynamic), revalidate, suspense (parallel data fetching)- Test your build and serve it on Vercel- Future: Server components VS Client components- Workshop easter egg (unrelated to the topic, calling out accessibility)- Wrapping up
Building a Shopify App with React & Node
React Summit Remote Edition 2021React Summit Remote Edition 2021
87 min
Building a Shopify App with React & Node
Top Content
WorkshopFree
Jennifer Gray
Hanna Chen
2 authors
Shopify merchants have a diverse set of needs, and developers have a unique opportunity to meet those needs building apps. Building an app can be tough work but Shopify has created a set of tools and resources to help you build out a seamless app experience as quickly as possible. Get hands on experience building an embedded Shopify app using the Shopify App CLI, Polaris and Shopify App Bridge.We’ll show you how to create an app that accesses information from a development store and can run in your local environment.
React Performance Debugging
React Advanced 2023React Advanced 2023
148 min
React Performance Debugging
Workshop
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)