Independent providers boot one at a time — an app pays the sum of its providers' latencies, not its longest chain #71

Closed
opened 2026-08-29 16:48:52 +00:00 by chrnx · 0 comments
Owner

The graph knows which nodes are independent. Nothing uses that fact: boot walks the topological order one node at a time, so an app pays the sum of its providers' latencies instead of the longest chain.

What exists today

bootProviders is a for...of with an await inside — src/app/index.ts:854:

for (const node of orderedProviders) {
  bus.emit('boot:provider:start', { name: node.name, scope: node.origin });
  const value = await runners.get(node.name)!(await snapshot(container, node.needs));
  container.register(node.name, 'app', () => value);
  await container.resolve(node.name); // warm the cache
  …
}

topoSort (src/graph.ts:46) returns a flat GraphNode[]. The ordering is correct, but flattening discards the only thing that would make concurrency derivable: which nodes sit at the same depth and therefore cannot constrain each other. There is no Promise.all anywhere in src/ — the single concurrent primitive in the codebase is a Promise.race in src/http/stream.ts:74, for write backpressure.

Measured

Three providers, no dependencies between them, each sleeping 200ms, on 26.8.0-beta.1:

   9ms start alpha
 211ms end   alpha
 211ms start beta
 413ms end   beta
 413ms start gamma
 614ms end   gamma

boot total: 616ms

616ms for a graph whose critical path is 200ms. With real providers this is a pool handshake, a schema check and a warm-up query that have nothing to say to each other, serialized because the list is flat.

Why this is worth doing here specifically

We already charge users for the DAG: they write needs and provides instead of ordering app.use calls by hand. Today they get back exactly one benefit from paying that — pruning. subgraphFor (src/graph.ts:82) is real and it works: /api/prices genuinely does not run Authenticate.

Concurrency is the other half, and it is the half a middleware chain structurally cannot offer. An Express user cannot parallelize app.use safely because nothing declares what is independent. We have that declaration and are not spending it. Boot time is also the one number a framework comparison reaches for first, and right now ours is the worst case by construction.

Scope

Boot providers — the clear case. Batch orderedProviders by dependency depth and Promise.all each level. Nothing between two same-level providers can order them; that is what the topological sort already proved. Needs a topoSort variant that returns GraphNode[][] rather than GraphNode[], or a depth annotation the booter groups on.

Per-request steps — not the same problem, do not bundle it. runSteps (src/pipeline.ts:69) has the same shape, but the context became one object mutated in place rather than copied per step, and Object.assign(context, output) on a shared accumulator from concurrent steps is a data race, not an optimization. If we want this, it needs its own issue and its own design.

Shutdown stays sequential. docs/plans/2026-08-17-shutdown-teardown-design.md:95 already settled it: "Reverse-topological order only means something if it is respected, and parallel teardown discards it." That reasoning holds and this issue does not reopen it. Boot is different — between independent nodes there is no order to respect.

What gets harder

  • Failure attribution. Today the first failing provider in list order throws and boot stops. Under Promise.all two providers at the same level can fail in the same tick; we have to decide whether the first rejection wins or we report the set. The current provider '<name>' failed: … error shape assumes exactly one.
  • boot:provider:start / :ok / :fail ordering. Consumers can currently assume events arrive in boot order. Interleaved emissions from a parallel level break any collector that pairs start/ok positionally.
  • Optional providers and degradedProviders. The degrade path pushes into a shared array from inside the loop; that needs to survive concurrent writes.
  • Whether it is opt-in. A provider with an undeclared side-effect dependency on another — writing a file a later one reads, say — works today by accident and breaks under batching. Given beta, it may be worth landing behind a flag before making it the default.

Acceptance

  • A test with N independent providers each sleeping t completes in roughly t, not N × t.
  • A test with a real chain (sessions → Authenticate → user) still boots in dependency order.
  • Deterministic failure reporting when two same-level providers both throw.
