Design a Web Crawler
hardA web crawler discovers and downloads web pages so a search engine or archive can index them. Google's crawler, Heritrix, Common Crawl, Bing's bot share the architecture. Interesting problems: politeness, scale, crawl traps, freshness.
Key Concepts
hash(domain), each partition is a priority queue backed by RocksDB or Kafka. Dedup against a Bloom filter first, then definitive check.?date=YYYY-MM-DD for every day forever), infinite redirect chains. Detect via URL pattern heuristics, throttle or block.High-level design
Seed → frontier (partitioned by domain, prioritized).
Fetcher pool → DNS cache → robots.txt cache → HTTP fetch → store HTML.
Parser → outlink extraction → normalization → dedup check → enqueue.
Parser → content extraction → indexing pipeline (separate system).
Recrawl scheduler → re-enqueue pages by change-rate model.
Optional headless fallback for JS-rendered pages.
Components
- Frontier (Kafka with domain-partitioned queues, or sharded priority queue in RocksDB).
- Fetcher pool (async HTTP).
- DNS resolver + cache.
- Robots.txt cache (TTL per domain).
- Parser (Beautiful Soup / lxml / native).
- Canonical URL builder.
- Dedup: Bloom filter + SimHash for near-dup.
- Storage: HTML compressed in object store, metadata in KV.
- Link graph store (for PageRank, recrawl decisions).
- Recrawl scheduler.
- Headless browser pool (Puppeteer/Playwright) for SPA fallback.
Politeness
Per-domain delay: robots.txt Crawl-delay directive, or default 1-10s.
One concurrent connection per domain (or low N).
Respect 429 / 503 responses with exponential backoff.
Honor robots.txt strictly — both for ethics and to avoid being banned.
User-Agent identifies your crawler with contact info.
Some sites whitelist specific bots (Googlebot, Bingbot) and may serve different content.
Dedup
URL normalization: lowercase host, sort query params, strip fragment, canonical form.
Bloom filter for 'have we seen this URL?'.
SimHash on content (~64-bit fingerprint of token n-grams) for near-duplicate detection.
MinHash + LSH for cluster-level near-dup at billion scale.
Mirror detection: same content at many hosts; pick canonical version.
Trade-offs
Politeness limits throughput — accept and parallelize across domains.
Frontier in memory: fast but bounded; disk-backed (RocksDB) for scale.
Dedup early (Bloom filter) vs late (content SimHash) — both, in sequence.
Headless browsing: 100x cost, necessary for SPAs.
Aggressive recrawl: fresher index, more bandwidth.
Sparse recrawl: cheap, stale results.
Real-world references
- Googlebot: undisclosed details; famously polite, BFS+PageRank+freshness-weighted.
- Heritrix (Internet Archive): open-source, designed for archival crawls.
- Common Crawl: monthly snapshots of the web, ~3B pages per crawl, public S3 data.
- Apache Nutch: open-source web crawler with PageRank.
- Scrapy: not for billion-scale, great for targeted crawls.