Design a Web Crawler
Crawl billions of pages politely and efficiently with a URL frontier, deduplication, robots.txt handling, distributed workers and storage.
Advanced · about 4 hours · difficulty 4/5
Learn first: Message Queues and Async Processing, Indexes and Storage Engines, Object Storage and Large Files
A web crawler (the first stage of a search engine, or a price monitoring tool) downloads pages, extracts links and keeps going. It tests your thinking about queues, dedup, politeness and scale.
Requirements
- Start from seed URLs, fetch pages, extract links, store content for indexing.
- Billions of pages, refreshed periodically; some sites much more often than others.
- Politeness: respect robots.txt and do not overload any one host.
- Robust to traps, broken HTML, slow servers and duplicate content.
Core components
- URL frontier: the to-crawl queue. Usually two levels: priority queues (by page importance and freshness need) feeding per-host queues, with each host assigned to one worker so you can enforce a delay between requests to the same host.
- DNS resolver cache: DNS lookups are slow; cache them aggressively.
- Fetchers: many async workers downloading pages with timeouts and size limits.
- Parser: extract text and links, normalise URLs (lowercase host, strip fragments, resolve relative paths).
- URL seen check: a large Bloom filter or a distributed key-value set of hashes, so each URL is crawled once per cycle.
- Content dedup: hash page content (or use SimHash for near-duplicates) to skip mirrors.
- Storage: raw pages in object storage, metadata (URL, last crawled, hash, status) in a wide-column store.
Scaling
Partition the frontier and the seen set by host hash so each worker owns a set of hosts. Scale workers horizontally. Re-crawl scheduling uses how often a page changed in the past.
Traps and safety
Infinite calendars, session IDs in URLs and auto-generated pages create crawler traps. Limit depth per site, URL length and pages per host, and detect repeating patterns.
Pitfalls
- A single global FIFO queue that hammers one host with thousands of parallel requests.
- Ignoring robots.txt and getting blocked or worse.
You are ready to move on when you can explain how your frontier enforces politeness per host while keeping thousands of workers busy.
Key concepts
- URL frontier with priority and host queues
- Politeness and robots.txt
- URL normalisation
- Bloom filter for seen URLs
- Content dedup and SimHash
- DNS caching
- Crawler traps
- Re-crawl scheduling
Mini project
Build a polite crawler that respects robots.txt, uses per-host queues with a 1 second delay, dedups URLs with a Bloom filter and stores pages to disk. Crawl a few thousand pages from a seed set you are allowed to crawl and report pages per second and duplicates skipped.
Check yourself
Why does the URL frontier need per-host queues instead of a single global queue?
Free resources
- About robots.txt (robotstxt.org)
- How Google Search works (Google)
- Common Crawl (Common Crawl)
- Bloom filter (Wikipedia)