โšกLearnIT
Architecture ยท Topic 18

๐ŸงฉMicro-Frontends & Architecture at Scale

18.1When micro-frontends make sense (and when they don't)

Use them when: many teams need independent deploys; the app has clear feature boundaries; teams want ownership; different permission tiers need genuinely different UIs; scale justifies the complexity.

Avoid when: small team (<~10 devs); tightly-coupled features; frequent cross-module refactoring; a monolith or monorepo would do. Micro-frontends are an organizational scaling tool first, a technical one second โ€” the main win is independent team deployment, and it comes with real infra/coordination cost.

Lighter alternatives: a monorepo (Nx/Turborepo) gives shared tooling + independent packages without runtime federation complexity. Feature flags enable gradual rollouts without splitting deploys.

18.2Module Federation (Webpack 5)

A shell/host app loads independently-deployed remotes at runtime. Shared deps (React, etc.) are declared singleton so they load once.

new ModuleFederationPlugin({
  name: 'shell',
  remotes: { '@tickets': 'tickets@https://tickets.example.com/remoteEntry.js' },
  exposes: { './shared': './src/shared', './auth': './src/auth' },
  shared: { react: { singleton: true }, 'react-dom': { singleton: true } },
});

Each remote exposes its components and declares the same shared singletons. Teams deploy on their own schedule; the shell composes them. (Vite has equivalents via @originjs/vite-plugin-federation; Next.js via @module-federation/nextjs-mf.)

18.3Permission-based module loading

Model permissions (role โ†’ permissions map); attach required permissions to each module; only load a remote if the user has them. Resolve permissions server-side during auth so the client knows upfront which modules to load and render. Enforce in the router (don't render routes the user can't access) โ€” but remember client checks are UX; the server must enforce authorization on every request.

18.4Coordinating state & security across remotes

  • Shared state via a store exposed by the shell (e.g., Zustand for user/org/global UI). Remotes read/write it for cross-module coordination without importing each other โ€” keeps them decoupled.
  • Security when loading remote scripts: verify remotes come from trusted domains; use SRI hashes; sandbox with error boundaries so a broken/slow remote shows a fallback instead of crashing the shell.
  • Smart loading: critical modules in parallel immediately; high-priority after; non-critical via requestIdleCallback. Cache loaded modules; set timeouts with fallback UI.

18.5Team coordination

Contract testing โ€” the shell publishes a contract (it exposes Button, Form, useAuthโ€ฆ); each remote tests against it before deploy, so a shell change can't silently break remotes. Semver for shared libs; an RFC process for breaking changes; documented ownership boundaries; per-team CI/CD; a monitoring dashboard (per-module load time, error rate) with auto-rollback on failure.

๐Ÿง  Quiz: Micro-Frontends & Architecture at Scale

1 question

1Micro-frontends make the most sense when:

Answered 0/1