Full answer
Requirements
Functional:
- Start from seed URLs, fetch pages, extract links and keep crawling.
- Respect robots.txt and per-host rate limits.
- Store page content and metadata for an indexer; re-crawl pages based on how often they change.
Non-functional:
- Scale to billions of pages; add machines to go faster.
- Never overload a single website.
- Survive worker crashes without losing the frontier.
Capacity estimates
Assumptions: 5B pages per 30-day month, average page 100 KB, compression to about a quarter for storage.
- Fetch rate: 5B / (30 x 86,400) = about 1,930 pages per second.
- Bandwidth: 1,930 x 100 KB x 8 = about 1.5 Gbps sustained.
- Raw storage: 5B x 100 KB = 500 TB per month; about 125 TB compressed.
- Seen-URL set: 10B URLs at 8-byte hashes is 80 GB; a Bloom filter at 1% false positives needs about 12 GB.
API
Internal interfaces rather than a public API:
frontier.add(urls, priority) # from parser or seed loader
frontier.next(worker_id) -> url # returns a URL whose host is due
store.put(url, content_hash, body, fetched_at, http_status)
Data model
- Frontier: per-host FIFO queues plus a priority index, persisted to disk (for example in RocksDB or Kafka partitions).
host_state: host, next_allowed_fetch_time, crawl_delay, robots_rules, robots_fetched_at.pages: url_hash, url, content_hash, fetched_at, status, change_rate estimate; bodies in object storage.
High-level design
flowchart LR
SEED[Seeds] --> F[URL frontier]
F --> W[Fetcher workers]
W --> DNS[DNS cache]
W --> R[robots.txt cache]
W --> OS[(Object storage)]
W --> P[Parser]
P --> D[Dedup: seen URLs and content hash]
D --> F
OS --> IX[Indexer]Politeness and priority
The frontier has two layers. Front queues order URLs by priority (page importance, change rate). Back queues hold one host each. A heap keyed by next_allowed_fetch_time decides which host is due; after a fetch, the host's next time moves forward by its crawl delay (for example 1 second, or what robots.txt asks). This means even 10,000 URLs from one site are fetched one at a time with gaps.
Each host is assigned to one worker by hash(host) mod N (or consistent hashing), so politeness state is local and no locks are needed.
Deduplication and traps
- Normalise URLs (lowercase host, remove default ports, sort query parameters, drop fragments) before checking the seen set.
- Skip pages whose content fingerprint (SHA-256 for exact, SimHash for near-duplicates) was already stored.
- Crawler traps such as endless calendar pages: limit depth per host and URL length, and cap pages per host per crawl.
Bottlenecks and trade-offs
- DNS can become slow at 2,000 lookups per second; cache results per host with their TTL.
- Bloom filter versus exact set: the Bloom filter saves memory but occasionally skips a new URL, which is acceptable for a crawler.
- Freshness versus coverage: re-crawling news sites hourly uses budget that could discover new pages; schedule re-crawls by observed change rate.
- Large hosts become a slow tail because of politeness; this is accepted by design.
- Crashes: the frontier is persisted, and a URL is marked done only after its content is stored, so a crash causes a re-fetch, not a loss.
By level
Fresher: Describe the fetch, parse and add-links loop and explain why the crawler must wait between requests to the same site. A fresher should mention robots.txt and a set of visited URLs.
Mid: Design the two-layer frontier, partition by host, size the system and handle normalisation and duplicates. Mention DNS caching.
Senior: Discuss freshness scheduling, near-duplicate detection, traps, Bloom filter trade-offs, persistence and recovery. Expect thoughtful reasoning about large hosts and politeness limits.
Key points
- Per-host back queues and a next-fetch-time heap enforce politeness
- Partition hosts to workers by hash
- Normalise URLs and deduplicate by URL and content
- About 1,930 pages per second and 1.5 Gbps for 5 billion pages a month
- Persist the frontier so crashes cause re-fetches, not losses
Common mistakes
- Fetching from one host in parallel with many threads
- Ignoring robots.txt and crawl delays
- No URL normalisation, so duplicates explode
- Forgetting crawler traps and DNS cost
Follow-ups
- How do you decide when to re-crawl a page?
- How would you crawl JavaScript-heavy sites?
- How do you detect near-duplicate pages?
Tips
- Explain front and back queues
- State the politeness rule clearly
- Size the seen-URL set and offer a Bloom filter
Evaluation rubric
- Correct frontier design with politeness
- Capacity math for rate, bandwidth and storage
- Deduplication and trap handling
- Partitioning and failure handling