Design a URL crawler
hard 60 minStart from a few addresses, download pages, find links, and repeat a billion times a month.
How to approach it
What this question tests, where it gets hard, and how to spend the time.
Read the question
What this problem tests
Can you find the real limit?
Bandwidth and CPU are small. Hosts are the scarce thing.
Can you design a queue?
Two orders at once: by value and by host.
Can you say about the same?
Huge sets, near copies, and an error rate you accept on purpose.
Can you expect trouble?
Traps, slow servers, bad data, and sites that say no.
Think
Five questions before you draw
| Ask yourself | Here |
|---|---|
| Who does what? | Operators set seeds and limits; fetchers visit; parsers read; the index team takes pages |
| What must never go wrong? | Flooding a site, or ignoring its rules |
| What is the hardest thing? | Choosing the next URL under two opposing rules |
| What breaks first as it grows? | The single queue, then the set of seen URLs |
| What am I giving up? | Completeness: some pages are skipped, all copies are a little old |
Spend the 45 minutes
| Minutes | Do | Leave with | Skip |
|---|---|---|---|
| 0 to 5 | Purpose, pages a month, politeness, freshness | HTML only, 1B a month | Ranking |
| 5 to 10 | Pages/s, hosts, seen set, bytes | "Hosts are the limit" | CPU to the last core |
| 10 to 15 | Messages, URL record | A key and an owner for each URL | Admin endpoints in full |
| 15 to 25 | One process, then grow | Frontier split by host | Product names |
| 25 to 40 | Frontier, then seen set or recrawl | Choices with costs | SimHash arithmetic, unless asked |
| 40 to 45 | Traps, failures, recap | A clear last sentence | New features |
Lines you can open with
- 1
Start
"Is it for search, an archive, or one site? I will assume search, HTML only, and that being polite is a hard rule."
- 2
Park something
"Rendering JavaScript is a side pool with its own budget. I will return to it if you like."
- 3
When stuck
"Which machine knows when this host may be visited next?"
- 4
Close
"One owner for each host gives politeness, a filter gives memory, a schedule gives freshness. Budgets protect us from the web."
Recognise
Patterns inside this problem
| Pattern | The signal | Where it lands |
|---|---|---|
| Single owner | A rule must hold across machines | Politeness |
| Two-level queue | Two orders at once | Frontier |
| Approximate set | Too many members, small harm | Seen URLs |
| Lease | Workers may vanish | Hosts in flight |
| Learned schedule | Items change at different rates | Recrawl |
| Budget | Input has no end | Pages for each host |
Do
- Clarify scale, politeness and freshness first
- Say that hosts are the limit
- Spend most time on the frontier
- Name the error you accept
Traps
- A global queue with many workers
- A shared rate counter asked on every fetch
- Treating robots.txt as optional
- A table of ten billion rows scanned to find what is due