๐๐ผ๐ ๐ ๐ฆ๐๐ผ๐ฝ๐ฝ๐ฒ๐ฑ ๐๐ผ๐๐ถ๐ป๐ด ๐๐ฃ๐ ๐๐ฎ๐น๐น๐ ๐๐ผ ๐ฅ๐ฎ๐๐ฒ ๐๐ถ๐บ๐ถ๐๐
My app dropped user requests. Logs showed 429 errors. My retry logic made it worse. The system stopped.
I used an AI API. It limited me to 50 requests per minute. I tried a simple sleep timer. It blocked my workers. It created bursts of failures.
I tried these methods first:
- Random waits.
- Local retry libraries.
- Token buckets without locks.
None of these worked.
I fixed it with Redis and asyncio. I separated the rate limiter from the retry logic.
It works like this:
- Redis tracks timestamps in a sliding window.
- The app waits if the limit is hit.
- I added exponential backoff.
- I added jitter to stop burst hits.
Follow these rules for your API calls:
- Only retry 429 and 5xx errors.
- Log every retry attempt.
- Use circuit breakers to stop trying after many failures.
Use libraries like aiolimiter or tenacity for production. They are better.
How do you handle rate limits?