Case study: Web crawler
Overview
A web crawler (spider) discovers and downloads pages so they can be indexed, archived or analysed. The core loop is simple; the challenges are scale (billions of URLs), politeness (never hammering one site), duplicates and traps.
Try the exercise first: 45 minutes, then compare.
1. Requirements
Functional
- Start from seed URLs, fetch pages, extract links and keep discovering.
- Store page content for indexing; revisit pages to keep them fresh.
- Respect
robots.txtand site-specific crawl delays.
Non-functional
- Scale: 1 billion pages per month.
- Politeness: limited request rate per host.
- Robust against malformed pages, slow servers and traps; extensible to new content types.
2. Estimates
pages : 1B / month ÷ 2.6M s ≈ 400 / s (plan for ~1,000 / s)
bandwidth : 400 × 100 KB ≈ 40 MB/s ≈ 320 Mbit/s
storage : 1B × 100 KB = 100 TB / month raw (compress ~4× → ~25 TB)
URL set : tens of billions of URLs known → hashes stored on disk; Bloom filter in memory3. Components and loop
- Frontier chooses the next URLs to fetch.
- Fetchers resolve DNS (cached), check
robots.txt(cached per host), download with timeouts and size limits. - Parser extracts text, metadata and links.
- Content dedupe skips pages identical or nearly identical to ones already stored.
- URL filter normalises links, removes already-seen and disallowed URLs, and sends new ones to the frontier.
- Storage keeps raw pages (object storage) and metadata (URL, fetch time, status, hash) for the indexer.
4. Deep dives
The URL frontier
The frontier balances two goals:
- Priority: fetch important pages first (high PageRank, popular sites, pages that change often). Front queues are ordered by priority.
- Politeness: never send many requests to one host at once. Back queues are one per host; a host’s queue is served by one worker with a delay between requests (respecting
Crawl-delay). A heap keyed by “next allowed time” picks which host to fetch from next.
Hosts are partitioned across fetcher machines (by hash of host), so each machine is solely responsible for being polite to its hosts.
Deduplication
- URLs: normalise (lowercase host, remove default ports and fragments, sort query parameters, strip tracking parameters), then check a Bloom filter in memory backed by a persistent store of URL hashes. Bloom filters can give false positives (occasionally skipping a new URL), never false negatives.
- Content: many URLs serve the same content (mirrors, session IDs). An exact hash catches identical pages; SimHash or MinHash fingerprints catch near-duplicates.
Recrawling
Pages change at different rates. Track each page’s observed change frequency and importance; schedule revisits accordingly (news homepages every few minutes, an old blog post every few months). Use conditional requests (If-Modified-Since, ETags) to avoid downloading unchanged pages.
Traps and robustness
- Crawler traps: infinite calendars, endlessly generated URLs, session IDs in URLs. Limit URL length and depth, cap pages per site, detect repeating path patterns.
- Bad servers: timeouts, maximum response size, limited redirects, retries with backoff.
- Content types: handle HTML first; PDFs and others through pluggable parsers.
Distribution and failure
Fetchers are stateless apart from their host assignments; the frontier is persisted (for example in a distributed queue or key-value store) so a crash loses little. Checkpointing lets the crawl resume.
5. Trade-offs and bottlenecks
- Politeness, not hardware, often limits speed on popular sites; crawl budgets per host are necessary.
- Bloom filter false positives trade a little coverage for a lot of memory.
- DNS resolution can become a bottleneck; cache aggressively and run local resolvers.
6. Follow-up questions
- JavaScript-heavy sites? A separate rendering tier with headless browsers for pages that need it (expensive).
- Prioritise new pages? Use sitemaps and links from frequently changing pages (news feeds, RSS).
- Legal and ethical limits? Respect robots.txt, terms of service, rate limits and opt-outs; identify the crawler with a user agent and contact page.
Recap
A crawler loops through a frontier of URLs, fetching politely, parsing, deduplicating and feeding new links back. The frontier balances priority with per-host politeness; Bloom filters and normalisation deduplicate URLs, and hashes or SimHash deduplicate content. Recrawl schedules keep content fresh, and limits on depth, URL length and per-site budgets defend against traps.
Design it yourself (45 minutes)
Set a 45-minute timer and design a web crawler for a search engine that should crawl 1 billion pages per month, revisit important pages more often, respect robots.txt and never overload any single website. Use the framework, then compare.
1. Clarifying questions:
2. Functional requirements:
3. Non-functional requirements:
4. Estimates (pages/s, bandwidth, storage):
5. Components:
6. Data structures (frontier, seen-set):
7. High-level design:
8. Deep dives (politeness, dedupe, prioritisation, traps):
9. Trade-offs and bottlenecks:A strong answer covers:
- Estimates: 1B/month ≈ 400 pages/s average (~1,000 peak); ~100 KB HTML → ~40 MB/s,
~100 TB/month raw (compressed less).
- Loop: frontier → fetchers → parse → extract links → normalise → URL filter
(seen? allowed?) → frontier; content dedupe → store.
- Frontier: priority queues (importance, freshness) feeding per-host queues with a
delay between requests to the same host (politeness); hosts mapped to fetcher workers.
- robots.txt cached per host; DNS caching; timeouts; max page size.
- URL seen-set: Bloom filter + persistent store of URL hashes; normalise URLs.
- Content dedupe: hash (exact) and SimHash/MinHash (near-duplicates).
- Traps: max depth, URL length limits, per-site page budgets.
- Recrawl scheduling based on change frequency and importance.
- Storage: raw pages in object storage; metadata in a DB; feed to indexing pipeline.Summary
- A crawler is a loop: take URLs from the frontier, fetch, parse, extract links, filter and add new URLs back.
- Politeness: one queue per host with delays, and respect robots.txt and crawl budgets.
- Deduplicate URLs (normalisation plus Bloom filter or hash set) and content (exact and near-duplicate hashes).
- Prioritise by importance and change frequency, and schedule recrawls.
- Defend against crawler traps with depth, length and per-site limits.