Design a Web Crawler
Crawl billions of pages politely and efficiently — the URL frontier with priority and per-host politeness queues, DNS caching, fetching at scale, robots.txt, URL normalisation and URL-seen dedup with Bloom filters, content dedup with fingerprints (SimHash), traps and spam, recrawl scheduling for freshness, and distributing the crawl.
1. Requirements
Functional
- Starting from seed URLs, fetch pages, extract links, and crawl the web (HTML only for this design).
- Store page content for indexing; recrawl for freshness.
Non-functional
- Scale: 1B pages/month initially, design for 10B+.
- Politeness: respect robots.txt; never overload a host.
- Robustness: malformed HTML, traps, slow servers, huge pages.
- Extensibility: new content types (PDF, images) later.
2. Estimation
| Quantity | Value |
|---|---|
| Pages | 1B/month ≈ 400 pages/s avg (plan for ~1,000/s peak) |
| Avg page | ~100 KB (HTML, compressed ~25 KB) |
| Bandwidth | 400 × 100 KB = 40 MB/s ≈ 320 Mbit/s |
| Storage | 1B × 100 KB = 100 TB/month raw (≈ 25 TB compressed) |
| URL-seen set | 10B URLs × ~10 bits (Bloom, ~1% FP) ≈ 12.5 GB |
3. High-level design

4. Deep dives
The URL frontier
Python
import heapq, time
from collections import deque, defaultdict
class PoliteFrontier:
def __init__(self, default_delay=1.0):
self.queues = defaultdict(deque) # host -> URLs (back queues)
self.heap = [] # (next_allowed_time, host)
self.delay = defaultdict(lambda: default_delay)
self.active = set() # hosts in the heap or being fetched
def add(self, url, host):
self.queues[host].append(url)
if host not in self.active:
self.active.add(host)
heapq.heappush(self.heap, (time.monotonic(), host))
def next(self):
"""Returns (url, host) when some host is ready; caller fetches, then calls done()."""
ready_at, host = heapq.heappop(self.heap)
time.sleep(max(0.0, ready_at - time.monotonic()))
return self.queues[host].popleft(), host
def done(self, host, fetch_seconds):
if self.queues[host]:
gap = max(self.delay[host], 10 * fetch_seconds) # slow host → back off more
heapq.heappush(self.heap, (time.monotonic() + gap, host))
else:
self.active.discard(host) # re-armed by the next add()
Fetching
- Async I/O (thousands of concurrent connections per fetcher), timeouts, max size (e.g., 10 MB), follow ≤ 5 redirects.
- DNS is a hidden bottleneck — run local caching resolvers and prefetch.
- robots.txt cached per host (refresh daily); obey
Disallow,Crawl-delay; identify with a clear user agent. - Conditional GETs for recrawls.
Dedup
| Level | Technique |
|---|---|
| URL | normalise → Bloom filter / partitioned hash set |
| Exact content | SHA-256 of normalised body |
| Near-duplicate | SimHash (64-bit; Hamming distance ≤ 3 → duplicate) |
Storage
Raw pages in object storage (WARC files), metadata (URL, fetch time, status, hash, outlinks) in a wide-column store (Bigtable/HBase/Cassandra) keyed by reversed host (com.example.www/path) so a site's pages are contiguous.
5. Trade-offs
| Decision | Choice |
|---|---|
| Partitioning | by host hash → politeness is local |
| URL-seen | Bloom filter (rare false positives → skipped URLs accepted) |
| Traversal | priority-driven (importance), not pure BFS |
| Freshness | adaptive recrawl by change rate × importance |
| JS-heavy pages | separate headless-browser rendering tier for a prioritised subset (expensive) |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.