Retries and rate limits¶
Retry what can recover, stop at once on what can't, and never push a server harder after it has asked you to slow down.
What is retried¶
| Outcome | Examples | What happens |
|---|---|---|
| Transient | timeouts, dropped connections, truncated bodies, empty bodies, TLS resets, HTTP 408, 425, 429, 500, 502, 503, 504, 520, 521, 522, 523, 524 | Retried with backoff, up to max_retries times (default 5) |
| Throttled | HTTP 429 | Retried, and new requests to that host pause (below) |
| Permanent | any other 4xx (404, 403, 410…), a body that isn't an image, an unusable URL or filename, more than 10 redirects, an untrusted TLS certificate | Recorded immediately. Retrying can't change the answer |
Every reason is listed with its meaning in the failure reasons reference.
How many attempts a URL gets¶
Every limit is per URL and per call (download(), a re-run, a Resume in machyon ui, retry_failed()):
| Failure | Requests in one call |
|---|---|
| Permanent (404, not an image…) | 1 |
| Transient, every time | 1 + max_retries in the pass, then as many again in the final sweep: 12 with the default max_retries = 5, or 6 with retry_failures=False |
| Transient, then fine | Until the first good answer, within those limits |
Nothing retries on its own after a call ends. Running it again (or pressing Resume) is a new call with its own
budget, and finished files are never downloaded again. When a dropped connection was one Machyon had reused,
aiohttp sends the request once more by itself, so a connection failure can cost up to two requests per attempt.
failed_downloads.jsonl records, for each failed row, the requests sent for it so far (attempts), added up across
calls until the row succeeds. A row whose total keeps growing is being asked for on every re-run.
The counters. retries (in the report, the progress snapshot and the CLI's progress line) counts retry
attempts across all rows and both rounds, so it can be much larger than the number of rows still failing: four
links that fail every time make 40 retries with the defaults. How many links are actually waiting for another
attempt is ProgressSnapshot.retrying, shown in machyon ui as Waiting to retry.
The backoff schedule¶
After a transient failure, the row goes back in the queue with a delay. It doesn't hold a concurrency slot while it waits, so other images keep downloading.
cap = min(60 s, backoff × 2^retry)
delay = cap/2 + random(0, cap/2) (and never less than the server's Retry-After)
With the default backoff = 0.5:
| Retry | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Waits | 0.5–1 s | 1–2 s | 2–4 s | 4–8 s | 8–16 s |
The random half ("jitter") spreads retries out, so a thousand rows that failed together don't all come back at the same instant. A large download that dropped mid-body retries within 0.5 s, because it resumes from where it stopped rather than starting over.
The final sweep. When the pass ends, rows that failed transiently get one more chance: Machyon waits a few
seconds, then runs them again with a fresh set of retries. Many outages clear in that time. It runs once per call
and never includes permanent failures. Turn it off with retry_failures=False (on download() and
Downloader.download()).
Rate limits (HTTP 429)¶
A 429 means "this host wants fewer requests". Machyon responds in three ways at once:
- The host pauses. No worker sends anything new to that host until the
Retry-Aftertime has passed: not the retry, and not the other rows waiting their turn, including ones that were already queued for a slot when the 429 arrived. Requests that were already on their way can't be called back; they finish normally, and a 429 among them extends the pause. Both forms are understood, seconds (Retry-After: 30) and an HTTP date. The pause is capped atmax_retry_after(default 120 s) so a misconfigured server can't stall a run for hours. Without aRetry-After, the pause grows with each retry, up to 30 s. - Other hosts keep going. Rows for a paused host are set aside until its pause ends instead of tying up workers, so a run that mixes CDNs isn't held hostage by one of them.
- Concurrency drops and stays lower. When 429s reach 1% of attempts in a measurement window, the adaptive limiter cuts concurrency to 70% and sets a ceiling at 90% of where the trouble began. The ceiling relaxes slowly while things stay calm. Adaptive concurrency
The result on a CDN that throttles above 48 requests in flight, from the benchmarks: Machyon finished all 3,000 images at 301 img/s, while a fixed-pool downloader at its default settings lost 2,995 of them to 429s.
Hedging: no straggler tax¶
At the end of a run the queue is empty and a few slow responses can hold everything up. Once the queue has
drained, any request that has taken longer than max(0.75 s, 2 × the p90 latency) gets a duplicate request.
The first good answer wins and the others are cancelled. A duplicate can stall too; if it hasn't answered within
the same threshold, the request gets another, up to three duplicates.
Those three are for the row's whole life, not for each attempt. Retries, the final sweep, Resume and
machyon retry all continue from what the row has already used, which failed_downloads.jsonl keeps as hedges.
A URL that never answers costs at most 2 × (1 + max_retries) attempts in one call plus three duplicates in
total: 15 requests with the defaults. A new save folder is a new job and starts from zero.
- Hedging only happens at the tail, never while there is still queued work.
- It's budgeted: at most
max(16, 5% of the jobs)duplicates per shard that don't win. A duplicate that wins replaced a stuck request rather than adding one, so it doesn't count. - Racing requests write through separate temporary files, so they can't corrupt each other.
- Turn it off with
hedge=False(--no-hedge) if every extra request counts against a quota.
Being a good citizen¶
Machyon is fast, so it's built to be polite by default: it honours Retry-After, backs off on errors, fetches a
URL that appears on several rows only once, and starts each run from the concurrency it learned last time instead
of rediscovering a host's limits. For large crawls, also identify yourself with a user_agent that includes a
contact address, and cap max_concurrency. Responsible use