← Back to Angular, page 5

angular-0090 · Hard senior

When would you choose micro-frontends for Angular, how can they be built, and what problems do they bring?

Micro-frontends split a large UI into independently built and deployed apps owned by separate teams, loaded into a shell at run time. In Angular this is usually done with Module Federation or Native Federation, with shared contracts and careful version management. They solve organisational scaling, not technical complexity, and bring cost in performance, consistency and operations.

Full answer

The reason to adopt micro-frontends is organisational: many teams need to release separately, on different schedules, without coordinating one big build. They are not a way to make a small app better. A modular monolith in a monorepo (with Nx or Angular workspaces and enforced boundaries) is simpler and enough for most teams.

Ways to build them:

  • Module Federation (webpack) and Native Federation (browser import maps, works with the esbuild builder): a shell loads a remote's entry file at run time and gets its routes or components. @angular-architects/native-federation is the common choice for the application builder.
  • Iframes: strong isolation, poor integration (routing, sizing, accessibility).
  • Web Components (Angular Elements): each remote exposes custom elements, so even different frameworks can coexist.
  • Build-time composition: separate libraries published as packages and assembled in one app. Teams still release together, which can be fine.

Design decisions:

  1. Granularity: split by business area (billing, courses), not by technical layer. Each remote owns a route prefix.
  2. Shared dependencies: the shell and remotes must share one copy of Angular, or you run two Angular instances. Federation config marks @angular/* as singletons with a version range. A major version mismatch breaks the page, so plan upgrades together or use a compatibility window.
  3. Contracts: a small shared package defines the interface between the shell and the remote (authentication state, navigation events, route data). Avoid sharing services with internal types.
  4. Routing: the shell owns the top-level router and loads each remote with loadChildren, which makes it lazy loaded.
  5. Resilience: a remote can be down. Load with a timeout and a fallback page.
  6. Consistency: shared design tokens or a component library keep the look the same, and accessibility needs one standard.
  7. Operations: independent pipelines, versioned remote entries, a manifest that the shell reads at run time (so a remote can be released without redeploying the shell), and contract tests.

Costs: more network requests, duplicated code when sharing is imperfect, harder local development and debugging, and more complex monitoring. Say clearly when you would not use them.

typescript
// file: contract.ts
import { InjectionToken, Signal } from '@angular/core';
import { Routes } from '@angular/router';

// Shared package: the only thing the shell and remotes both import.
export interface ShellApi {
  readonly userName: Signal<string | null>;
  navigate(path: string): void;
}

export const SHELL_API = new InjectionToken<ShellApi>('SHELL_API');

export interface RemoteEntry {
  routes: Routes;
}

// file: remote-loader.ts
import { Component } from '@angular/core';
import { Route, Routes } from '@angular/router';
import { RemoteEntry } from './contract';

@Component({
  selector: 'app-remote-unavailable',
  template: `<p role="alert">This section is not available right now. Please try again later.</p>`,
})
export class RemoteUnavailableComponent {}

const FALLBACK: Routes = [{ path: '**', component: RemoteUnavailableComponent }];

function withTimeout<T>(promise: Promise<T>, ms: number): Promise<T> {
  return new Promise<T>((resolve, reject) => {
    const timer = setTimeout(() => reject(new Error('Remote timed out')), ms);
    promise.then(
      (value) => {
        clearTimeout(timer);
        resolve(value);
      },
      (error) => {
        clearTimeout(timer);
        reject(error);
      },
    );
  });
}

export function remoteRoute(path: string, load: () => Promise<RemoteEntry>, timeoutMs = 4000): Route {
  return {
    path,
    loadChildren: () =>
      withTimeout(load(), timeoutMs).then(
        (entry) => entry.routes,
        () => FALLBACK,
      ),
  };
}

// file: app.routes.ts
import { Routes } from '@angular/router';
import { RemoteEntry } from './contract';
import { remoteRoute } from './remote-loader';

// With Native Federation the loader is: loadRemoteModule('billing', './routes').
// A plain dynamic import of a URL is used here to keep the sample dependency free.
const BILLING_ENTRY = 'https://billing.example.com/entry.js';

export const routes: Routes = [
  remoteRoute('billing', () => import(/* @vite-ignore */ BILLING_ENTRY) as Promise<RemoteEntry>),
];

Angular 20.3, type-checked. The sample shows the shell side: a typed contract, a loader with timeout and fallback, and the routes. The federation plugin itself is not installed here, so the dynamic import of a real remote is not run.

Key points

  • Micro-frontends solve team scaling and independent releases, not technical problems.
  • Options: Module or Native Federation, iframes, Web Components, build-time packages.
  • Hard parts: shared singleton dependencies, contracts, consistent UX, and operations.
  • A modular monolith with enforced boundaries is often the better first choice.

Common mistakes

  • Adopting micro-frontends for a small team and paying the cost with no benefit.
  • Sharing Angular versions loosely, producing two copies of Angular on one page.
  • Sharing internal services across remotes, which couples them tightly.

Follow-ups

  • How would you upgrade Angular across the shell and ten remotes?
  • How do you share login state between the shell and a remote?
  • How would you detect a broken contract before release?

Tips

  • Open by saying what problem they solve, and when you would not use them.
  • Name the singleton shared dependency problem. Interviewers look for it.
  • Give an alternative (modular monolith).

Evaluation rubric

  • Gives clear reasons and counter-reasons.
  • Knows the technical options and their trade-offs.
  • Addresses sharing, contracts, resilience, and operations.