Skip to curriculum
Jump to the editor

14.6. A retry budget shared by many callers

Hard
Topics
35–50 min

Retries are good for one request and dangerous for many. When a service slows down, every caller on the page (a feed, three widgets, a polling badge) fails at once and retries at once, so the service gets two or three times the traffic just when it can least take it. A retry budget caps the retries for everyone together: at most limit retries in any interval ms. A few requests still get their second chance, but a failure can no longer multiply itself.

Write createRetryBudget(options) in retry-budget.js. It returns { run, remaining }. run(fn, callOptions) calls fn and retries its failures, but only while the call has retries left and the shared budget has one to spend. remaining() says how many retries the budget allows right now.

Signature

JAVASCRIPT
function createRetryBudget(options) // returns { run(fn, callOptions), remaining() }
  • options (optional): limit (default 10), the retries allowed in any window, and interval (default 1000), the window length in ms.
  • run(fn, callOptions): calls fn(attempt), with attempt 0, 1, 2… fn may return a promise or a value, or throw. callOptions (optional): retries (default 3), the most retries for this call, and delay (default 100), the wait in ms before each retry. Returns a promise of fn's result.
  • remaining(): a number, limit minus the retries spent in the last interval ms, never below 0.

Requirements

  1. A budget starts with limit retries. A call that succeeds the first time spends nothing.
  2. After a failure, the call retries when it has retries left and the budget has one: it spends one retry at that moment, waits exactly delay ms, and calls fn again.
  3. When the call is out of retries, or the budget is empty, the promise rejects at once (no wait) with the last error, the same object fn failed with.
  4. All calls to run on one budget share it: retries spent by one caller are gone for the others.
  5. A retry spent at time t counts until t + interval: at exactly interval ms later it is back. Each spent retry comes back on its own (a sliding window, not a counter reset every second).
  6. When many calls fail together, exactly limit of them get a retry, in the order they failed, and remaining() never goes below 0.
  7. An error thrown by fn is handled like a rejection. No timer is left once the calls settle, and separate budgets are independent.

Examples

JAVASCRIPT
const budget = createRetryBudget({ limit: 2, interval: 1000 });

budget.run(loadFeed);    // fails at 0 ms: retry 1 spent; retries at 100
budget.run(loadBadge);   // fails at 0 ms: retry 2 spent; retries at 100
budget.run(loadWidget);  // fails at 0 ms: budget empty, rejects at once
budget.remaining();      // 0
JAVASCRIPT
// 1000 ms after the first two retries were spent
budget.remaining(); // 2: both are back
JAVASCRIPT
budget.run(save, { retries: 0 }); // never retries, never spends

Environment

  • Plain JavaScript in a browser page; the tests call the global createRetryBudget from retry-budget.js.
  • The tests run on a virtual clock that replaces setTimeout and Date.now. Read the time with Date.now() and wait with setTimeout.
  • Backoff that grows with each attempt is left out to keep the budget in focus (it is problem 14.4); every retry here waits the same delay.

Hints

  1. A counter that resets every second lets 2 × limit retries through around the reset. What do you need to remember about each retry instead?
  2. Why must the retry be spent before the wait, not after it?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…