Retries
The Apify client automatically retries requests that fail due to:
- Network errors
- Internal errors in the Apify API (HTTP status codes 500 and above)
- Rate limit errors (HTTP status code 429)
By default, the client retries a failed request up to 8 times. The retry intervals use an exponential backoff strategy:
- The first retry occurs after 500 to 1,000 milliseconds.
- The second retry occurs after 1,000 to 2,000 milliseconds, and so on.
The delay is spread over a random factor between one and two, so clients that failed together don't come back at the API in lockstep.
You can customize this behavior using the following options in the ApifyClient constructor:
maxRetries: Defines the maximum number of retry attempts.minDelayBetweenRetriesMillis: Sets the minimum delay between retries in milliseconds.
Retries with exponential backoff help reduce the load on the server and increase the chances of a successful request.
import { ApifyClient } from 'apify-client';
const client = new ApifyClient({
token: 'MY-APIFY-TOKEN',
maxRetries: 8,
minDelayBetweenRetriesMillis: 500,
});
When the retries are exhausted, the client throws the error of the final attempt. Which transport errors count as retryable is decided by the HTTP client the requests go through. The built-in axios client retries every network failure, timeouts included. A custom client classifies its own errors. For details, see HTTP clients.
A request that times out is retried the same way, with a longer timeout on each attempt. For how the timeouts are set, see Timeouts.