Design a web crawler / search indexer
A distributed web crawler systematically downloads the web to feed a search index. The hard parts are the URL frontier (what to crawl next, without duplicates), politeness (don't hammer a host), and scaling fetchers.
Streaming & Pipelines, 10 components, about 40 min
1. Scope
In scope
- • Fetch pages from seed URLs
- • Discover + enqueue new links
- • Deduplicate URLs
- • Respect robots.txt + politeness
- • Build an index
Out of scope
- • Ranking
- • Query serving
- • JS rendering farm (mention only)
2. Functional requirements
Must have
- • Crawl pages starting from seeds
- • Avoid re-crawling the same URL
- • Per-host rate limiting
- • Extract and follow links
Nice to have
- • Freshness re-crawl scheduling
- • JS rendering
- • Sitemap ingestion
3. Non-functional requirements
4. Back-of-envelope estimation
Assumptions
- • Crawl 1B pages/month
- • Average page 100 KB
- • Re-crawl popular pages weekly
- • Dedup via bloom filter
Derived numbers
5. API design
Get next URL respecting politeness
Enqueue discovered links after dedup
6. High-level design
What each box in the reference diagram is responsible for:
7. Data model
8. Deep dives
The URL frontier
The frontier decides what to crawl next. It balances two goals: priority (crawl important/fresh pages first) and politeness (don't exceed ~1 req/sec per host). A common design uses a front queue set for priority and a back queue set partitioned by host, each with a politeness delay, so fetchers pull host-respecting work.
Deduplication
You'll discover the same URL millions of times. A bloom filter gives memory-efficient "have I seen this?" checks with a tiny false-positive rate (occasionally skipping a new URL, never re-crawling). Back it with durable storage so it survives restarts.
Politeness & robots.txt
Fetch and cache each host's robots.txt; honor crawl-delay and disallow rules. Partition fetchers by host so a single host is never hit by multiple workers simultaneously. Cache DNS aggressively to avoid resolver overload.
Freshness
Pages change at different rates. Track per-page change frequency and schedule re-crawls adaptively — news sites often, static pages rarely — to spend limited crawl budget where it matters.
9. Bottlenecks
- Per-host politeness limits throughput → massive host parallelism
- Frontier size → distributed, partitioned by host
- Crawl traps/infinite spaces → depth limits + URL normalization
10. Trade-offs
Frontier structure
Choose: Front queues by priority feeding back queues per host, so a scheduler enforces per-host delay (the Mercator design).
Instead of: One global FIFO queue: simple, but bursts many requests at the same host.
Politeness is a hard requirement; per-host queues make it structural.
URL dedupe
Choose: Bloom filter in memory for "seen" checks, backed by a disk store for exact checks on positives.
Instead of: Exact hash set of all URLs in memory: billions of URLs need hundreds of GB.
A Bloom filter at 1% false positives needs ~10 bits per URL: about 1.2 GB per billion URLs.
Render JavaScript or not
Choose: Fetch HTML first; send only pages that need it to a separate headless-browser render tier.
Instead of: Render everything: 10–100× more CPU.
Most pages index fine without rendering.
11. Failure modes
Spider trap (infinite calendar URLs)
Crawler wastes budget on one site.
Mitigation: Per-host page budgets, URL depth limits, and pattern detection for repeating path segments.
DNS resolver becomes the bottleneck
Fetchers sit idle.
Mitigation: Local caching resolvers with batched async lookups.
Fetcher crashes with leased URLs
URLs never crawled.
Mitigation: Leases with timeouts; unacknowledged URLs return to the frontier.
12. What interviewers probe
Answer each one yourself before opening it.
How many fetchers for 1B pages a month?Show a strong answer
~385 pages/s average. At ~1 s per fetch with 100 concurrent connections per machine, a handful of machines; politeness and DNS usually limit you first.
How do you detect near-duplicate pages?Show a strong answer
SimHash or MinHash fingerprints of content; pages within a small Hamming distance are near-duplicates.
How do you decide re-crawl frequency?Show a strong answer
Estimate each page's change rate from past crawls and its importance; schedule to maximize freshness per fetch.
How do you respect robots.txt efficiently?Show a strong answer
Fetch and cache it per host with a TTL (about a day); check every URL against the cached rules before enqueueing.
Sign in to track this guide in your curriculum progress.