Comece agoraComece grátis

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.

Este exercicio faz parte do curso

Developing applications on AWS

Ver curso

Instruções do exercicio

  • Inside the try block, call flaky_call with payload and return its result.
  • Compute the exponential cap for this attempt using base and the current attempt count.
  • Apply full jitter by picking a random wait between 0 and cap using random.uniform().

exercicio interativo prático

Tente este exercicio completando este código de exemplo.

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)
Editar e Executar Código