Skip to content

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.

  1. 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
    }
}
  1. 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.

Clone this wiki locally