{}DSA Atlas

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.

Advanced5 interview questions

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

QuantityValue
Pages1B/month ≈ 400 pages/s avg (plan for ~1,000/s peak)
Avg page~100 KB (HTML, compressed ~25 KB)
Bandwidth400 × 100 KB = 40 MB/s ≈ 320 Mbit/s
Storage1B × 100 KB = 100 TB/month raw (≈ 25 TB compressed)
URL-seen set10B URLs × ~10 bits (Bloom, ~1% FP) ≈ 12.5 GB

3. High-level design

Web crawler architecture with crawler service, reverse index and document services, queues, NoSQL and SQL stores
A reference crawler that feeds search indexing. Diagram from The System Design Primer by Donne Martin, licensed CC BY 4.0.
seed URLs URL frontierpriority + politeness fetchers DNS cache robots cache parser content seen? (hash / SimHash) content store (S3) → indexer link extractor + normalise URL seen? (Bloom) new URLs
A loop: frontier → fetch → parse → extract → filter/dedup → frontier.

4. Deep dives

The URL frontier

prioritiser F1 high F2 F3 Fn low biasedselector B: wikipedia.org B: bbc.co.uk B: github.com B: …one host each min-heapnext fetch timeper host front queues = what matters; back queues + heap = when each host may be hit again
Mercator-style frontier: priority first, then per-host politeness.
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

LevelTechnique
URLnormalise → Bloom filter / partitioned hash set
Exact contentSHA-256 of normalised body
Near-duplicateSimHash (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

DecisionChoice
Partitioningby host hash → politeness is local
URL-seenBloom filter (rare false positives → skipped URLs accepted)
Traversalpriority-driven (importance), not pure BFS
Freshnessadaptive recrawl by change rate × importance
JS-heavy pagesseparate headless-browser rendering tier for a prioritised subset (expensive)

Interview questions

0 / 5 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.