Design a URL crawler
hard 60 minStart from a few addresses, download pages, find links, and repeat a billion times a month.
Agree on the scope
Ask what changes the design, then commit to a short list.
Clarifying requirements
Interviewer
Let's design a web crawler. What would you ask?
You
First the purpose and the size:
- I assume it feeds a search index.
- 1 billion pages a month, with each month partly new pages and partly returns.
- HTML first. PDFs, images and video can be added as types later. Pages that need JavaScript go to a side pool, since a browser costs far more than a plain request.
- Freshness differs: a news front page in an hour, an archive in months.
Interviewer
What about politeness and the law?
You
Politeness is a hard rule, not a setting:
- Obey robots.txt, before any other request to a host.
- One request a second for each host unless it asks for less.
- Say who we are in the User-Agent, with a page that explains how to opt out. Several IP addresses spread load; they are never used to get around a block.
- Personal data and copyright rules differ by country. I would make hosts and types excludable, and ask lawyers instead of guessing.
Interviewer
How do you discover URLs?
You
Three sources:
- About 10,000 good seeds, such as encyclopedias, news and government sites.
- Links: I assume 80 on a page.
- Sitemaps: a file holds up to 50,000 URLs, with the time of the last change. A hint, not a promise.
- Order is neither pure breadth nor pure depth: by value, with a budget for each host.
Interviewer
Several regions?
You
Yes, later. A Japanese site is closer from Tokyo, which cuts waiting on every round trip and shows what local readers see. Each host belongs to one region, by where its server is. Pages stay there; links and records travel.
Functional requirements
- Find URLs from seeds, links and sitemaps
- Download HTML and store it compressed, with headers
- Obey robots.txt and a pace for each host
- Never fetch the same URL twice by accident; recognise copies
- Visit pages again when they are likely to have changed
- Operators: add seeds, set limits, block a host, force a recrawl
Out of scope for now
- Ranking
- Building and querying the index
- Rendering every page in a browser
- Pages behind a login
Non-functional requirements
| Quality | Target | Why |
|---|---|---|
| Throughput | 1B pages a month: 386/s, 1,157/s at peak | The job |
| Politeness | 1 request a second and 1 connection for each host | Hard rule; being blocked ends the crawl |
| Availability | 99.9% (about 8.8 hours a year) | A pause loses freshness, not data |
| Durability | Frontier and pages survive any one machine | Rebuilding takes months |
| Freshness | 90% of important pages within their interval | Stale results |
| Consistency | Eventual; a double fetch is waste, not damage | No global locks |
| Latency | Not a target for one page; total rate is | A 2 s fetch is fine when thousands run together |
If we must degrade, slow down the fetching first. Never drop a page that was already fetched, and never drop politeness.
Why these questions
Each answer changes the design
| You ask | If the answer is | Then |
|---|---|---|
| How many pages? | 1 million a month | One process and one database |
| One site or the web? | One site | Politeness sets the time; no sharding |
| Keep old versions? | Yes | Append-only files, no overwriting |
| How fresh? | Minutes, for a few pages | A separate fast lane, feeds and sitemaps |
| JavaScript? | All pages | Compute, not hosts, becomes the limit |
| Who may we crawl? | Only partners | No traps; agreed limits |