Maximum Retry Attempts and Timeout Budgets
What you'll learn: How to prevent retry logic from running forever by setting hard limits on attempts and total time spent.
The Problem with Unlimited Retries
You've learned exponential backoff with jitter to space out retry attempts intelligently. But what happens if a downstream service stays down for an extended period? Without limits, your application could retry indefinitely, blocking threads, exhausting connection pools, and degrading user experience.
Think of it like knocking on a neighbor's door. Exponential backoff tells you to wait longer between knocks, but maximum retry attempts tells you "after 5 knocks, give up and leave a note instead." Meanwhile, a timeout budget says "if you've been standing here for 30 seconds total, move on regardless of how many times you knocked."
Two Essential Limits
Maximum Retry Attempts caps the number of tries:
maxRetries = 3
// Try once, then up to 3 retries = 4 total attempts
Timeout Budget caps total elapsed time:
timeoutBudget = 10 seconds
// Even if you have retries left, stop at 10s total
These work together. You might exhaust your retry count first (say, 5 quick attempts), or hit your time budget first (if exponential backoff pushes delays too far). Whichever limit is reached first triggers graceful failure—returning an error to the caller or falling back to cached data.
Why Both Matter
Retry counts protect against fast-failing errors (like immediate connection refused), while timeout budgets protect against slow failures (hanging requests, degraded services). Together, they ensure bounded resource usage and predictable latency.
Key Takeaway:
Always set both maximum retry attempts and a timeout budget to prevent retry logic from blocking indefinitely and exhausting system resources.