Glossary
Exponential backoff: waiting longer after each failure
Exponential backoff is a retry strategy where the wait before each new attempt grows by a constant factor, usually doubling, often with random jitter added so many clients do not retry in lockstep.
Why fixed delays fall short
When a server is overloaded or rate limiting you, retrying immediately makes things worse. Retrying after a fixed delay helps a little, but if a thousand clients all failed at the same moment, they all come back at the same moment too, and the server falls over again. Growing the delay gives the server room to recover; randomizing it spreads the returning traffic out.
The formula
A common form is: wait = min(cap, base × 2attempt), then add jitter. With a base of 1 second and a cap of 60 seconds, the waits are about 1, 2, 4, 8, 16, 32, 60, 60 seconds.
Jitter comes in a few flavors. "Full jitter" picks a random value between zero and the computed wait, which spreads clients most evenly. "Equal jitter" keeps half the wait and randomizes the other half. Either is far better than none.
import random
def backoff_delays(base=1.0, cap=60.0, tries=6):
for n in range(tries):
ceiling = min(cap, base * (2 ** n))
yield random.uniform(0, ceiling) # full jitter
for pause_s in backoff_delays():
print(f"next attempt in {pause_s:.1f}s")
What to retry
| Failure | Retry with backoff? |
|---|---|
| Connection reset, timeout | Yes, for idempotent calls |
| 429 Too Many Requests | Yes; honor Retry-After if present |
| 502, 503, 504 | Yes |
| 400, 404, 422 | No; the request itself is wrong |
| 401, 403 from an API | No; fix credentials or permissions |
| 403 challenge page from a website | Not by waiting alone; solve the challenge |
When a server sends Retry-After, use that value instead of your own calculation, or the larger of the two. Always set a maximum number of attempts and give up cleanly. Pair this with a sensible timeout on each call, so a hung request counts as a failure instead of stalling the loop.
Backoff with the GateSolver API
For the solving API itself, two error codes call for backoff: ERROR_RATE_LIMIT, which means you went past 600 requests per minute on your key, and ERROR_SERVICE_UNAVAILABLE, a temporary condition. Back off and retry those. Others are not timing problems: ERROR_ZERO_BALANCE needs a top up, ERROR_KEY_DENIED_ACCESS needs a correct key, ERROR_INVALID_TASK_DATA names the field to fix. Error responses are not billed, so backoff costs only time (see HTTP 429 for the website side of the same problem).
Do not confuse backoff with the normal polling rhythm. Polling getTaskResult every 3 seconds for a token task (or every second for an image task) while the status is processing is expected behavior, not a retry. Apply backoff when calls fail, not while a task is simply still working. A AWS WAF task typically finishes under 3 seconds, well within the normal polling pattern.
Backoff toward target sites
For the sites you scrape, backoff per host keeps you polite: one slow or rate limiting host gets longer delays while the rest of the crawl continues. Combine it with a request queue that can hold a URL until its retry time, and with per host rate limits.
Common questions
What is jitter in exponential backoff?
A random amount added to or replacing part of each delay, so many clients do not retry at the same instant.
Should I retry a 404 with backoff?
No. The resource does not exist, so waiting will not help. Log it and move on.
Which GateSolver errors deserve backoff?
ERROR_RATE_LIMIT and ERROR_SERVICE_UNAVAILABLE. Other codes point to input, key or balance problems to fix.
Is polling getTaskResult a form of backoff?
No. Polling at a steady interval while a task is processing is normal; backoff applies when calls fail.
What cap should I set on the delay?
Something like 30 to 60 seconds is common, together with a maximum number of attempts.
Keep reading
- Rate limitingCaps on requests per IP, key or session, the 429 response that enforces them, and the limit GateSolver applies to its own API.
- IdempotencyThe property that repeating an operation has the same effect as doing it once, and why it decides which retries are safe.
- How do I avoid HTTP 429 Too Many Requests?A 429 means you crossed a rate limit; slow down, honor Retry-After, back off with jitter and spread load across sessions.
- Error handling for CAPTCHA APIsSort every CAPTCHA API error into retry, fix or stop, follow those rules in code, and know which outcomes cost nothing.