Make a Retry Helper Testable With Backoff and Jitter
Last updated: November 29, 2026
Quick Overview
Write a retry helper for calls to a flaky internal RPC that uses capped exponential backoff with full jitter, retries only transient errors, and can be unit tested without real sleeping.
0
0
0 solved
Write a retry helper for calls to a flaky internal RPC that uses capped exponential backoff with full jitter, retries only transient errors, and can be unit tested without real sleeping.
Google's SRE guidance stresses backoff with jitter to avoid retry storms, and a retry loop is a quick way to check both correctness and testability. Interviewers look for selective retries, a cap, jitter, and an injectable sleep and random source.
What the Interviewer Expects
- Retries only transient error types
- Uses exponential backoff with a cap
- Adds jitter to avoid synchronized retries
- Injects sleep and randomness for tests
- Preserves the original error as the cause
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Why can retries make an outage worse, and how do retry budgets help?
- How would you respect a deadline propagated from the caller?
- Should every layer of a call stack retry?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
retry(fn, attempts, base, cap)calls fn up toattemptstimes.- Retry on ConnectionError and TimeoutError only; other errors propagate immediately.
- Delay before retry k is uniform in [0, min(c...
High-Level Design
A loop over attempts. On a retryable error, sleep for the jittered delay unless this was the last attempt. sleep and rand are parameters with production defaults, so tests can pass sleeps.append...