14.6. A retry budget shared by many callers
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#
function createRetryBudget(options) // returns { run(fn, callOptions), remaining() }options(optional):limit(default 10), the retries allowed in any window, andinterval(default 1000), the window length in ms.run(fn, callOptions): callsfn(attempt), withattempt0, 1, 2…fnmay return a promise or a value, or throw.callOptions(optional):retries(default 3), the most retries for this call, anddelay(default 100), the wait in ms before each retry. Returns a promise offn's result.remaining(): a number,limitminus the retries spent in the lastintervalms, never below 0.
Requirements#
- A budget starts with
limitretries. A call that succeeds the first time spends nothing. - 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
delayms, and callsfnagain. - 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
fnfailed with. - All calls to
runon one budget share it: retries spent by one caller are gone for the others. - A retry spent at time
tcounts untilt + interval: at exactlyintervalms later it is back. Each spent retry comes back on its own (a sliding window, not a counter reset every second). - When many calls fail together, exactly
limitof them get a retry, in the order they failed, andremaining()never goes below 0. - An error thrown by
fnis handled like a rejection. No timer is left once the calls settle, and separate budgets are independent.
Examples#
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// 1000 ms after the first two retries were spent
budget.remaining(); // 2: both are backbudget.run(save, { retries: 0 }); // never retries, never spendsEnvironment#
- Plain JavaScript in a browser page; the tests call the global
createRetryBudgetfromretry-budget.js. - The tests run on a virtual clock that replaces
setTimeoutandDate.now. Read the time withDate.now()and wait withsetTimeout. - 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#
- A counter that resets every second lets 2 ×
limitretries through around the reset. What do you need to remember about each retry instead? - 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