Provider scopes in NestJS determine the lifetime of a provider instance. By default, providers have application lifecycle scope, but can also be request-scoped.

Default scope (Application lifecycle)

Providers are instantiated once when the application bootstraps and destroyed when the application shuts down. All requests share the same instance.

@Injectable()
export class CatsService {
  // Single instance shared across all requests
}

Request scope

Providers are instantiated for each incoming request and destroyed after the response is sent:

@Injectable({ scope: Scope.REQUEST })
export class RequestScopedService {
  // New instance for each request
}

When to use request scope

  • Per-request data storage
  • Request-specific resources
  • When you need guaranteed isolation between requests

Trade-offs

Application scope (default):

  • ✅ Efficient (single instance)
  • ✅ Safe in Node.js (see State Sharing Model)
  • ✅ Suitable for most services

Request scope:

  • ⚠️ Performance overhead (instantiation per request)
  • ✅ Guaranteed isolation
  • ✅ Useful for request-specific context

Most applications should use the default scope unless specific per-request isolation is needed.