0
0
NestJSframework~10 mins

Provider scope (default, request, transient) in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Provider scope (default, request, transient)
Start Application
Resolve Provider
Default Scope: Single Instance
Reuse Same Instance
Request Scope: New Instance Per Request
Create Instance For Each Request
Transient Scope: New Instance Every Injection
Create New Instance Every Time
When NestJS starts, it creates providers based on their scope: default creates one shared instance, request creates a new instance per HTTP request, and transient creates a new instance every time it is injected.
Execution Sample
NestJS
import { Injectable, Scope } from '@nestjs/common';

@Injectable({ scope: Scope.REQUEST })
export class MyService {
  id = Math.random();
}
Defines a service with request scope that gets a new instance with a unique id for each HTTP request.
Execution Table
StepScope TypeActionInstance Created?Instance IDNotes
1DefaultApp starts, provider resolved first timeYes0.1234Single instance created and reused
2DefaultSecond injection in same app lifetimeNo0.1234Same instance reused
3RequestFirst HTTP request startsYes0.5678New instance created for request
4RequestSecond injection in same requestNo0.5678Same instance reused within request
5RequestSecond HTTP request startsYes0.9101New instance created for new request
6TransientFirst injection anywhereYes0.1111New instance created every injection
7TransientSecond injection anywhereYes0.2222Another new instance created
8TransientThird injection anywhereYes0.3333Another new instance created
9-App stops or no more injectionsNo-Execution ends
💡 No more injections or app stops, so no new instances created
Variable Tracker
Instance IDStartAfter Step 1After Step 3After Step 5After Step 6After Step 7After Step 8Final
Default Scope InstanceNone0.12340.12340.12340.12340.12340.12340.1234
Request Scope InstanceNoneNone0.56780.91010.91010.91010.91010.9101
Transient Scope InstanceNoneNoneNoneNone0.11110.22220.33330.3333
Key Moments - 3 Insights
Why does the default scope provider reuse the same instance every time?
Because in the execution_table at steps 1 and 2, the instance ID stays the same (0.1234), showing the single instance is reused throughout the app lifetime.
How does the request scope provider behave across multiple HTTP requests?
As seen in steps 3 and 5, a new instance is created for each HTTP request (IDs 0.5678 and 0.9101), but within the same request (step 4), the instance is reused.
Why does the transient scope create a new instance every time?
Because at steps 6, 7, and 8, each injection creates a new instance with a different ID, showing no reuse happens.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is true about the request scope instance?
AIt is the same instance as step 3
BIt is a new instance different from step 3
CIt is the same as the default scope instance
DNo instance exists at this step
💡 Hint
Check the Instance ID column for steps 3 and 4 in execution_table
At which step does the transient scope create its second new instance?
AStep 6
BStep 8
CStep 7
DStep 5
💡 Hint
Look at the Instance Created? and Instance ID columns for transient scope in execution_table
If the default scope provider was changed to transient, what would happen at step 2?
ANo new instance would be created
BA new instance would be created
CThe app would crash
DThe instance ID would stay the same
💡 Hint
Compare how transient scope creates new instances every injection in execution_table steps 6-8
Concept Snapshot
Provider scopes in NestJS control instance creation:
- Default: one shared instance for app lifetime
- Request: new instance per HTTP request
- Transient: new instance every injection
Use @Injectable({ scope: Scope.X }) to set scope.
Scopes affect memory and behavior of services.
Full Transcript
In NestJS, providers can have different scopes that control how many instances are created and when. The default scope creates one instance that is reused everywhere. The request scope creates a new instance for each HTTP request, but reuses it within that request. The transient scope creates a new instance every time the provider is injected. This example shows how instances get created and reused or recreated depending on the scope. Understanding this helps manage resource use and service behavior in your app.