One slow site stalls your whole crawler. How do you fix it?
Question
Your crawler has 2 workers and one shared queue of URLs. The politeness rule: one request to a site at a time, and a 1-second pause after each one finishes. A worker takes the URL at the head of the queue. If the rule says wait, the worker sleeps, then fetches.
The queue, in order: slow.test/1, slow.test/2, slow.test/3, shop.test/1, shop.test/2, news.test/1. Pages on slow.test take 4 seconds; the others take 1 second.
- When does each site's last page finish?
- Change the design so slow.test can't hold up the other sites, without breaking the rule.
Show hints (3)
- "TRACE THE WORKERS"
At second 0, worker 2 takes
slow.test/2. When may it start? What does worker 1 do when it finishes at second 4? - "WHAT BLOCKS"
Shop and news pages could start at second 0, but they wait in line behind slow.test. What if each site had its own line?
- "WHO PICKS"
Keep one queue per site and the second it may start again. A free worker picks a site that is ready now.
Show checked explanation
With one shared queue, both workers sleep on slow.test and the other pages wait behind it: shop ends at 12 seconds, news at 13. Fix: one queue per site, plus the time each site may start again. A free worker takes a site that is ready now. Then news ends at 2 and shop at 3.
from collections import deque def crawl(sites, workers=2, pause=1): queues = {s: deque(pages) for s, pages in sites.items()} # one per site ready_at = dict.fromkeys(queues, 0) # when each site may start again free_at, done = [0] * workers, {} while any(queues.values()): w = free_at.index(min(free_at)) # the worker that is free first waiting = [s for s in queues if queues[s]] site = min(waiting, key=ready_at.get) # ready now, or soonest start = max(free_at[w], ready_at[site]) free_at[w] = done[site] = start + queues[site].popleft() ready_at[site] = free_at[w] + pause # the pause counts from the end return done print(crawl({"slow.test": [4, 4, 4], "shop.test": [1, 1], "news.test": [1]}))
{'slow.test': 14, 'shop.test': 3, 'news.test': 2}Code run on CPython 3.12 and compared with a separate second-by-second simulation of both designs. Checked by an editor other than the author.
Walkthrough
At second 0, worker 1 starts slow.test/1. Worker 2 takes slow.test/2, but the site is busy, so it sleeps until second 5: the finish at 4 plus the pause. At second 4, worker 1 takes slow.test/3 and sleeps until 10. Both workers wait on one site, and three pages that could run sit behind them. This is head-of-line blocking: one stuck item at the front holds up everything behind it.
| site | pages | shared queue: done at | queue per site: done at |
|---|---|---|---|
| slow.test | 3 × 4 s | 14 | 14 |
| shop.test | 2 × 1 s | 12 | 3 |
| news.test | 1 × 1 s | 13 | 2 |
The fix keeps one FIFO queue per site, plus the second each site may start again: its last finish plus the pause. A site with a fetch running is never ready. At second 0, the workers take slow.test and shop.test. At 1, shop.test is in its pause, so a worker takes news.test. At 2, shop.test is ready again. From 3 on, only slow.test has pages left.
slow.test still ends at 14: its pages and pauses alone take 4 + 1 + 4 + 1 + 4 = 14 seconds. The fix doesn't speed up the slow site. It stops that site from holding the workers.
With thousands of sites, keep the idle sites in a min-heap by ready time. A free worker pops the top site once its time has come. After the fetch, the site goes back in with its new ready time. A busy site isn't in the heap, so no other worker can take it.
Common mistakes
- Counting the pause from the start of a request. With 4-second pages and a 1-second pause, each request starts the moment the last one ends. Count it from the finish.
- Giving each site its own thread. With 50,000 sites that is 50,000 threads, almost all asleep. A small pool that takes any ready site does the same work.
- Treating
shop.testandwww.shop.testas two sites. They may share one server, which then gets twice your rate. Group such names, for example by domain or IP address.
Follow-up questions
- "You run 10 crawler machines. How do you keep the rule?" Send all URLs of a site to one machine, for example by hashing the site name. If two machines fetch from one site, each follows its own timer, and the site gets twice the rate.
- "The site answers 429 or 503. What do you do?" Slow down for that site only: move its ready time later. If the reply has a
Retry-Afterheader (seconds or a date), wait at least that long. If it keeps happening, raise that site's pause.
What a strong answer covers
Traces the shared queue and names head-of-line blocking. Proposes one queue per site, a ready time counted from the finish, and a heap of idle sites. Keeps each site on one machine and backs off on 429 or 503.
Open in interactive practice deck →