Challenge - 5 Problems
Throttler Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a user exceeds the rate limit in NestJS Throttler?
Consider a NestJS controller using the ThrottlerGuard with a limit of 5 requests per 10 seconds. What is the expected behavior when a user sends the 6th request within 10 seconds?
NestJS
import { Controller, Get, UseGuards } from '@nestjs/common'; import { ThrottlerGuard, Throttle } from '@nestjs/throttler'; @Controller('test') @UseGuards(ThrottlerGuard) export class TestController { @Get() @Throttle(5, 10) getTest() { return 'Success'; } }
Attempts:
2 left
💡 Hint
Think about what HTTP status code is standard for rate limiting.
✗ Incorrect
NestJS ThrottlerGuard automatically blocks requests exceeding the limit and returns HTTP 429 status code to indicate too many requests.
📝 Syntax
intermediate2:00remaining
Which option correctly applies a global rate limit of 10 requests per minute using NestJS Throttler?
You want to set a global rate limit of 10 requests per 60 seconds for all routes in your NestJS app. Which code snippet correctly configures this?
Attempts:
2 left
💡 Hint
Remember that ThrottlerModule.forRoot sets global config and ThrottlerGuard is applied globally.
✗ Incorrect
The correct way is to import ThrottlerModule with config and apply ThrottlerGuard globally via app.useGlobalGuards with the guard instance from the app context.
🔧 Debug
advanced2:00remaining
Why does the custom rate limit decorator not work as expected?
You wrote a custom decorator to apply a rate limit of 3 requests per 5 seconds on a controller method, but the limit is not enforced. What is the likely cause?
NestJS
import { SetMetadata } from '@nestjs/common'; export const CustomThrottle = () => SetMetadata('throttle', { limit: 3, ttl: 5 }); // Usage: @CustomThrottle() @Get() getData() { return 'data'; }
Attempts:
2 left
💡 Hint
Check what metadata key the official @Throttle decorator sets.
✗ Incorrect
NestJS ThrottlerGuard looks for metadata key 'throttle' set by the official @Throttle decorator. Custom decorators must set the same key and format or extend the guard to read custom keys.
❓ state_output
advanced2:00remaining
What is the value of remaining requests after 2 calls in 10 seconds with limit 5?
Given a NestJS controller method with @Throttle(5, 10), after a user makes 2 successful calls within 10 seconds, what is the value of the 'X-RateLimit-Remaining' header in the response of the 2nd call?
NestJS
import { Controller, Get, UseGuards } from '@nestjs/common'; import { ThrottlerGuard, Throttle } from '@nestjs/throttler'; @Controller('api') @UseGuards(ThrottlerGuard) export class ApiController { @Get('data') @Throttle(5, 10) getData() { return 'ok'; } }
Attempts:
2 left
💡 Hint
The header counts how many requests remain after the current one.
✗ Incorrect
The limit is 5 requests per 10 seconds. After 2 calls, 3 remain. But the header shows remaining after the current request, so after the 2nd call, 4 remain.
🧠 Conceptual
expert3:00remaining
How does NestJS Throttler handle distributed rate limiting in a multi-instance setup?
In a production environment with multiple NestJS app instances behind a load balancer, how does the ThrottlerModule ensure consistent rate limiting across all instances?
Attempts:
2 left
💡 Hint
Think about how to share state across servers in a cluster.
✗ Incorrect
NestJS Throttler supports using external stores like Redis to share request counts, enabling consistent rate limiting across multiple app instances.