Implementing exponential backoff with jitter
You're integrating with a flaky third-party payments API that occasionally returns transient errors. To avoid hammering the service when it's already struggling, you'll implement a retry strategy that uses exponential backoff with full jitter, increasing the wait between attempts while adding randomness so a thundering herd of clients doesn't all retry at the same instant.
A flaky_call(payload) function is available: it fails with TransientError on the first 3 invocations, then succeeds on the 4th. time, random, and TransientError are already loaded.
This exercise is part of the course
Developing applications on AWS
Exercise instructions
- Inside the
tryblock, callflaky_callwithpayloadand return its result. - Compute the exponential cap for this attempt using
baseand the currentattemptcount. - Apply full jitter by picking a random wait between
0andcapusingrandom.uniform().
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
def retry_with_backoff(payload, max_attempts=5, base=0.2):
last_error = None
for attempt in range(max_attempts):
try:
# Try the call
return ____(payload)
except TransientError as err:
last_error = err
# Cap grows exponentially with each attempt
cap = ____ * (2 ** attempt)
# Pick a random wait between 0 and cap (full jitter)
wait = random.____(0, cap)
time.sleep(wait)
# All attempts failed; surface the last error
raise last_error
result = retry_with_backoff({"order_id": 42})
print(result)