Google Apps Script Retry With Exponential Backoff

A partial mashup

Ferris Argyle
Sep 1, 2018 · 1 min read

As you write add-ons which run thousands of times per day, you’ll probably see situations when a backend service such as SpreadsheetApp.getActiveSpreadsheet() occasionally throws an exception. The right way to handle this is with truncated exponential backoff, providing the exception isn’t due to exceeding quota (quota denominators are generally a second or greater; retrying within this time period just ramps load in a negative feedback loop, and retry intervals beyond this have unacceptable user latency).

Peter Hermann provides a sample Javascript implementation, along with a corresponding Apps Script library. So you’re done right? Well, that depends…what if some of the functions which require retry may be passed an argument, or your backoff conditions vary according to the function? The latter is easily addressed; the former requires partials, which we’d reviewed in the the Google Apps Script Libraries And Properties article.

Which brings us to this mashup of Hermann’s sample with partials; it’s generally applicable to Javascript beyond Apps Script:

Retry with truncated exponential backoff

You can use this function like so in your script:

Retry harness snippet

Ferris Argyle

Written by

These are my personal writings; the views expressed in these pages are mine alone and not those of my employer, Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade