Design a URL crawler
hard 60 minStart from a few addresses, download pages, find links, and repeat a billion times a month.
Video lesson · Chapter 1 of 5
Fetch one eligible public page
Theory
Read or listen first. About 2 min.
What it is
A web crawler (or spider, or bot) is a program that downloads pages, reads the links in them, and downloads those too. Search engines, web archives, research datasets and site checkers all begin with one.
It is a search over a graph that nobody can see in full. Pages are the nodes, links are the edges, and the graph changes while you walk it.
Why it exists
Nobody has a list of all pages. Links, and the sitemaps that some sites publish, are the only way to find them.
As an interview problem it mixes ideas that are usually met one at a time: a priority queue, speed limits owned by strangers, a set of ten billion members, and a hostile environment. A design with plenty of servers can still fail, because the limit is how fast each site wants to be visited.
A short history
1994
On 30 June, members of the robots mailing list agree on robots.txt, after robots had visited servers where they were not welcome.
2005
Google introduces Sitemaps in June. Other search engines join the next year.
2008
Common Crawl's public dataset of crawled pages goes back to this year.
2022
RFC 9309 makes the Robots Exclusion Protocol an Internet standards track document in September.
Words to know
- Seed
- An address we start from.
- Frontier
- The URLs waiting to be fetched, with the rules for which goes next.
- Host
- The server name in a URL. Speed limits apply to it.
- robots.txt
- A file where a site says which paths a robot may visit.
- Politeness
- Never asking one site for too much, too fast.
- Near duplicate
- Two pages that differ only in small parts, such as a date.
- Spider trap
- A site that can produce new links for ever.
- Conditional request
- Asking for a page only if it changed. The answer 304 means it did not.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Pick. The best URL whose host is ready
Understand it
Explore, mark, queue, share, return.
Scope
What are we building?
A distributed crawler that finds and downloads pages for a search engine: one billion pages a month. It starts with seeds, follows links, stores what it fetched, and returns to pages as they change. Ranking and searching belong to another team.
Discover
Seeds, links and sitemaps lead to new URLs.
Fetch politely
Obey robots.txt and go slowly on each host.
Store once
Compressed pages, with copies recognised.
Return
Often to pages that change, rarely to the rest.
Crawlers you may know
| Name | What it is for |
|---|---|
| Googlebot, Bingbot | Crawlers of the two search engines |
| Apache Nutch | An open-source crawler that runs on Hadoop |
| Heritrix | The Internet Archive's open-source archival crawler |
| Common Crawl | A public collection of crawled pages |
| Screaming Frog | A desktop spider for checking one site |
The hard part
The web sets the pace
We need 386 pages a second on average. One machine could download that. But one host gets at most one request a second, so we must always have over a thousand different hosts ready at peak. A site with ten million pages takes 116 days at that pace, however many servers we own.
The finished system
Where the design ends up
This is the last version. The Design tab starts from one process and one database.
The whole system
Where to read more
Ideas this problem borrows
| Idea | Its home | Used here for |
|---|---|---|
| Work waiting in line | Queues, log-based queues | Fetched pages |
| Split by key | Sharding, consistent hashing | Hosts to machines |
| Speed limits | Rate limiter algorithms | One pace for each host |
| Names to addresses | How DNS works | Resolver cache |
| Large cheap bytes | Object storage | Pages |
| Many small writes | LSM tree | URL records |
| Run it later | Task scheduler | Recrawls |
| Try again safely | Retries, idempotency | Failed fetches |
| What happens next | Distributed search engine | Indexing our pages |
Interview tabs: approach, scope, numbers, API and data, design, deep dives and mock.
Summary
The whole topic in one minute.
A crawler is a loop: pick a URL, check the rules, fetch, store, read links, add the new ones. A thousand fetches a second is little work for modern machines. The waiting is elsewhere: one polite second per host.
So the frontier is the heart. It is split by host, so that one machine alone decides when a host may be visited next. A Bloom filter remembers seen URLs, SimHash spots near copies, conditional requests make second visits cheap, and budgets per host keep traps from eating the crawl.
- The limit is politeness, not servers: one request a second for each host.
- Split the frontier by host, so one owner decides the pace without asking anyone.
- Front queues choose by value, back queues keep hosts apart.
- Each page brings four new URLs, so the queue never empties. Budgets are part of the design.
- Ten billion seen URLs at 0.1% error take 18 GB in a Bloom filter.
- Pack pages into large files. A billion small uploads cost more than the bytes.
- Second visits use conditional requests and a schedule learned from each page.