Skip to content
Back to all posts
TechnologyAPI rate limiting errors

Handling API Rate Limits: Retry-After, Backoff, and Queues

Handle HTTP 429 responses safely with Retry-After, exponential backoff, jitter, idempotency, and shared rate limits across workers.

4 min readUpdated June 28, 2026AI-assisted

TechSilo

AI-assisted and human-curated

An HTTP 429 Too Many Requests response means the server is asking the client to slow down. The limit may apply to an API key, user, IP address, endpoint, or whole account, so the correct fix depends on the provider's policy.

Do not respond by immediately sending the same request again. That can make the limit last longer and create a retry storm.

Read the response before retrying

Check the response body and headers. Some providers return quota details or reset timestamps. The standard Retry-After header can specify a delay in seconds or an HTTP date.

A client should:

1. Respect Retry-After when it is present.

2. Use exponential backoff with jitter when the server gives no retry time.

3. Stop after a bounded number of attempts.

4. Avoid retrying requests that are not safe to repeat unless the API supports idempotency.

5. Keep a concurrency limit so many requests do not all retry together.

A bounded retry helper for GET requests

This Node.js example handles 429 and temporary 503 responses. It is deliberately limited to a GET-style request; do not blindly reuse it for payments or other non-idempotent operations.

js
function retryAfterMs(value) {
  if (!value) return null;

  const seconds = Number(value);
  if (Number.isFinite(seconds) && seconds >= 0) {
    return seconds * 1_000;
  }

  const date = Date.parse(value);
  return Number.isNaN(date) ? null : Math.max(0, date - Date.now());
}

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function getWithRetry(url, { maxRetries = 4, signal } = {}) {
  for (let attempt = 0; ; attempt += 1) {
    const response = await fetch(url, { method: 'GET', signal });

    const retryable = response.status === 429 || response.status === 503;
    if (!retryable || attempt >= maxRetries) {
      return response;
    }

    const serverDelay = retryAfterMs(response.headers.get('retry-after'));
    // If the provider asks for a long wait, return the response so a job queue
    // can reschedule it instead of retrying earlier than the server requested.
    if (serverDelay !== null && serverDelay > 120_000) return response;

    const exponentialDelay = Math.min(30_000, 500 * (2 ** attempt));
    const jitter = Math.floor(Math.random() * 300);
    const delay = serverDelay ?? (exponentialDelay + jitter);

    // Do not keep a response body open while waiting to retry.
    await response.body?.cancel().catch(() => {});
    await sleep(delay);
  }
}

const response = await getWithRetry('https://api.example.com/v1/items');
if (!response.ok) {
  throw new Error(`API request failed: ${response.status}`);
}
const items = await response.json();
console.log(items);

The example caps a single wait at two minutes. If a provider asks for a longer delay, a background queue is usually better than holding a web request open. Production code should also pass an abort signal and define an overall request deadline.

Why exponential backoff needs jitter

If 100 workers all receive a 429 response and wait exactly one second, they may all retry together. That synchronized burst can trigger another limit.

Exponential backoff increases the delay after each failed attempt. Jitter adds a random component so clients spread their retries over time. Always cap the delay and the number of retries; otherwise a temporary provider problem can turn into a large backlog.

A local counter is not a shared rate limiter

A counter stored in a single Node.js process only controls that process. If your application runs four workers or multiple server instances, each counter has its own view and the combined traffic may still exceed the account quota.

For shared limits, use a central store or queue, such as a Redis-backed token bucket, and make the limit key match the provider's policy (for example, API key plus endpoint). Also limit in-flight concurrency; a request-rate limit alone does not stop a small number of very slow requests from exhausting your connection pool.

Avoid retrying unsafe operations

GET requests are generally safe to retry, but POST requests may create duplicate records or charge a payment twice. Retry a non-idempotent operation only when the API documents a safe mechanism, such as an idempotency key, and reuse the same key for the same logical operation.

Also distinguish a rate limit from authentication errors, validation failures, and quota exhaustion. A 401, 403, or 400 response normally needs a different fix, not a retry loop.

MDN's [429 status reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/429) explains the status and Retry-After behavior. Start with the API provider's own quota documentation, because the exact limit and reset policy are provider-specific.

Want to put this into practice?

Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.

Related blog posts