← Blog

NestJS Injection Scopes: Singleton, Request, and Transient

Should a service be shared by the whole application, recreated for every HTTP request, or created separately for each consumer? Injection scopes control the lifetime of providers in NestJS.

Nest provides three common scopes.

1. DEFAULT: one shared instance

The default scope is singleton. Nest creates one provider instance in the application context and shares it wherever it is injected.

@Injectable()
export class ProductsService {}

This is suitable for most services, repositories, and database-related components. It avoids repeatedly constructing objects and allows safe reuse when the service does not store request-specific mutable state.

2. REQUEST: one instance per request

A request-scoped provider gets a new instance for each incoming request:

import { Injectable, Scope } from '@nestjs/common';

@Injectable({ scope: Scope.REQUEST })
export class RequestTrackerService {}

This can help when a provider genuinely needs request-specific state, such as tracking information or tenant context. The trade-off is additional object creation. Request scope also bubbles up the dependency chain: a controller that depends on a request-scoped service becomes request-scoped too.

3. TRANSIENT: a separate instance per consumer

A transient provider is not shared between consumers. Each consumer that injects it receives its own instance:

@Injectable({ scope: Scope.TRANSIENT })
export class LoggerService {}

This can be useful when each consumer needs an independent helper instance. It does not mean a new instance is created for every method call.

Which scope should you choose?

Start with the default singleton scope. Choose request scope only when the dependency truly needs per-request lifetime, and use transient scope when separate consumers need isolated instances.

Avoid putting user-specific mutable data in a singleton service: concurrent requests can then interfere with one another. Also remember that request-scoped dependencies can make more of the dependency tree request-scoped, increasing the work Nest performs per request.

The takeaway: scope is a lifecycle decision, not just a configuration detail. Pick the narrowest lifetime you actually need, while keeping singleton as the normal choice.

Reference: NestJS Injection Scopes