Handling HTTP 429 Rate Limit Errors with Retry Strategy in NestJS

241 Views Asked by At

I'm currently integrating a third-party API in my NestJS application and facing challenges with HTTP 429 (Too Many Requests) rate limit errors. My requirement involves making parallel calls to the same API with different data. For larger batches, I'm hitting the rate limit, leading to 429 errors.

Here's a simplified version of my approach:

async fetchData() {
  try {
    const response = await firstValueFrom(
      this.httpService.post('API_ENDPOINT', {}, { headers: {'Content-Type': 'application/json'} }),
    );
    return response.data;
  } catch (error) {
    console.log('error', error);
    return handleError(error);
  }
}

async fetchDataPromises() {
  const promises = [];
  for (let i = 0; i < 25; i++) {
    promises.push(() => this.fetchData());
  }
  return this.waitForAll(promises);
}

handleRejection(promise) {
  return promise().catch((error) => {
    console.error(error);
    return { error };
  });
}

async waitForAll(promises) {
  return Promise.all(promises.map(this.handleRejection));
}

I want to implement a strategy to wait and retry the failed requests until the rate limit is reset, ensuring all requests eventually complete successfully. I'm looking for advice on how to best handle this scenario in a NestJS application.

What are the best practices for managing and retrying requests after encountering a 429 error? Is there a recommended way to implement a delay between retries in NestJS? How can I efficiently manage the retry mechanism for a batch of requests to minimize the overall execution time while respecting the rate limit? Any insights or examples on handling rate limits with retries in NestJS would be greatly appreciated. Thank you!

i was try axios-retry package and nestjs-axios-retry package . its works but i need best way to do it in large dataset

0

There are 0 best solutions below