The graph knows which nodes are independent. Nothing uses that fact: boot walks the topological order one node at a time, so an app pays the sum of its providers' latencies instead of the longest chain. ## What exists today `bootProviders` is a `for...of` with an `await` inside — `src/app/index.ts:854`: ```ts for (const node of orderedProviders) { bus.emit('boot:provider:start', { name: node.name, scope: node.origin }); const value = await runners.get(node.name)!(await snapshot(container, node.needs)); container.register(node.name, 'app', () => value); await container.resolve(node.name); // warm the cache … } ``` `topoSort` (`src/graph.ts:46`) returns a flat `GraphNode[]`. The ordering is correct, but flattening discards the only thing that would make concurrency derivable: which nodes sit at the same depth and therefore cannot constrain each other. There is no `Promise.all` anywhere in `src/` — the single concurrent primitive in the codebase is a `Promise.race` in `src/http/stream.ts:74`, for write backpressure. ## Measured Three providers, no dependencies between them, each sleeping 200ms, on 26.8.0-beta.1: ``` 9ms start alpha 211ms end alpha 211ms start beta 413ms end beta 413ms start gamma 614ms end gamma boot total: 616ms ``` 616ms for a graph whose critical path is 200ms. With real providers this is a pool handshake, a schema check and a warm-up query that have nothing to say to each other, serialized because the list is flat. ## Why this is worth doing here specifically We already charge users for the DAG: they write `needs` and `provides` instead of ordering `app.use` calls by hand. Today they get back exactly one benefit from paying that — pruning. `subgraphFor` (`src/graph.ts:82`) is real and it works: `/api/prices` genuinely does not run `Authenticate`. Concurrency is the other half, and it is the half a middleware chain structurally cannot offer. An Express user cannot parallelize `app.use` safely because nothing declares what is independent. We have that declaration and are not spending it. Boot time is also the one number a framework comparison reaches for first, and right now ours is the worst case by construction. ## Scope **Boot providers — the clear case.** Batch `orderedProviders` by dependency depth and `Promise.all` each level. Nothing between two same-level providers can order them; that is what the topological sort already proved. Needs a `topoSort` variant that returns `GraphNode[][]` rather than `GraphNode[]`, or a depth annotation the booter groups on. **Per-request steps — not the same problem, do not bundle it.** `runSteps` (`src/pipeline.ts:69`) has the same shape, but the context became one object mutated in place rather than copied per step, and `Object.assign(context, output)` on a shared accumulator from concurrent steps is a data race, not an optimization. If we want this, it needs its own issue and its own design. **Shutdown stays sequential.** `docs/plans/2026-08-17-shutdown-teardown-design.md:95` already settled it: *"Reverse-topological order only means something if it is respected, and parallel teardown discards it."* That reasoning holds and this issue does not reopen it. Boot is different — between independent nodes there is no order to respect. ## What gets harder - **Failure attribution.** Today the first failing provider in list order throws and boot stops. Under `Promise.all` two providers at the same level can fail in the same tick; we have to decide whether the first rejection wins or we report the set. The current `provider '<name>' failed: …` error shape assumes exactly one. - **`boot:provider:start` / `:ok` / `:fail` ordering.** Consumers can currently assume events arrive in boot order. Interleaved emissions from a parallel level break any collector that pairs start/ok positionally. - **Optional providers and `degradedProviders`.** The degrade path pushes into a shared array from inside the loop; that needs to survive concurrent writes. - **Whether it is opt-in.** A provider with an undeclared side-effect dependency on another — writing a file a later one reads, say — works today by accident and breaks under batching. Given beta, it may be worth landing behind a flag before making it the default. ## Acceptance - A test with N independent providers each sleeping `t` completes in roughly `t`, not `N × t`. - A test with a real chain (`sessions → Authenticate → user`) still boots in dependency order. - Deterministic failure reporting when two same-level providers both throw.
chrnx closed this issue 2026-09-04 13:32:30 +00:00
Sign in to join this conversation.