Chapter 69: Design Web Crawler

Requirements

Functional Requirements

Non-Functional Requirements

Interview Tip: Start by clarifying the scale (1 billion pages? 10 billion?), crawl frequency (one-time vs continuous), and whether you need to handle dynamic content (JavaScript-rendered pages). Ask if there are specific domains or content types to prioritize.

High-Level Design

graph TB subgraph "Web Crawler System" Seeds[Seed URLs] --> Frontier[URL Frontier] Frontier --> Fetcher[Fetcher Pool] Fetcher --> Parser[HTML Parser] Parser --> URLFilter[URL Filter] URLFilter --> Dedup[URL Dedup] Dedup --> Frontier Parser --> ContentStore[Content Store] Parser --> LinkGraph[Link Graph DB] RobotsTxt[Robots.txt Cache] -.->|Check| Fetcher DNSCache[DNS Cache] -.->|Resolve| Fetcher BloomFilter[Bloom Filter] -.->|Check| Dedup Fetcher -->|Rate Limit| PolitenessQueue[Politeness Queue] PolitenessQueue --> Fetcher end Internet[Internet] <--> Fetcher

Core Components

1. Seed URLs

Starting point for the crawler. High-quality seeds lead to better crawl coverage. Sources: popular domains, site directories, previously crawled URLs.

2. URL Frontier

Queue of URLs to be crawled. Manages both priority (important pages first) and politeness (rate limiting per domain). Hybrid data structure combining priority queue and per-domain queues.

3. Fetcher Pool

Worker threads that download web pages. Each fetcher checks DNS cache, robots.txt, applies rate limits, and handles HTTP requests. Typically 100-1000 parallel fetchers.

4. HTML Parser

Extracts links from downloaded pages, normalizes URLs, and extracts content for storage. Validates HTML and handles malformed pages gracefully.

5. URL Filter

Applies rules to determine which URLs to crawl: blacklist (spam, malicious sites), file type filters (skip .exe, .zip unless needed), URL normalization (remove tracking parameters).

6. Content Store

Distributed storage system (S3, HDFS) for crawled content. Typically compressed and deduplicated. Supports both archival and fast retrieval for indexing.

Interview Tip: Walk through a single URL's journey: Frontier → Fetcher (check robots.txt, DNS) → Download → Parse → Extract links → Filter → Dedup → Back to Frontier. Mention this is a continuous loop.

URL Frontier

Architecture

The URL Frontier is the heart of the crawler, managing which URLs to crawl next. It must balance two competing goals:

graph LR subgraph "URL Frontier Architecture" Prioritizer[Prioritizer] --> Q1[Priority Queue 1
High Priority] Prioritizer --> Q2[Priority Queue 2
Medium Priority] Prioritizer --> Q3[Priority Queue 3
Low Priority] Q1 & Q2 & Q3 --> Router[Domain Router] Router --> DQ1[Domain Queue 1
example.com] Router --> DQ2[Domain Queue 2
site.org] Router --> DQ3[Domain Queue 3
page.net] DQ1 & DQ2 & DQ3 --> Selector[Selector
Round-Robin] Selector --> Fetchers[Fetcher Pool] end

Two-Layer Queue System

Layer 1: Priority Queues (Front Queues)

URLs are assigned priority scores (0-100) based on:

URLs are distributed into N priority buckets (e.g., 5 buckets: 90-100, 70-90, 50-70, 30-50, 0-30).

Layer 2: Politeness Queues (Back Queues)

Each queue corresponds to a single domain (or IP address). This ensures URLs from the same domain are crawled sequentially with delays, never in parallel.

BFS vs DFS

Approach Description Use Case
BFS (Breadth-First) Crawl by levels: all links from homepage, then all links from those pages General web crawling - ensures important pages (closer to root) are crawled first
DFS (Depth-First) Follow one path deep before backtracking Focused crawling (e.g., crawl all product pages on an e-commerce site)

Recommendation: Use BFS for general web crawling. It naturally discovers high-quality pages first and provides better coverage.

Interview Tip: Emphasize the two-layer queue design. Front queues handle priority, back queues handle politeness. This separation of concerns is a classic interview pattern.

Politeness

Why Politeness Matters

A poorly designed crawler can overwhelm target servers, causing:

Politeness Mechanisms

1. Robots.txt

Standard protocol where websites specify crawl rules in /robots.txt:

