We are hitting a wall of 429 Too Many Requests errors when applying Terraform configurations that update hundreds of users in a single run. The current retry logic is too aggressive and just gets throttled harder, so we need a solid exponential backoff implementation for the PUT /api/v2/users/{userId} calls. Has anyone got a working snippet for handling this rate limit properly?
Here’s a Python snippet using time.sleep with exponential backoff. It checks for 429, waits, and retries. Don’t forget to add jitter to avoid thundering herd issues.
import time
import requests
def retry_with_backoff(url, payload, max_retries=5):
for attempt in range(max_retries):
response = requests.put(url, json=payload)
if response.status_code != 429:
return response
wait_time = (2 ** attempt) + random.uniform(0, 1)
time.sleep(wait_time)