AlgoViz

Design a URL crawler

hard 60 min

Start 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

0:00 / 4:10

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

  1. 1994

    On 30 June, members of the robots mailing list agree on robots.txt, after robots had visited servers where they were not welcome.

  2. 2005

    Google introduces Sitemaps in June. Other search engines join the next year.

  3. 2008

    Common Crawl's public dataset of crawled pages goes back to this year.

  4. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Pick. The best URL whose host is ready

Understand it

The ants and the tunnels
ABCDEOne ant at the door

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

NameWhat it is for
Googlebot, BingbotCrawlers of the two search engines
Apache NutchAn open-source crawler that runs on Hadoop
HeritrixThe Internet Archive's open-source archival crawler
Common CrawlA public collection of crawled pages
Screaming FrogA desktop spider for checking one site
We design a system like these, not any one of them.

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

Operators add seeds to a frontier split by host. Fetchers lease hosts from the frontier, check a DNS and robots cache, and fetch from websites. Fetched pages go to Kafka. Parsers read them, write packed pages to object storage and page records to Cassandra, and send links back to the frontier, which first checks its Bloom filter. A recrawl planner reads due URLs from Cassandra and puts them in the frontier. A coordinator decides which machine owns each host.
Blue is fetching, pink is discovery, orange is returning to known pages. Dashed means coordination.

Where to read more

Ideas this problem borrows

IdeaIts homeUsed here for
Work waiting in lineQueues, log-based queuesFetched pages
Split by keySharding, consistent hashingHosts to machines
Speed limitsRate limiter algorithmsOne pace for each host
Names to addressesHow DNS worksResolver cache
Large cheap bytesObject storagePages
Many small writesLSM treeURL records
Run it laterTask schedulerRecrawls
Try again safelyRetries, idempotencyFailed fetches
What happens nextDistributed search engineIndexing our pages

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.

Sources