Skip to main content
HS
HiSETSuccess
LeetCode Prep

System Design

Comprehensive interview guide: scalability, databases, distributed systems, cloud architecture, and full case-study walkthroughs — plus adaptive quizzes.

Case study

Design a Web Crawler

Crawl billions of web pages with politeness, deduplication, and distributed worker coordination.

BFSdeduppoliteness

Requirements

  • Functional: discover and fetch URLs; parse HTML; extract links; store content
  • Respect robots.txt and crawl-delay per domain
  • Prioritize important pages (PageRank, freshness)
  • Non-functional: 1B pages crawled/month; politeness ≥ 1 sec between requests per domain
  • Detect duplicate content (near-duplicate via simhash)

Back-of-envelope Estimation

MetricCalculationResult
Pages/month1BGiven
Fetch QPS1B / (30 × 86,400)~400/sec average sustained
Avg page size50 KB HTML50 TB/month raw HTML
URL frontier10B unique URLs discoveredBloom filter ~ 12 GB @ 1% FP
Domains100M uniquePer-domain queues essential for politeness

Internal API Design

ComponentInterface
Seed APIPOST /seeds { urls[], priority } — bootstrap frontier
Fetcherpull(url) → html, headers, status
Parserparse(html, baseUrl) → { links[], text, title }
Robots cachegetPolicy(domain) → { allowed paths, crawl-delay }

Data Model

StorePurpose
URL frontierPriority queue per domain shard (Kafka/Redis)
visited_bloomProbabilistic dedup of canonical URLs
url_metadataurl_hash, last_crawled, etag, content_hash — Cassandra
page_storeRaw HTML + parsed text — S3/HDFS
robots_cachedomain → robots.txt parsed rules — Redis TTL 24h

High-level Design

Crawler fleet
Coordinator ──► Domain Shards (hash domain → queue)
                      │
              Fetcher Workers (pull when politeness window open)
                      │
              Parser ──► extract links ──► normalize URL ──► dedup ──► re-enqueue
                      │
              Page Store (S3) + Index pipeline (downstream search index)

Deep Dive: Politeness & Scheduling

Each domain has a last_fetch timestamp and crawl-delay (from robots.txt, default 1s). Worker can fetch only if now − last_fetch ≥ delay. Priority within domain by PageRank estimate and freshness (last_crawled age).

  • Canonicalize URLs: lowercase host, strip fragments, resolve relative paths
  • DNS cache per worker; respect max redirects (3)
  • Rate limit per IP block to avoid bans

Deep Dive: Dedup at Scale

LayerMechanism
URL dedupBloom filter + DB confirm on positive
Content dedupSHA-256 of normalized text; skip if seen
Near-duplicateSimhash with Hamming distance ≤ 3

Failure Modes & Monitoring

MetricAlert
Fetch success rate< 90% (exclude 404/403)
Frontier lag per domain> 7 days for high-priority
Parser crash rate> 0.1%
Robots violationsAny — critical