Rate limiting helps protect your app from too many requests at once. It keeps your server safe and fair for everyone.
Rate limiting with throttler in NestJS
import { Throttle } from '@nestjs/throttler'; @Throttle(limit, ttl) @Controller('example') export class ExampleController { @Get() getExample() { return 'Hello!'; } }
ttl means time to live in seconds (how long the limit lasts).
limit means how many requests are allowed in that time.
@Throttle(10, 60) @Get('data') getData() { return 'Data here'; }
@Throttle(5, 30) @Get('info') getInfo() { return 'Info here'; }
@Throttle(20, 120) @Controller('users') export class UsersController { @Get() getUsers() { return ['User1', 'User2']; } }
This controller has one route that allows only 3 requests per minute. If a user sends more, they get blocked until time resets.
import { Controller, Get } from '@nestjs/common'; import { Throttle } from '@nestjs/throttler'; @Controller('greet') export class GreetController { @Throttle(3, 60) // 3 requests per 60 seconds @Get() sayHello() { return 'Hello, welcome!'; } }
Make sure to import and configure ThrottlerModule in your app module to enable throttling.
Rate limits apply per user IP by default, so different users have separate limits.
You can customize error messages when limits are exceeded for better user experience.
Rate limiting protects your app from too many requests.
Use @Throttle(ttl, limit) to set limits on routes or controllers.
Configure ThrottlerModule to enable throttling in NestJS.
Practice
@Throttle(ttl, limit) decorator in a NestJS application?Solution
Step 1: Understand the purpose of rate limiting
Rate limiting is used to protect an app from too many requests in a short time.Step 2: Role of
This decorator sets how many requests are allowed within a time window (ttl).@Throttle(ttl, limit)Final Answer:
To limit how many requests a user can make in a given time -> Option AQuick Check:
Rate limiting = request limit [OK]
- Confusing throttling with logging
- Thinking it speeds up responses
- Assuming it retries failed requests
Solution
Step 1: Understand
The first parameter is ttl (time to live in seconds), second is limit (max requests).@Throttle(ttl, limit)parameter orderStep 2: Match parameters to desired limit
For 10 requests per 60 seconds, ttl=60 and limit=10, so@Throttle(60, 10).Final Answer:
@Throttle(60, 10) -> Option BQuick Check:
ttl first, limit second = @Throttle(60, 10) [OK]
- Swapping ttl and limit parameters
- Using named parameters which are unsupported
- Providing only one parameter
import { Controller, Get } from '@nestjs/common';
import { Throttle } from '@nestjs/throttler';
@Controller('api')
export class ApiController {
@Get('data')
@Throttle(10, 5)
getData() {
return 'Success';
}
}Solution
Step 1: Analyze the throttle settings
@Throttle(10, 5) means 5 requests allowed per 10 seconds.Step 2: Evaluate request count against limit
6 requests exceed the limit by 1, so the 6th request is blocked.Final Answer:
First 5 requests return 'Success', 6th request is blocked -> Option AQuick Check:
Limit 5 per 10s, 6th blocked [OK]
- Assuming all requests succeed
- Thinking limit is per request instead of per time window
- Confusing ttl and limit values
@Throttle(60, 10) to a controller method but it does not limit requests as expected. What is the most likely cause?Solution
Step 1: Check if
Throttling requiresThrottlerModuleis configuredThrottlerModule.forRoot()to be imported in the app module.Step 2: Understand decorator usage and parameters
The decorator works on controllers and methods; parameter order is correct as@Throttle(60, 10).Final Answer:
You forgot to import and configureThrottlerModulein your app module -> Option CQuick Check:
Module config required for throttling [OK]
- Assuming decorator works without module setup
- Swapping ttl and limit parameters
- Not restarting server after changes (usually needed but not main cause)
Solution
Step 1: Configure global throttling in
Global settings useThrottlerModule.forRoot(){ ttl: 60, limit: 100 }for 100 requests per 60 seconds.Step 2: Override limit on specific controller with
The decorator parameters are@Throttle(60, 200)@Throttle(ttl, limit), so 200 requests per 60 seconds overrides global limit.Final Answer:
SetThrottlerModule.forRoot({ ttl: 60, limit: 100 })globally and add@Throttle(60, 200)on the specific controller -> Option DQuick Check:
Global config + decorator override = SetThrottlerModule.forRoot({ ttl: 60, limit: 100 })globally and add@Throttle(60, 200)on the specific controller [OK]
- Swapping ttl and limit in decorator
- Misconfiguring global module parameters
- Trying to set global limits with decorators only
Ready to prove it?
Pass the test and this pattern moves unknown
