Skip to main content
03

Design a web crawler

MediumURL frontierPolitenessDeduplicationDistributed workers

Design a web crawler that downloads billions of web pages, for example to build a search engine index or a dataset: start from seed URLs, fetch pages, extract links, and keep going.

A typical framing: "Crawl 1 billion pages a month, keep popular pages fresh, never overload any website, and don't waste effort on duplicates or crawler traps."

TL;DR

The crawler is a loop: pick a URL from the frontier → fetch → parse → store content → extract links → filter and dedupe → add new URLs to the frontier. At scale, the hard parts are the URL frontier (priority plus politeness: one connection per host and a delay between requests, respecting robots.txt), deduplication (a seen-URL set, often a Bloom filter, and content fingerprints like SimHash for near-duplicates), and distribution (partition the work by hostname, so each host is handled by exactly one worker, which makes politeness local).

Store raw pages in object storage, metadata in a scalable database, and push parsed content to downstream consumers (indexing) through a queue. Defend against traps (infinite calendars, session ids in URLs) with depth and per-host page limits, and schedule recrawls based on how often each page changes.

1. Requirements and estimates

Functional

  • Start from seed URLs, fetch HTML pages, extract and follow links.
  • Store page content and metadata for downstream indexing.
  • Recrawl pages to keep them fresh.

Non-functional

  • Politeness: respect robots.txt and don't overload any site.
  • Scalable to billions of pages, robust to bad HTML, slow servers and traps.
  • Extensible: new content types (PDFs, images) later.

Estimates

  • 1B pages / month ≈ 400 pages per second, peaks ~1,000.
  • Average page ~100 KB (HTML, compressed less) → 100 TB per month of raw content.
  • Seen-URL set: tens of billions of URLs; at ~100 bytes each that's terabytes, which is why a Bloom filter or a disk-based set is used.

2. API

A crawler is mostly internal, but it exposes a few control points:

http
POST /v1/seeds           { urls: [...] , priority }
GET  /v1/hosts/{host}    -> crawl status, last fetch, robots rules, error rate
POST /v1/hosts/{host}/pause

Downstream systems consume page.fetched events: { url, finalUrl, status, fetchedAt, contentHash, storageKey }.

3. Data model

text
url_state   (url_hash PK, url, host, last_fetched, next_fetch_at, status, content_hash, change_rate)
hosts       (host PK, robots_txt, robots_fetched_at, crawl_delay_ms, error_count)
pages       object storage: s3://crawl/{date}/{url_hash}.html.gz

4. High-level architecture

Loading diagram...

5. Deep dives and scaling

The URL frontier

Two layers:

  1. Front queues by priority: URLs are scored (PageRank-like importance, freshness needs, domain quality) and placed into priority queues.
  2. Back queues by host: each host has its own FIFO queue, and a heap keyed by "next allowed fetch time" decides which host is ready. A worker takes a host whose time has come, fetches one URL, and sets the host's next time to now + crawl delay.

This guarantees one request at a time per host with a delay, while keeping all workers busy with other hosts.

Distribution

Partition hosts across crawler nodes with consistent hashing on the hostname. Each node owns its hosts' back queues, DNS cache and robots rules, so politeness needs no global coordination. Extracted links for hosts owned by another node are forwarded to it in batches.

Deduplication

  • URL normalization first: lowercase the host, remove default ports, sort query parameters, drop fragments and known tracking parameters.
  • Seen URLs: a Bloom filter answers "definitely new" or "probably seen" in a few bits per URL; a false positive means occasionally skipping a new URL, which is acceptable. Back it with the url_state table for exact answers when needed.
  • Duplicate content: an exact hash (SHA-256) catches identical pages; SimHash catches near-duplicates (the same article with different ads), comparing fingerprints by Hamming distance.

Politeness and robots.txt

Fetch and cache robots.txt per host (refresh daily), obey Disallow and Crawl-delay, identify the crawler with a clear User-Agent. Back off automatically when a host returns 429 or 503 or slows down.

Traps and bad content

  • Limit URL length, path depth and pages per host per day.
  • Detect repeating path patterns (/a/b/a/b/a/b) and calendar-style infinite pagination.
  • Time out slow responses and cap the download size.
  • Render JavaScript (a headless browser pool) only for high-value pages, because it's 10 to 100 times more expensive.

Freshness

Track how often each page changed between fetches and schedule its next fetch accordingly: news front pages every few minutes, static documentation every few weeks. Use If-Modified-Since / ETag to make unchanged recrawls cheap (304 Not Modified).

Fault tolerance

The frontier is persisted (Kafka or a durable queue per partition), so a crashed node's hosts are reassigned and their queues resumed. Fetches are idempotent: refetching a page is harmless.

Common follow-ups

  • "BFS or DFS?" Roughly BFS by priority. DFS dives into one site and falls into traps; BFS with per-host queues spreads the load.
  • "How do you crawl a site that only renders with JavaScript?" A separate rendering tier with headless browsers, fed only with URLs that need it (detected by an almost empty HTML body).
  • "How would you avoid re-downloading 100 TB every month?" Conditional requests, change-rate based scheduling, and storing only changed pages (or diffs).

What interviewers look for

  • A clear crawl loop and component boundaries.
  • A frontier that handles priority and politeness (per-host queues, robots.txt).
  • Partitioning by host to scale without global locks.
  • URL normalization, Bloom filters and content fingerprints for dedupe.
  • Defenses against traps, plus a freshness strategy.

Was this helpful?

Related questions