Frontend System Design · Lesson 39 of 86 · 4 min
Communication Between Micro-Frontends
In an interview, this topic tests your understanding of distributed systems principles on the client side. Your answer shows whether you can design for loose coupling or if you'd accidentally build a distributed monolith.
When your micro-frontends need to interact, you have to enable that communication without destroying the isolation that made you choose them in the first place. The goal is to create explicit, well-defined contracts between them, not a tangled web of direct dependencies.
There are four main patterns. You need to know the trade-offs for each.
1. Use Browser Events for Simple, Local Communication
This is the most straightforward approach. You use the browser's built-in eventing system.
Custom Events are for micro-frontends running in the same browser window and on the same origin. One micro-frontend dispatches a CustomEvent on a shared DOM element, typically window or document, and others listen for it.
It's simple, decoupled, and requires no external libraries. Use it for simple notifications, like a "product added to cart" event fired by a product page, which a header micro-frontend listens to in order to update a cart icon.
window.postMessage() is for when your micro-frontends are in different browser contexts, most commonly an iframe. This is the only secure way to communicate across different origins on the client.
Your interviewer is listening for two words: "origin validation". When you receive a message, you must check the
event.originproperty to ensure the message is from a trusted source. When you send a message, you must specify the target origin. Omitting this is a critical security flaw and a major red flag in an interview.
2. Implement a Pub/Sub Bus for Application-Wide Events
For more complex applications, relying on raw DOM events can become chaotic. It's hard to track what events exist, who is firing them, and who is listening.
A global Pub/Sub (Publish/Subscribe) event bus solves this. It's a simple, centralized module that micro-frontends can import. Instead of dispatching events on the window, they publish named events to the bus. Other micro-frontends subscribe to those events.
This pattern formalizes the communication. It gives you a single place to log, debug, or even middleware events. It decouples senders from receivers; a publisher doesn't know or care who is subscribed.
Here is a minimal implementation. This is all you need to explain the concept.
// --- Global Event Bus (shared module) ---
class EventBus {
private subscribers: { [key: string]: Function[] } = {};
on(eventName: string, callback: Function) {
if (!this.subscribers[eventName]) {
this.subscribers[eventName] = [];
}
this.subscribers[eventName].push(callback);
}
emit(eventName: string, data?: any) {
if (!this.subscribers[eventName]) return;
this.subscribers[eventName].forEach(callback => callback(data));
}
}
export const globalEventBus = new EventBus();
// --- Micro-frontend A (e.g., Authentication) ---
import { globalEventBus } from './event-bus';
function login(user: { name: string }) {
// ...login logic
globalEventBus.emit('auth:user-logged-in', user);
}
// --- Micro-frontend B (e.g., Header) ---
import { globalEventBus } from './event-bus';
globalEventBus.on('auth:user-logged-in', (user: { name: string }) => {
console.log(`Updating header to show welcome message for ${user.name}`);
// document.getElementById('user-display').textContent = `Welcome, ${user.name}`;
});
The trap: Don't let your event bus become a dumping ground for complex state. It's for transient events, not for state management. An event should announce that something happened, not transfer the entire application state in its payload.
3. Use the URL for State and Navigation
The URL is the most robust and naturally decoupled communication channel you have. It's global, user-visible, and bookmarkable.
Use the URL to communicate page-level state. When a user clicks an item in a list, navigate to a new URL like /products/123. The product detail micro-frontend can then read the ID from the URL and fetch its own data.
This completely decouples the list from the detail view. The list micro-frontend doesn't need to know anything about the detail micro-frontend, only how to construct a URL.
This is the best pattern for cross-micro-frontend navigation and for passing small, public identifiers.
4. Use Shared State as a Last Resort
Sometimes, you have a piece of state that is truly global and fundamental, like user authentication status or the current theme. Forcing this through events can be cumbersome.
In these rare cases, you can use a shared library or state management module (like a tiny Zustand store or a simple context provider). The key is to keep this shared surface area as small as humanly possible.
The trap: Proposing a single, monolithic Redux store for all micro-frontends. This is an anti-pattern that recreates the tightly coupled monolith you were trying to escape. If you share state, share only the atoms of state that are absolutely necessary, and keep the logic for updating that state isolated. For example, only the auth micro-frontend should be able to update the user's auth status in the shared store.
Try it
Implement a simple EventBus class like the one above in a single file. Create two functions that simulate micro-frontends. Have one function publish a theme:changed event with a payload { theme: 'dark' }. Have the second function subscribe to that event and log the received payload to the console.
Next: Micro-Frontends: Pros and Cons for Large Teams
Lock it in — apply this lesson under interview conditions.