Sub topic 7
Deciding what is safe to retry
This chapter assumes you can build an HttpClient request, use catchError, and know the retry operator from the previous chapter. Here you decide when a retry is a good idea and when it does harm.
Why a retry can hurt
retry resubscribes to the source Observable. For HttpClient, that sends the request again. The server may have already done part or all of the work before the failure reached you. A retry then repeats that work.
Consider a POST /api/orders call that times out. The server may have created the order and only the response was lost. Retrying creates a second order and charges the customer twice. A retry on GET /api/orders/7 is harmless, because reading data twice changes nothing.
Two questions to ask
Every retry rule answers two questions:
- Is the request safe to send again? This depends on the HTTP method.
- Is the failure likely to go away on its own? This depends on the status code.
Retry only when both answers are yes.
The method check
An operation is idempotent when running it many times has the same effect as running it once. The HTTP specification defines these methods as idempotent: GET, HEAD, OPTIONS, PUT and DELETE. POST and PATCH are not guaranteed to be.
GET, HEAD and OPTIONS are also safe, meaning they do not change server state. PUT and DELETE are idempotent in the specification, but your server may not follow it. A DELETE that returns 404 on the second call can confuse your error handling. A conservative rule is to retry safe methods only, and to add PUT or DELETE after you confirm the server behaves correctly.
If a POST must be retried, make it idempotent first. Send an idempotency key header, which the server uses to recognize and ignore duplicates. This only works if the backend supports it.
The status check
| Status | Meaning | Retry? |
|---|---|---|
| 0 | Network failure, no response | Yes |
| 408 | Request timeout | Yes |
| 429 | Too many requests | Yes, with a longer delay |
| 500, 502, 503, 504 | Server or gateway trouble | Yes, usually |
| 400, 401, 403, 404, 422 | The request itself is wrong or not allowed | No |
A 4xx error means the same request will fail the same way. Retrying only adds load. A 500 is a judgment call. It may be a temporary fault or a permanent bug, so keep the retry count small.
For 429 and 503, the server may send a Retry-After header with the wait time in seconds. Read it when you can, and wait at least that long.
A reusable predicate
Put the rule in one function so every retry in your app follows the same policy.
import { HttpErrorResponse } from '@angular/common/http';
const SAFE_METHODS = new Set(['GET', 'HEAD', 'OPTIONS']);
const TRANSIENT_STATUSES = new Set([0, 408, 429, 500, 502, 503, 504]);
export function isRetryable(method: string, error: unknown): boolean {
return (
error instanceof HttpErrorResponse &&
SAFE_METHODS.has(method) &&
TRANSIENT_STATUSES.has(error.status)
);
}You can use it with retry in a service. Rethrowing from delay stops the retries at once and passes the original error on.
import { HttpClient } from '@angular/common/http';
import { Injectable, inject } from '@angular/core';
import { Observable, retry, throwError, timer } from 'rxjs';
import { isRetryable } from './is-retryable';
@Injectable({ providedIn: 'root' })
export class ProductService {
private http = inject(HttpClient);
getProducts(): Observable<string[]> {
return this.http.get<string[]>('/api/products').pipe(
retry({
count: 2,
delay: (error: unknown) =>
isRetryable('GET', error) ? timer(500) : throwError(() => error),
})
);
}
}The next chapter replaces the fixed 500 ms wait with exponential backoff.
Common mistakes
- Retrying every error with a bare
retry(3). This repeats 404 and 403 responses that cannot succeed. - Retrying POST without an idempotency key, which risks duplicate records.
- Using a large
countwith no delay. Many clients doing this at once can overload a server that is already struggling.
Exercise
A POST /api/payments request fails with status 503. Should your retry policy resend it, and what would make it safer?
Show answer
By default, no. POST is not idempotent, and the server may have processed the payment before the 503 was returned, so a retry could charge twice. It becomes safe to retry only if the request carries an idempotency key that the server uses to ignore duplicates.