1 | ---
|
2 | title: apollo-link-retry
|
3 | description: Attempt an operation multiple times if it fails due to network or server errors.
|
4 | ---
|
5 |
|
6 | Sometimes, you're in an unreliable situation but you would rather wait longer than explicitly fail an operation. `apollo-link-retry` provides exponential backoff, and jitters delays between attempts by default. It does not (currently) handle retries for GraphQL errors in the response, only for network errors.
|
7 |
|
8 | One such use case is to hold on to a request while a network connection is offline and retry until it comes back online.
|
9 |
|
10 | ```js
|
11 | import { RetryLink } from "apollo-link-retry";
|
12 |
|
13 | const link = new RetryLink();
|
14 | ```
|
15 |
|
16 | ## Options
|
17 |
|
18 | The standard retry strategy provides exponential backoff with jittering, and takes the following options, grouped into `delay` and `attempt` strategies:
|
19 |
|
20 | ### options.delay
|
21 |
|
22 | * `delay.initial`: The number of milliseconds to wait before attempting the first retry.
|
23 |
|
24 | * `delay.max`: The maximum number of milliseconds that the link should wait for any retry.
|
25 |
|
26 | * `delay.jitter`: Whether delays between attempts should be randomized.
|
27 |
|
28 | ### options.attempts
|
29 |
|
30 | * `attempts.max`: The max number of times to try a single operation before giving up.
|
31 |
|
32 | * `attempts.retryIf`: A predicate function that can determine whether a particular response should be retried.
|
33 |
|
34 | ### Default configuration
|
35 |
|
36 | The default configuration is equivalent to:
|
37 |
|
38 | ```ts
|
39 | new RetryLink({
|
40 | delay: {
|
41 | initial: 300,
|
42 | max: Infinity,
|
43 | jitter: true
|
44 | },
|
45 | attempts: {
|
46 | max: 5,
|
47 | retryIf: (error, _operation) => !!error
|
48 | }
|
49 | });
|
50 | ```
|
51 |
|
52 | ## Avoiding thundering herd
|
53 |
|
54 | 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.
|
55 |
|
56 | 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.
|
57 |
|
58 | These two features combined help alleviate [the thundering herd problem](https://en.wikipedia.org/wiki/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.
|
59 |
|
60 | ## Custom Strategies
|
61 |
|
62 | 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 arguments (`count`, `operation`, `error`).
|
63 |
|
64 | The `attempts` function should return 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.
|
65 |
|
66 | ```js
|
67 | import { RetryLink } from "apollo-link-retry";
|
68 |
|
69 | const link = new RetryLink({
|
70 | attempts: (count, operation, error) => {
|
71 | return !!error && operation.operationName != 'specialCase';
|
72 | },
|
73 | delay: (count, operation, error) => {
|
74 | return count * 1000 * Math.random();
|
75 | },
|
76 | });
|
77 | ```
|