Retry Policy
retry-policy
Practice Problems
Fix a Webhook Reply Sender
Debug an SMS reply webhook handler that sent duplicate texts, left some messages unanswered, and multiplied model calls under rate limits, timeouts, and redeliveries: the tests replay each failure with scripted providers and a fake clock, and you fix each defect with the smallest change.
Bounded Retry Policy
Decide whether to retry a failed provider call: stop for unsafe replay, a permanent failure, used-up attempts, or a deadline that cannot fit the wait, and otherwise return a capped, jittered backoff delay that honors retry-after.
Code Snippets
Retry with Exponential Backoff
Network calls and flaky integrations need a retry wrapper, but plain retries hammer the same endpoint and amplify outages. Exponential backoff doubles the wait between attempts so transient failures recover fast and persistent failures don't DDoS the upstream. This snippet covers the canonical retry, a jittered version that prevents thundering-herd retries across clients, and a policy-driven variant that lets the caller decide which errors are retryable.
Poll Until a Condition Is True
Polling shows up everywhere systems are eventually consistent: waiting for a job status to flip to `done`, for a file to appear, for a deploy to roll out. This snippet walks from a basic fixed-interval poller to one with a deadline, then to exponential backoff with jitter so a thundering herd does not hammer the upstream. Reach for it any time you need to wait for a remote condition without writing the same retry loop again.
AI Engineering
Validate JSON output and recover from failures
Implement a dependency-free validator for a ticket contract, classify each fixture failure by layer, and design a two-attempt recovery that never commits invalid output.
Explain latency, throughput, and rate limits
Explain why a slow model provider raises concurrency and retry load, using latency, throughput, rate limits, deadlines, and a bounded response strategy.
Implement a bounded retry policy
Implement a pure, deterministic retry policy with attempt limits, capped jittered backoff, retry-after, and a deadline check, then fit it into an executor that handles cancellation, partial streams, and uncertain writes.
Debug an integration with partial failures and retries
Find why an SMS reply webhook sent duplicate texts, left some messages unanswered, and multiplied model calls under rate limits, timeouts, and redeliveries. Fix each defect with a regression test, then declare the release check for the fix.
