0
0
NestJSframework~20 mins

Rate limiting with throttler in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Throttler Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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';
  }
}
AThe server crashes due to exceeding the request limit.
BThe 6th request is blocked and the server responds with HTTP 429 Too Many Requests error.
CThe 6th request is accepted and returns 'Success' like the previous requests.
DThe 6th request is delayed by 10 seconds before responding with 'Success'.
Attempts:
2 left
💡 Hint
Think about what HTTP status code is standard for rate limiting.
📝 Syntax
intermediate
2: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?
A
app.useGlobalGuards(app.get(ThrottlerGuard));
// and in module imports: ThrottlerModule.forRoot({ ttl: 60, limit: 10 })
Bapp.useGlobalGuards(Throttle({ limit: 10, ttl: 60 }));
C
app.useGlobalGuards(new ThrottlerGuard());
app.use(Throttle({ limit: 10, ttl: 60 }));
Dapp.useGlobalGuards(new ThrottlerGuard({ limit: 10, ttl: 60 }));
Attempts:
2 left
💡 Hint
Remember that ThrottlerModule.forRoot sets global config and ThrottlerGuard is applied globally.
🔧 Debug
advanced
2: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';
}
AThe limit and ttl values must be strings, not numbers.
BThe custom decorator syntax is invalid and causes a runtime error.
CThe controller method must be decorated with @Throttle instead of a custom decorator.
DThe ThrottlerGuard does not read the 'throttle' metadata key; it expects 'throttle' to be set via @Throttle decorator.
Attempts:
2 left
💡 Hint
Check what metadata key the official @Throttle decorator sets.
state_output
advanced
2: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';
  }
}
A5
B3
C4
D2
Attempts:
2 left
💡 Hint
The header counts how many requests remain after the current one.
🧠 Conceptual
expert
3: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?
AIt uses an external shared store like Redis to keep request counts synchronized across instances.
BEach instance tracks requests independently, so limits apply per instance, not globally.
CIt uses in-memory storage with periodic synchronization between instances automatically.
DIt disables rate limiting automatically when multiple instances are detected.
Attempts:
2 left
💡 Hint
Think about how to share state across servers in a cluster.