Skip to content
NestRS

The request lifecycle

The stages every unit runs, on every edge, and how scope chooses where a guard, pipe, interceptor or exception filter executes — exactly once.

Four layer families wrap every unit the framework dispatches — an HTTP request, a GraphQL field, a WebSocket message, an MCP tool call, a job’s attempt, a tick, a listener call: guards (gate access), pipes (edge conversion), interceptors (cross-cutting work around the handler) and exception filters (answering a failure). One pipeline runs them, in one order, whatever the edge. This page is the one place that explains how they compose — every other page links here instead of re-explaining it.

Declaring a layer at any scope — global (GlobalGuards<(…)> and its siblings, listed by the module the app boots), host (on the controller, resolver, gateway, MCP host or worker block), or handler (beside the method) — contributes to one pool per family, deduplicated by TypeId in a single composition. The layer executes exactly once per unit. Broadest scope wins; #[force_guards] is the opt-in to re-run a guard.

Scope never multiplies executions — it chooses the execution site. The teachable rule:

Global = around every unit the app serves. Scoped = around your handler. Either way, once.

You can watch the dedup happen. Declare RequireApiKey both globally (GlobalGuards<(RequireApiKey,)>) and on a controller (#[use_guards(RequireApiKey, …)]), and boot logs the redundant declaration it dropped — the guard still runs exactly once:

DEBUG nest_rs::layers: redundant layer declaration deduped
layer="RequireApiKey" kept="global" skipped="host"
hint="broadest site wins; `#[force_guards]` re-runs a guard"

Every unit runs the same stages, outermost first:

  1. The edge: the unit’s span and its operation line, the request scope, the deadline, panic containment.
  2. The unit of work, opened through the database binding when one is imported: a transaction where the unit writes, the pool otherwise. It commits on the unit’s own outcome.
  3. The unit stage: #[interceptor(stage = unit)] (Server-Timing, the RFC 9728 challenge).
  4. The guard stage: the global, host and method guards, then the #[authorize] class gate. A refusal is answered here.
  5. Exception filters: the method’s, the host’s, then the app’s.
  6. The handler stage: the app’s global list, ordered by Layer::priority, the order written breaking a tie, then the host’s, then the method’s.
  7. The tail: the global pipes over the input, the rules of every value each decoded argument carries, the per-argument pipes, the handler, the reply — each wire row it carries judged where the handler built it.

Each layer reads the unit through one execution context: which unit, which handler, what it declared, what it carries. The operation span is outside all of it, so every stage logs under one trace_id — see Correlation.

The stages are the same everywhere; what a unit has decides which of them run:

Unit Stages What differs
HTTP route 1 to 7 its guards run check_http — their check unless they override it — the global pipes read the body
A self-mount’s request: /graphql, /mcp, a gateway’s upgrade, the OpenAPI document 1 to 5 its guard stage is the request stage the transport composes; no handler of yours runs, so no handler stage — /graphql’s variables meet the global pipes at its endpoint
What the router answers itself: a 404, a 405, a module’s fallback 1, 3, 5 no unit of work, no guard
GraphQL root field 1, 4 to 7, inside its request its guards run check; the request ran the unit stage
#[field_resolver] call its resolver’s and its own guards, its own interceptors and filters runs once per parent row, inside the root field that produced it
WebSocket message 1 to 7 its guards run check, the global pipes read its data
A WebSocket frame no handler reads: an event none serves, a frame no envelope holds 1, 3, 5 no unit of work, no guard; the app’s exception filters alone
MCP request 1 to 3 one unit per JSON-RPC request the /mcp request carries
its tool call or prompt fetch 4 to 7, inside the request its guards run check
Job, tick, listener call 1 to 3, 5 to 7 no caller, so no guard stage; the decorator’s timeout is its deadline

A WebSocket #[on_connect] or #[on_disconnect] hook carries what the upgrade’s guards attached, the ability withheld from its reads, and runs no layer yet.

Global guards are pooled globally but execute in each site’s guard stage, after routing, because a guard reads the site’s declarations — #[public], #[meta] — that only exist once the site is known. Nothing the framework runs before them reads an HTTP body: the transport wraps it, and whatever reads it first — a guard, the pipe pool, an extractor — reads it under the body cap and its gap bound.

One spelling per stage. At the handler stage an app-wide interceptor is the app’s list, GlobalInterceptors<(…)>, declared by the module it boots: a feature never chooses what wraps every handler. Around the guards — a 404 and a refusal included — it is what a module import brings, #[interceptor(stage = unit)], for a capability needing nothing but its import; writing stage = unit is the visible line that opens it.

The #[authorize] class gate runs with the guards, before any interceptor or filter the site declares: a caching interceptor never answers a caller the gate refuses, and a filter never sees the refusal. Layer::priority orders entries within a site, never across sites: a handler-scope guard at -10 still runs after every global and host guard.

A failure is sticky. A handler’s error, or a 4xx/5xx it answered, fails the unit; an exception filter or an interceptor may change what the client reads, never whether the unit failed, so a failed unit rolls back whatever it answered.

#[public] and #[meta(EXPR)] are not layers — they are declarations a layer reads. #[public] marks a route or an operation as anonymous-admissible (the authentication guard admits it through), on every edge; where a guard running at the request a WebSocket message or an MCP operation arrives on would turn a visitor away first, it could open nothing, and the boot refuses it; #[meta] declares typed data on a host or one of its methods, which a layer reads through cx.reflector(). Each site keeps one declaration for the process, its values built once as it mounts; its layers read it through a pointer, so nothing is copied per unit.

Every site’s #[meta] is read the same way: a route, a resolver’s field, a gateway’s message, an MCP tool, a job, a tick and a listener call keep theirs with their site, read by its guards where it has a caller and by its interceptors and exception filters everywhere.

Wiring mistakes fail the boot with a named error, never a silent drop.

If a provider injects a layer whose module the app never imported, the access graph fails the boot naming the exact fix:

module access violation: `PostsController` (in module `PostsHttpModule`)
depends on `AuthzGuard`, but `PostsHttpModule` imports no module that
provides it. `AuthzGuard` is provided by `AuthzModule` — add
`AuthzModule` to `#[module(imports = [...])]` of `PostsHttpModule`, or
route the dependency through a module `PostsHttpModule` already imports.

A global guard, pipe, interceptor or exception filter that cannot resolve (its provider’s module is not imported) fails the boot of every app, one that serves no HTTP included, naming the type: the check runs at the boot’s wiring step, before any hook.

A layer a site declares — on a controller or a route, a resolver or a field, a gateway or a message, an MCP host or an operation, a worker block or a method — that no imported module provides fails the boot too, naming the layer and the site. No site ever runs without a layer it declares.

A route whose guards are misordered — an authorization guard listed before the authentication guard whose principal it reads, at any scope — fails the boot naming both, rather than answering 500 to every request; a global pool listed that way fails it in an app serving no route too, and so does a GraphQL field, a WebSocket message or an MCP operation, read after the guards its request ran.