Caching and Error Translation in the Resources Layer
Caching and error translation both hide something from callers: where data came from and how a vendor fails. See how the Resources layer handles each.
A rate-limited endpoint turns away ten thousand mobile clients in the same second. Each one waits a second and tries again, then two seconds, then four. The server now meets ten thousand simultaneous requests at every one of those moments. Retries are supposed to give a struggling dependency room to recover. Written carelessly, they become the load that keeps it down.
Exponential backoff lengthens the gap between attempts, which helps. It does nothing about timing. If a thousand callers all began failing at the same instant, they all compute the same schedule, and they stay in step for as long as the outage lasts.
The cure is randomness. If each caller picks its wait from a range instead of a fixed number, the herd thins into a stream. The book names this hazard the "thundering herd" and pairs backoff with jitter for exactly that reason.
Many hand-written versions add jitter at the end: compute the exponential delay, clamp it to the cap, then add a random bonus. The clamp is now a fiction, because the final wait can be larger than the cap. Putting the randomness inside the bound removes the problem by construction.
export function backoffMs(attempt: number, baseMs: number, capMs: number): number {
const ceiling = Math.min(capMs, baseMs * 2 ** attempt); // attempt counts from 0
return Math.floor(Math.random() * ceiling); // 0 up to, never past, ceiling
}
export async function retrying<T>(call: () => Promise<T>, tries = 4): Promise<T> {
for (let attempt = 0; ; attempt++) {
try {
return await call();
} catch (err) {
if (attempt + 1 >= tries || !isRetryable(err)) throw err;
await new Promise((r) => setTimeout(r, backoffMs(attempt, 200, 5_000)));
}
}
}
The argument is short. Math.random() returns a value below 1, so the product is below ceiling, and ceiling is never above capMs. No input can push a wait past the cap, and a test can assert that over a few thousand random draws.
If a zero-millisecond wait bothers you, reserve a minimum: take half the ceiling as a floor and add a random amount up to the other half. The sum still cannot exceed the ceiling. The rule is simply that nothing is added after the clamp.
The book splits this small feature across two layers. Deciding whether an error is transient (a timeout, a 503, a dropped connection) is a pure function of the error, so it belongs in Utils. The loop sleeps and calls out to the world, so it belongs in Resources. In the sketch above, isRetryable would come from L2 and retrying would live in L3. The same logic is easy to unit test in one place and hard to get wrong in the other.
Two further habits come from general practice rather than from LVD, and both are cheap:
Backoff assumes the dependency will be back soon. When it will not, every caller still spends its full budget of waits before giving up. Protecting the rest of the system from a dependency that is down, slow or hogging shared capacity takes other tools, and the book places them in the same Resources-layer section as retry. You can read more about where that layer sits in the Resources layer overview, or in the patterns that keep external systems at arm's length.
Treat a retry loop as code with an invariant, not as a convenience: the wait is random, it grows, and it never exceeds the cap. Keep error classification pure in Utils, keep the sleeping in Resources, and retry at exactly one level.