User-agent: *
Disallow: /admin/
Disallow: /private/
Crawl-delay: 2

User-agent: Googlebot
Crawl-delay: 1

2. Rate Limiting Per Domain

Implement token bucket or leaky bucket algorithm per domain:

3. DNS Caching

Cache DNS lookups (TTL: 1 hour) to reduce load on DNS servers. DNS queries can become a bottleneck at scale.

4. User-Agent Identification

Always identify your crawler with a descriptive User-Agent header and contact information:

User-Agent: MyCrawler/1.0 (+http://example.com/bot.html; bot@example.com)
Interview Tip: When discussing politeness, mention that good crawlers are "polite" both ethically and practically. Being blocked wastes your resources too. Always bring up robots.txt and rate limiting.

Deduplication

Deduplication is critical for efficiency. Without it, the crawler would waste resources re-crawling the same pages and storing duplicate content.

1. URL Deduplication

Prevent crawling the same URL multiple times:

Bloom Filter

Secondary Check

For URLs that pass Bloom filter, check authoritative data store:

URL Normalization

Before deduplication, normalize URLs to canonical form:

2. Content Deduplication

Detect duplicate or near-duplicate content (copied pages, mirrors):

SimHash (Recommended)

MinHash

Checksum (MD5/SHA-256)

Interview Tip: For URL dedup, mention Bloom filter first (shows you know space-efficient probabilistic data structures). For content dedup, mention SimHash for near-duplicates. These are classic crawler techniques.

Distributed Crawling

Why Distribute?

Single-machine crawlers hit limits around 1-10 million pages. For billions of pages, you need horizontal scaling.

Partitioning Strategy

Domain-Based Partitioning (Recommended)

URL-Based Partitioning

Architecture

graph TB Coordinator[Coordinator] --> W1[Worker 1
Domains: a.com-m.com] Coordinator --> W2[Worker 2
Domains: n.com-z.com] Coordinator --> W3[Worker 3
Domains: 0-9] W1 --> Storage1[Content Store
S3/HDFS] W2 --> Storage1 W3 --> Storage1 W1 --> MetaDB[(Metadata DB
URL status, timestamps)] W2 --> MetaDB W3 --> MetaDB W1 <--> Queue[Distributed Queue
Kafka/SQS] W2 <--> Queue W3 <--> Queue

Components

1. Coordinator

2. Workers

3. Distributed Queue (Kafka/SQS)

4. Shared Metadata DB

Interview Tip: Emphasize domain-based partitioning for politeness. Mention consistent hashing to handle load imbalance. Discuss how workers communicate via distributed queue (Kafka).

Freshness

The Challenge

Web content changes constantly. News sites update every minute, blogs weekly, archived pages never. A one-time crawl quickly becomes stale.

Re-Crawl Strategy

1. Prioritize by Change Frequency

Track how often each page changes and adjust re-crawl frequency:

2. Change Detection

Detect if page has changed since last crawl:

3. Historical Analysis

Learn change patterns from history:

4. Sitemap and RSS

Priority Formula

Combine multiple signals to compute re-crawl priority:

priority = (changeFrequency * 0.4) +
           (pageImportance * 0.3) +
           (timeSinceLastCrawl * 0.2) +
           (userDemand * 0.1)
Interview Tip: Freshness is often forgotten in interviews. Mentioning it proactively shows depth. Discuss trade-offs: re-crawling everything daily is expensive; adaptive strategies balance freshness and cost.

Scalability

Scale Calculations

Scenario: Crawl 1 billion web pages per month

Bottlenecks and Solutions

1. Network Bandwidth

2. DNS Resolution

3. URL Frontier Size

4. Storage

5. Deduplication at Scale

Fault Tolerance

Monitoring

Interview Tip: Always do back-of-envelope calculations early. Show you can translate requirements (1 billion pages) into concrete numbers (380 pages/sec, 38 MB/sec, 100 TB). This demonstrates systems thinking.

Interview Summary

Key Talking Points

  1. URL Frontier: Two-layer design (priority + politeness queues) is the core pattern
  2. Politeness: Robots.txt, rate limiting, domain-based partitioning
  3. Deduplication: Bloom filter (URL) + SimHash (content)
  4. Distributed Crawling: Domain-based partitioning, consistent hashing, Kafka for URL routing
  5. Freshness: Adaptive re-crawl based on change frequency and importance

Common Follow-Up Questions