Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I retry a failed request without retrying every kind of error?

Quick answer

Retry only when the failure is explicitly treated as temporary by the service and repeating the operation is safe. Set a maximum number of attempts and keep delays inside the caller’s deadline. Do not retry every exception or assume a status code has the same meaning for every API. For failures you have not classified, stop and use ordinary error handling or review the service’s guidance. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a request that sometimes fails. How do I retry only the failures that might succeed on another attempt, instead of catching every error and trying again?

Geez

Separate the decision to retry from the code that reports or handles an error. First check whether the specific service describes this failure as transient. Microsoft’s Azure guidance gives HTTP 429 and 5xx responses as typical retry candidates, but says many 4xx responses usually point to a problem another attempt will not fix. That is guidance, not a universal rule for every API, so check the service’s own behavior. [S1]

Then ask whether repeating this particular operation is safe. A repeated request that creates, charges, or otherwise changes something may have a different effect from repeating a read. Guidance recommends considering idempotency because repeating a non-idempotent operation can cause inconsistencies. [S1]

A practical policy has three outcomes: retry, stop and report the failure, or stop and request review because the failure is not classified. This is safer than treating “an error occurred” as permission to repeat.

Follow-up question

What would that look like in code? Please make the stop conditions clear, and don’t assume I’m using a particular API.

Geez

Here is a JavaScript-style sketch. isDocumentedTransient and isSafeToRepeat are deliberately placeholders: implement them from the service’s documentation and the operation’s behavior, not from a blanket rule. maxAttempts includes the first attempt.

async function requestWithRetry(send, options) {
  const {
    maxAttempts,
    isDocumentedTransient,
    isSafeToRepeat,
    delayForAttempt,
    deadlineReached
  } = options;

  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
    try {
      return await send();
    } catch (error) {
      const mayRetry =
        attempt < maxAttempts &&
        isDocumentedTransient(error) &&
        isSafeToRepeat() &&
        !deadlineReached();

      if (!mayRetry) throw error;
      await delayForAttempt(attempt);
    }
  }
}

The stop conditions are visible: the attempt limit is reached, the failure is not known to be transient, repeating is not safe, or the deadline has been reached. In each case, the error is passed to the caller rather than retried. A real implementation must also make sure the delay itself fits within the remaining deadline; this sketch does not calculate that budget.

Follow-up question

How should I choose the delay, and what if lots of requests are failing at once?

Geez

Use a bounded delay policy rather than retrying immediately in a tight loop. Azure-oriented API guidance recommends bounded retries with backoff and jitter, keeping attempts and delays within the request deadline, and limiting aggregate retries. It also suggests considering a circuit breaker when repeated dependency failures could exhaust capacity. [S2]

For example, a local policy might allow at most three total attempts, increase the wait after each failed attempt, add a small randomized variation, and refuse to wait past the caller’s deadline. Those numbers are illustrative placeholders, not universal settings. Choose values for your operation and dependency. If many calls are failing, a per-request attempt limit alone may still allow a large number of retries across the application; consider a shared limit or a circuit breaker as part of the broader design. [S2]

Keep the policy’s output observable in your application’s normal logs or metrics: record the attempt number and the final outcome using your existing conventions. Do not log sensitive request contents just to explain a retry.

Follow-up question

What should I do with failures I can’t confidently classify, and how do I review the policy before enabling it?

Geez

Treat uncertainty as a reason to stop, not as a reason to retry. A status code or exception category is only useful if the specific service documents what it means for this operation. The cited guidance does not establish a universal classification for every API, nor does it determine whether your particular operation is safe to repeat. [S1] [S2]

Review these separately from the retry policy: authentication or input problems, failures with unknown meaning, operations whose repeated effects are unclear, and failures that may need user action. Route them through your ordinary error handling or a review path; do not silently turn them into repeated attempts.

Before enabling retries, test the decision logic with representative errors and confirm that each test stops or retries for the intended reason. That is a suggested review step, not evidence that the service will behave a particular way. Keep the policy configurable so you can adjust it if service guidance or the operation changes.

A practical checklist

  • Confirm the service documents the failure as transient for this operation.

  • Confirm repeating the operation is safe; if uncertain, do not retry.

  • Set a maximum total attempt count and a caller deadline.

  • Use bounded backoff with jitter, and keep each wait within the remaining deadline.

  • Limit aggregate retries where many requests can fail together; consider a circuit breaker.

  • Send unclassified failures to ordinary error handling or review.

A few more useful answers

Should every HTTP 5xx response be retried?

No universal status-code rule is established here. Azure guidance describes 5xx responses as typical candidates, but says to check the specific service’s behavior and retry guidance. [S1]

Is it safe to retry a request that changes data?

Not automatically. Check whether repeating the operation is safe and how the service handles repeated submissions; non-idempotent repeats can cause inconsistencies. [S1]

Does a maximum attempt count prevent overload?

It bounds retries per request, but does not by itself limit retries across many requests. Guidance also recommends limiting aggregate retries and considering a circuit breaker for repeated dependency failures. [S2]

Sources

References used for this guide.

  1. [S1] Transient Fault Handling - Azure Architecture Center
    learn.microsoft.com · Checked
  2. [S2] RESTful Web API Implementation Best Practices - Azure Architecture Center
    learn.microsoft.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work