К содержанию
Статья ещё не опубликована на языке Russian. Показана версия на English.

Resilient Retries: Exponential Backoff, Jitter, Non-Retryable Errors, and DLQs

2 мин на чтение

Naive retries turn minor downstream hiccups into full-blown outages. If 5,000 jobs fail simultaneously at 12:00:00 and all retry on a fixed 5-second timer, they hit the recovering upstream API together as a synchronized thundering herd at 12:00:05, knocking it right back down.

Classifying Errors Before Retrying

Never retry every exception blindly. Classify every external failure into one of three buckets:

Error ClassExamplesCorrect Action
Transient / RetryableHTTP 429, 502, 503, 504, ECONNRESET, socket timeoutRetry with exponential backoff + full jitter (honor Retry-After)
Permanent / Non-RetryableHTTP 400 bad template, 401 revoked token, invalid phone number, user opted outFail immediately; mark non-retryable; never waste attempts
Policy / Quota PauseTenant daily cap hit, provider account paused, billing balance zeroPause batch or open circuit breaker; alert workspace owner

Exponential Backoff With Full Jitter

Full jitter spreads retry traffic uniformly across [0, min(cap, base * 2^attempt)], minimizing collision spikes while preserving fast recovery on early attempts:

ts
export function computeBackoffWithJitterMs(input: {
  attempt: number;
  baseMs?: number;
  maxMs?: number;
  retryAfterMs?: number;
}): number {
  const base = input.baseMs ?? 500;
  const max = input.maxMs ?? 60_000;
  if (input.retryAfterMs && input.retryAfterMs > 0) {
    return Math.min(max, input.retryAfterMs + Math.floor(Math.random() * 500));
  }
  const ceiling = Math.min(max, base * Math.pow(2, Math.max(0, input.attempt - 1)));
  return Math.floor(Math.random() * ceiling);
}

Dead-Letter Queues (DLQ) and Selective Replay

When a job exhausts its maximum retry attempts, move it to an inspectable terminal state (DLQ / failed-items table) preserving:

  • Exact normalized failure code and sanitized provider error payload.
  • Input reference and idempotency key.
  • Timestamp and attempt count.

Always support failed-only selective replay so operators can fix an expired credential or template typo and re-run only the failed subset without ever re-sending to recipients who already succeeded.

Next: orchestrate multi-day human interactions cleanly in Human-Timed Durable Workflows.

Было ли это полезно?