Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Retry With Backoff

In this video, you added one bounded retry loop to ask(), so a rate limit or a failing server costs the tea importer’s batch a short wait instead of an email, and a failure that retries can’t fix isn’t sent again.

Key Points

  • Backoff, the cap and Retry-After: when a request fails for a moment, wait and send it again, with each wait longer than the last, often twice as long: one second, then two, then four. A cap on the attempts means you give up in the end. When the server sends a Retry-After header, its number of seconds replaces your own wait.
  • The stand-in server: this video’s failures come from a small local stand-in server that forwards real requests to the provider and fails the others with messages of its own, because you can’t make a provider fail on cue. It’s only for the recording, so you don’t need to build it.
  • Without a loop, one 429 stops the batch. With a 429 on the second email, Priya’s draft arrived, then the script stopped with an uncaught error that said retryable: true, which means another try can help, and retryAfter: 2, the header’s two seconds. Marcus’s email got no draft, and Hannah’s was never sent.
  • The retry lives inside ask(), around the provider’s send(), not around each call to ask(), so every feature that calls ask() gets it. sendWithRetries() tries send() up to MAX_ATTEMPTS, four times. A reply comes straight back. An error that isn’t retryable, or a failure on the last attempt, is thrown on to the caller. Otherwise, the loop works out the wait, retryAfter or one second doubling with each attempt, prints a line, and waits. wait() wraps setTimeout() in a promise, so await pauses the loop.
  • A wait over a minute is thrown on too. MAX_WAIT_SECONDS caps how long the loop will wait, because reporting that email beats leaving the batch idle.
  • A billing failure, a wrong key and a call with no response are sent once and never retried, because their retryable isn’t true.
  • The draft script reports a failure and moves on, unless the next email fails the same way. By the time an error reaches the script’s catch, ask() has already retried it if it could. The script reports it with Not drafted and goes on to the next. But the problem might be the key or the account. So lastFailure remembers each failure’s message, and a draft clears it. If the next email fails with the same message, the script throws it, and the batch stops.
  • This rule replaces lesson 14’s stop by status. A 401 now costs one more call before the batch stops, but the rule also catches failures that no list of statuses names.
  • With the loop, Marcus’s 429 cost one retry after the two seconds the header asked for. Hannah’s email got a 500 every time: four attempts with waits of one, two and four seconds, seven seconds in all, then Not drafted. The batch finished with two drafts out of three.
  • One loop, never two. This loop is for fetch(), which sends each request once. A provider’s SDK already retries with backoff, so with an SDK you set its maxRetries option instead. A loop of yours around an SDK’s retries multiplies the requests: with an SDK’s three tries on each of this loop’s four attempts, one 500 would cost twelve requests.

The rule: retry only what the error marks as retryable. Wait the Retry-After seconds when there are some, and a doubling wait when there aren’t. Cap the attempts, then report the failure and move on.

The Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your batch script drafts replies to the tea importer’s inbox. In this video, you’ll make ask() send a failed request again, but only when another try can help.

00:10 You’ve met the pattern before. When a request fails for a moment, you wait and send it again. Each wait is longer than the last, often twice as long: one second, then two, then four.

00:23 That’s backoff. A cap on the attempts means you give up in the end. And when the server sends a Retry-After header, its number of seconds replaces your own wait.

00:35 Nothing acts on a failed call yet. These runs use a small local stand-in server, because you can’t make a provider fail on cue. It sends real requests on to the provider, and fails the others with messages of its own.

00:48 It’s only for this recording, so you needn’t build it. Run the draft script. Priya’s draft arrives. The next call gets a 429, and the whole script stops.

01:01 The error said retryable, which means another try can help, and its retryAfter holds the header’s two seconds. Instead, Marcus’s email has no draft, and Hannah’s was never sent.

01:15 The retry goes inside ask(), around the provider’s send. That way, every feature that calls ask() gets it. ask() now passes the request to sendWithRetries, a new function.

01:28 Its for loop counts the attempts, up to a cap of four. Each attempt calls the provider’s send, and a reply comes straight back.

01:37 When send throws, the if statement decides. An error that isn’t retryable, or a failure on the last attempt, is thrown on to the caller. Otherwise, the loop works out the wait: retryAfter when the server sent one, and if not, one second, doubling with each attempt.

01:55 A wait over a minute is thrown on too, because reporting that email beats leaving the batch idle.

02:01 It prints a line, then waits. The wait function wraps setTimeout in a promise, so await pauses the loop. Then it tries again. That means a billing failure or a wrong key is sent once and never retried.

02:17 The same goes for a call that gets no response at all. In the draft script, a try block now wraps the ask() call. By the time an error gets here, ask() has already retried it if it could.

02:30 So the script reports it, leaves that email undrafted, and moves on. But the problem might be the key or the account. So lastFailure remembers each failure’s message, and a draft clears it.

02:43 If the next email fails with the same message, the script throws it, and the batch stops.

02:49 Run the script with the same 429 on Marcus’s email. Priya’s draft arrives, worded differently this time, and yours will differ too. Then ask() prints Attempt 1, waits the two seconds the header asked for, and Marcus’s draft arrives.

03:06 Next, the stand-in answers Hannah’s email with a 500 every time, and no Retry-After.

03:13 That’s four attempts, with waits of one, two and four seconds, seven in all. The fourth was the last, so the script printed Not drafted with its error. The batch finished with two drafts out of three.

03:28 This loop is for fetch, which sends each request once. A provider’s SDK already retries with backoff, so with an SDK, you set its maxRetries option instead.

03:40 Never both. A loop of yours around an SDK’s retries multiplies the requests. Retry only what the error marks as retryable. Wait the Retry-After seconds when there are some, and a doubling wait when there aren’t.

03:55 Cap the attempts, then report the failure and move on.

04:00 You still haven’t handled a call that’s slow to answer. Each attempt already stops at the adapters’ own 60-second timeout. Next, in the lesson Stop a Call That Takes Too Long, you’ll choose a limit that suits the drafts.

Become a Member to join the conversation.