Lesson 6 of 9 · 26m
State Isolation and Cross-Application Communication
Keep state local by default, pass identity and context explicitly, version domain events, and never build a shared mutable frontend store.
The most seductive bad idea in micro-frontends is the global shared store. It starts innocently: "checkout needs the cart count, search needs the selected store, account needs the user object." Six months later every team imports the same mutable store, deploy order matters, and a type change in one repository breaks four production applications at runtime.
State isolation is the point of the architecture. The shell and remotes may share context, but they should not share ownership.
Classify the state before choosing the mechanism
- Local UI state — open menus, focused inputs, transient wizard steps. Keep it inside the remote.
- Domain state — cart contents, account profile, search results. Owned by one slice, exposed through explicit contracts.
- Session context — user identity, locale, feature flags, theme. Read-mostly context passed in by the shell.
- Shareable navigation state — filters, selected tabs, pagination. Put it in the URL.
- Cross-domain notifications — cart updated, order completed, session expiring. Use versioned events.
If you cannot classify a piece of state, that usually means the boundary is wrong.
Pass context in as props or initialization data
<CheckoutApp
session={{ userId: user.id, locale: "en-US", currency: "USD" }}
featureFlags={flags.checkout}
initialCartId={cartId}
/>The remote should not reach into shell internals or read random globals. Explicit inputs make test harnesses and local development possible.
Use events for facts, not commands
window.dispatchEvent(new CustomEvent("acme:cart.updated", {
detail: {
version: 2,
cartId: "crt_92fj",
itemCount: 3,
subtotal: { amount: 8425, currency: "USD" },
},
}));An event says "this happened." It should not say "checkout, open your modal now." Commands create hidden coupling and make the emitter depend on the receiver's UI.
Version event payloads. Consumers must tolerate unknown fields, and producers must not remove fields inside a version.
APIs beat shared frontend caches
If catalog data is needed by checkout, checkout can call the catalog read API or receive a narrow projection. What it should not do is inspect the search remote's client cache because "it is already there." Caches are implementation details; APIs are contracts.
Auth is shared context, not shared machinery
The shell usually owns token acquisition and session lifecycle. Remotes receive authenticated fetch clients, user identity, or narrowly scoped capabilities. Do not let every remote implement token refresh independently; thundering herd refresh races and inconsistent logout behavior are guaranteed.
Field notes
- If two teams must coordinate to understand a data structure, neither fully owns it.
- Custom events are easy to abuse; require schemas, versioning, and ownership documentation.
- A shared mutable store across independently deployed frontends recreates the monolith at runtime with worse tooling.
- Logout is a cross-application flow. Test it explicitly: one remote should not keep displaying authenticated data after the shell ends the session.
Was this lesson helpful?