Problem Description

The Thundering Herd

Difficulty: MEDIUMID: rate-limit-001

The Scenario

You are building a data scraper that hits a third-party API. The API is unstable and frequently returns 429 Too Many Requests.

Your current implementation attempts to handle this by recursively calling the function again. However, this is causing a RecursionError and crashing the application when the API is under heavy load (The Thundering Herd problem).

The Goal

Refactor the fetch_data function to handle rate limits gracefully.

  • Use the tenacity library (already installed) to implement exponential backoff.
  • Stop retrying after 5 attempts.
  • Reraise the exception if it still fails after 5 attempts.
solution.py
Loading...
SYSTEM_LOGS
// Waiting for execution trigger...