-
-
Notifications
You must be signed in to change notification settings - Fork 194
AWS SDK v3 Client Rate Limiter
Roman edited this page Aug 23, 2025
·
6 revisions
Some aws endpoints have a rather punishing rate limit, so client side rate limiting is a good idea, especially if you're planning on scaling out client calls across multiple threads. Thankfully, the @smithy/util-retry package provides a way to do integrate with rate-limiter-flexible.
- To do so, first create a new class that implements the RateLimiter interface from the @smithy/util-retry package:
import { RateLimiter, } from '@smithy/util-retry';
import { RateLimiterAbstract, RateLimiterRes } from 'rate-limiter-flexible';
import { setTimeout } from 'timers/promises';
export class CustomRateLimiter implements RateLimiter {
private readonly flexibleRateLimiter: RateLimiterAbstract;
constructor(flexibleRateLimiter: RateLimiterAbstract) {
this.flexibleRateLimiter = flexibleRateLimiter;
}
public async getSendToken() {
let success = false;
do {
try {
await this.flexibleRateLimiter.consume(1);
success = true;
} catch (error) {
if (error instanceof RateLimiterRes) {
await setTimeout(error.msBeforeNext);
} else {
throw error;
}
}
} while (!success);
}
public updateClientSendingRate(_response: any) {
// We don't need to do anything here, because the rate limiter handles the backoff
}
}- Then when creating the client, you can create a new instance of the AdaptiveRetryStrategy and pass in the rate limiter:
import { RateLimiterMemory } from 'rate-limiter-flexible';
const client = new MediaConnectClient({
retryStrategy: new AdaptiveRetryStrategy(async () => 10, {
rateLimiter: new CustomRateLimiter(new RateLimiterMemory({
points: 5,
duration: 1,
}))
})
})Obviously, a memory based rate limiter is not a good idea for production, using a database rate-limiter, like RateLimiterDrizzle, RateLimiterValkey or other database limiter from rate-limiter-flexible, is a better idea.
Get started
Middlewares and plugins
Migration from other packages
Limiters:
- Cluster
- Drizzle
- DynamoDB
- Etcd
- Memcached
- Memory
- MongoDB (with sharding support)
- MySQL
- PM2 Cluster
- PostgreSQL
- Prisma
- Redis
- SQLite
- Valkey: iovalkey and Valkey Glide
- BurstyRateLimiter
- RateLimiterUnion
- RateLimiterQueue
Wrappers:
- AWS SDK v3 Client Rate Limiter
- RLWrapperBlackAndWhite Black and White lists
- RLWrapperTimeouts Timeouts
Knowledge base:
- Block Strategy in memory
- Insurance Strategy
- Periodic sync to reduce number of requests
- Comparative benchmarks
- Smooth out traffic peaks
-
Usage example
- Minimal protection against password brute-force
- Login endpoint protection
- Websocket connection prevent flooding
- Dynamic block duration
- Different limits for authorized users
- Different limits for different parts of application
- Block Strategy in memory
- Insurance Strategy
- Third-party API, crawler, bot rate limiting