Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free

Retry Link

Attempt an operation multiple times if it fails due to network or server errors.


Overview

@apollo/client/link/retry can be used to retry an a certain amount of times. This comes in handy when dealing with unreliable communication situations, where you would rather wait longer than explicitly fail an operation. @apollo/client/link/retry provides exponential backoff, and jitters delays between attempts by default.

Note: It does not currently handle retries for errors in the response, only for network errors; the onError link can be used to retry an operation after a GraphQL error. For more information, see the Error handling documentation.

An example use case is to hold on to a request while a network connection is offline, and retry until it comes back online.

import { RetryLink } from "@apollo/client/link/retry";
const link = new RetryLink();

Options

The standard retry strategy provides exponential backoff with jittering, and takes the following options, grouped into delay and attempt strategies:

options.delay

OptionDescription
delay.initialThe number of milliseconds to wait before attempting the first retry.
delay.maxThe maximum number of milliseconds that the link should wait for any retry.
delay.jitterWhether delays between attempts should be randomized.

options.attempts

OptionDescription
attempts.maxThe max number of times to try a single operation before giving up.
attempts.retryIfA predicate function that can determine whether a particular response should be retried.

Default configuration

The default configuration is equivalent to:

new RetryLink({
delay: {
initial: 300,
max: Infinity,
jitter: true
},
attempts: {
max: 5,
retryIf: (error, _operation) => !!error
}
});

Avoiding thundering herd

Starting with initialDelay, the delay of each subsequent retry is increased exponentially, meaning it's multiplied by 2 each time. For example, if initialDelay is 100, additional retries will occur after delays of 200, 400, 800, etc.

With the jitter option enabled, delays are randomized anywhere between 0ms (instant), and 2x the configured delay. This way you get the same result on average, but with random delays.

These two features are combined to help alleviate the thundering herd problem, by distributing load during major outages. Without these strategies, when your server comes back up it will be hit by all of your clients at once, possibly causing it to go down again.

Custom strategies

Instead of the options object, you may pass a function for delay and/or attempts, which implement custom strategies for each. In both cases the function is given the same (count, operation, error).

The attempts function should return a boolean (or a Promise which resolves to a boolean) indicating whether the response should be retried. If yes, the delay function is then called, and should return the number of milliseconds to delay by.

import { RetryLink } from "@apollo/client/link/retry";
const link = new RetryLink({
attempts: (count, operation, error) => {
return !!error && operation.operationName != 'specialCase';
},
delay: (count, operation, error) => {
return count * 1000 * Math.random();
},
});
Previous
REST
Next
Schema
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company