Case Study 12 โ€” Distributed Web Crawler

Design a web crawler that discovers and downloads billions of web pages, respects site policies, and feeds a search index or analytics pipeline.

1. Problem

Given seed URLs, the system should continuously fetch pages, extract new links, avoid duplicates, stay polite to websites, and store raw content for downstream processing (indexing, ML, archival).

2. Requirements

Functional (MVP)

  • Accept seed URLs and crawl scope (domain, depth limit)
  • Fetch HTML pages over HTTP/HTTPS
  • Parse links and enqueue new URLs
  • Deduplicate URLs (never fetch same canonical URL twice)
  • Respect robots.txt and per-domain rate limits
  • Store page content + metadata (URL, status, fetch time, checksum)
  • Track crawl status: pending, in-progress, done, failed
  • Retry transient failures with backoff

Out of scope (initially)

  • JavaScript rendering (headless browser farm)
  • Login-gated / CAPTCHA pages
  • Real-time "instant index" (< 1 min) for entire web
  • Full distributed graph analytics on link structure
  • Image/video binary crawling at scale (can be phase 2)

Non-functional

  • Scalable to billions of URLs
  • Polite โ€” don't overload small sites
  • Fault tolerant โ€” worker crashes shouldn't lose progress
  • Extensible โ€” plug in new parsers (news, products, etc.)
  • Eventual completeness within hours/days, not seconds
  • Cost-efficient storage and bandwidth

3. Back-of-the-envelope estimates

These numbers are rough order-of-magnitude math โ€” not a capacity plan. In interviews, the goal is to show you know what to count and which resource breaks first. A useful shortcut: 1 QPS sustained โ‰ˆ 86,400 operations/day. Crawlers run 24/7, but peak parallelism can be much higher than average when you burst to finish a recrawl window.

Why we estimate

A web crawler is a throughput pipeline, not a user-facing latency system. Estimates tell us:

  • Whether storage, URL metadata, or network bandwidth is the limiting factor
  • Why a single Redis queue cannot hold billions of URLs
  • How politeness (per-domain rate limits) caps theoretical max fetch rate

Assumptions

AssumptionValueWhy it matters
Unique pages in corpus5 billionTotal work per crawl cycle
Recrawl interval~30 daysSets sustained average fetch rate
Average HTML page size50 KBStorage and bandwidth
Peak crawl rate (large fleet)100,000 pages/sBurst capacity with many workers
Metadata per URL~500 BURL state DB sizing
Politeness default~1 req/s per domainReal throughput << raw worker count

Step A โ€” Traffic (QPS) with labeled arithmetic

Sustained average fetch rate (refresh entire corpus in 30 days):

Total pages         = 5,000,000,000
Seconds in 30 days  = 30 ร— 86,400 = 2,592,000 seconds

Average fetch QPS   = 5,000,000,000 รท 2,592,000
                    โ‰ˆ 1,930 pages/second (sustained)

Round to ~1,900 pages/s โ€” this is what you must sustain to recrawl monthly.

Peak fetch rate (parallel fleet):

Peak fetch QPS โ‰ˆ 50,000โ€“100,000 pages/s (design target for large crawl)

Peak is 50ร—+ above average โ€” you crawl faster when needed, then throttle for politeness.

Enqueue rate (new URLs discovered while parsing):

Discovery adds URLs continuously. If each page yields ~10 new links and 90% are duplicates after dedup:

New unique URLs/s โ‰ˆ 1,900 ร— 10 ร— 10% โ‰ˆ 1,900 enqueues/s (order of magnitude)

Dedup and frontier design must handle this steady write load to metadata.

Step B โ€” Storage

Raw HTML in object storage:

Pages           = 5 billion
Size per page   = 50 KB

Raw HTML        = 5B ร— 50 KB = 250 TB
With metadata   = 250 TB + indexes โ†’ plan for ~300 TB+

URL metadata database:

URL rows        = 5 billion
Bytes per row   โ‰ˆ 500 B (hash, URL, status, timestamps, blob pointer)

Metadata raw    = 5B ร— 500 B = 2.5 TB
With indexes    โ‰ˆ 5โ€“8 TB (several TB total)

Bloom filter for dedup (in memory, approximate):

5B URLs, 1% false-positive rate โ†’ roughly 5โ€“6 GB per filter (varies by implementation)
Often sharded by hash prefix across Redis/cluster nodes

Step C โ€” Bandwidth

Egress at peak crawl:

Peak fetch QPS   = 100,000 pages/s
Page size        = 50 KB

Bandwidth        = 100,000 ร— 50 KB
                 = 5,000,000 KB/s
                 โ‰ˆ 5 GB/s download throughput

Sustained average bandwidth:

1,900 pages/s ร— 50 KB โ‰ˆ 95 MB/s (much lower than peak)

Bandwidth and disk write speed to object storage matter as much as CPU.

Step D โ€” Read:write ratio table

OperationTypeRateNotes
HTTP GET (fetch page)Read (external)~1,900/s avg; 100k/s peakPoliteness caps per domain
Dedup checkRead~1 per enqueueBloom + DB confirm
URL enqueue / status updateWrite~1,900/s+Metadata DB hot path
Blob store writeWrite~1,900/s avg250 TB total
Parse / link extractCPU~1,900/s avgEmits new URLs to frontier
Recrawl scheduler queryReadBatch / periodicScans stale URLs

Ratio: roughly 1:1 fetch to metadata write on the hot path; dedup reads are cheap compared to HTTP fetches.

What the numbers tell us

  • 250 TB of HTML โ†’ object storage (S3), not a database
  • Billions of URL rows โ†’ sharded metadata store (Cassandra/DynamoDB), not one Postgres
  • Frontier cannot be one queue โ†’ partition by hash(domain) or Kafka topics
  • Bloom filters save billions of DB lookups for dedup
  • 1 req/s per domain means 100k global QPS requires 100k+ distinct domains in flight โ€” fair scheduling across domains is critical
  • Fetch workers are stateless; all coordination lives in frontier + dedup + metadata

Common mistake for this problem

Designing for maximum HTTP QPS without per-domain politeness. A crawler that ignores robots.txt and hammers one site at 10k req/s will get blocked and is unethical. Another mistake: storing raw HTML in SQL โ€” at 250 TB, blobs belong in object storage; SQL holds metadata only.

4. High-Level Design (HLD)

Components

ComponentRole
Seed ServiceInitial URLs, crawl policies, allow/deny lists
URL FrontierPriority queues of URLs to fetch (by domain shard)
Fetch WorkersHTTP GET, headers, redirects, timeouts
Robots CacheCached robots.txt rules per host
Dedup Store"Have we seen this URL?" โ€” Bloom filter + DB confirm
Object StorageRaw page blobs (cheap, durable)
Parse WorkersHTML parse, extract links, title, text snippet
Metadata DBURL state, last fetch, HTTP status, content hash
SchedulerPrioritize important domains, recrawl stale pages
Index PipelineDownstream search index (separate system)

Flows

Add seed & discover

  1. Admin adds https://news.example.com
  2. URL normalized โ†’ hash โ†’ dedup check โ†’ enqueue frontier
  3. Fetch worker pulls URL (respecting domain rate limit)
  4. Download page โ†’ store blob โ†’ emit parse job
  5. Parser extracts links โ†’ normalize โ†’ dedup โ†’ enqueue new URLs

Recrawl

  1. Scheduler queries Metadata DB for URLs where last_fetched_at < now - 30 days
  2. Re-enqueue with lower priority than fresh discoveries

Failure handling

  1. HTTP 5xx / timeout โ†’ retry with exponential backoff (max N tries)
  2. HTTP 404 โ†’ mark done, don't retry soon
  3. Worker crash mid-fetch โ†’ URL lease expires โ†’ another worker picks it up

Trade-offs

ChoiceProsCons
BFS vs priority crawlBFS simple; priority focuses on important pagesPriority needs ranking signals
Per-domain queuesNatural rate limitingHot domains need fair scheduling
Bloom filter dedupTiny memory, fastFalse positives need DB confirm
Store HTML in blob vs DBCheaper at scaleExtra hop to read content
Politeness delay 1 req/s/domainSafe for sitesSlow for large domains โ€” use adaptive limits

5. Low-Level Design (LLD)

APIs (internal / admin)

POST /api/v1/crawl/seeds
Body: { urls: ["https://example.com"], maxDepth: 5, allowedDomains: ["example.com"] }
โ†’ 202 { crawlJobId }

GET /api/v1/crawl/jobs/:jobId
โ†’ { status, urlsDiscovered, urlsFetched, urlsFailed, progressPct }

GET /api/v1/urls/:urlHash
โ†’ { canonicalUrl, status, lastFetchedAt, httpStatus, contentHash, blobKey }

POST /api/v1/crawl/pause?domain=example.com
โ†’ 200

GET /api/v1/stats
โ†’ { fetchRate, queueDepthByShard, errorRate }

Schema

urls (
  url_hash       BIGINT PRIMARY KEY,  -- hash of normalized URL
  canonical_url  TEXT NOT NULL,
  domain         TEXT NOT NULL,
  status         VARCHAR(20),  -- PENDING, FETCHING, FETCHED, FAILED, SKIPPED
  depth          INT,
  priority       INT DEFAULT 0,
  last_fetched   TIMESTAMPTZ,
  next_fetch_after TIMESTAMPTZ,
  http_status    INT,
  content_hash   VARCHAR(64),
  blob_key       TEXT,
  fail_count     INT DEFAULT 0,
  crawl_job_id   UUID
)

crawl_jobs (
  job_id         UUID PRIMARY KEY,
  seeds          JSONB,
  config         JSONB,
  created_at     TIMESTAMPTZ,
  status         VARCHAR(20)
)

domain_policies (
  domain         TEXT PRIMARY KEY,
  min_delay_ms   INT DEFAULT 1000,
  max_concurrent INT DEFAULT 2,
  robots_txt     TEXT,
  robots_fetched TIMESTAMPTZ,
  is_paused      BOOLEAN DEFAULT FALSE
)

fetch_log (
  id             BIGSERIAL PRIMARY KEY,
  url_hash       BIGINT,
  fetched_at     TIMESTAMPTZ,
  http_status    INT,
  bytes          INT,
  worker_id      TEXT,
  latency_ms     INT
)

Shard urls by hash(domain) % N or by url_hash for even spread.

Modules

SeedController / CrawlJobService
FrontierService / DomainQueueManager / PriorityScheduler
FetchWorker / HttpClient / RateLimiter / RobotsParser
DedupService / BloomFilterManager / UrlNormalizer
ParseWorker / HtmlParser / LinkExtractor
BlobStore / MetadataRepository
RecrawlScheduler

Key algorithm โ€” URL normalization

function normalize(rawUrl):
  url = parse(rawUrl)
  url.scheme = lower(url.scheme)           # https
  url.host = lower(url.host)                 # strip www? policy choice
  url.fragment = null                        # usually ignore #anchor
  url.path = collapseSlashes(url.path)
  if url.path empty: url.path = "/"
  # optional: remove default ports, sort query params, strip tracking params
  return canonicalString(url)

function urlHash(normalizedUrl):
  return murmur128(normalizedUrl)  # 128-bit โ†’ store as BIGINT or UUID

Consistent normalization prevents http://ExAmple.com/ and https://example.com being treated differently (policy-dependent).

Key algorithm โ€” enqueue with dedup

function enqueue(url, depth, priority):
  norm = normalize(url)
  h = urlHash(norm)

  if bloom.mightContain(h):
    if db.exists(h): return DUPLICATE
  bloom.add(h)  # may false-positive; DB is final judge

  inserted = db.insertIfAbsent(urls, h, norm, status=PENDING, depth, priority)
  if not inserted: return DUPLICATE

  shard = hash(norm.domain) % NUM_SHARDS
  frontier.push(shard, { urlHash: h, priority, domain: norm.domain })
  return ENQUEUED

Key algorithm โ€” fetch worker loop

function fetchLoop(workerId):
  while true:
    task = frontier.pullBlocking(timeout=5s)  # respects domain fairness
    if task is null: continue

    policy = policyCache.get(task.domain)
    if policy.is_paused: requeue(task); continue

    waitForRateLimit(task.domain, policy.min_delay_ms)

    if not robotsAllowed(task.url, policy.robots_txt):
      db.update(task.urlHash, SKIPPED)
      continue

    lease = db.claimUrl(task.urlHash, workerId, leaseTtl=120s)
    if not lease: continue  # another worker has it

    try:
      response = http.get(task.url, timeout=10s, maxBytes=10MB)
      blobKey = blobStore.put(response.body)
      db.update(task.urlHash, FETCHED, response.status, hash(body), blobKey)
      parseQueue.push({ urlHash: task.urlHash, blobKey })
    catch TransientError:
      db.incrementFailCount(task.urlHash)
      if failCount < MAX_RETRIES:
        frontier.requeueWithBackoff(task, delay=2^failCount seconds)
      else:
        db.update(task.urlHash, FAILED)

Key algorithm โ€” domain-fair frontier

# Round-robin across domain buckets so one huge site doesn't starve others
function pullNext():
  for domain in roundRobin(domainList):
    if domain.isPaused: continue
    if not rateLimiter.canFetch(domain): continue
    url = domainQueue[domain].pop()
    if url: return url
  return null

Concurrency & correctness

ConcernApproach
Duplicate fetchesBloom + INSERT ... ON CONFLICT DO NOTHING on url_hash
Same URL fetched by 2 workersclaimUrl sets status FETCHING with lease TTL atomically
robots.txt raceCache with TTL; refresh async before bulk crawl
Poison URLs (infinite redirects)Max redirect hops; max depth; max fail count
Hot partition (one domain)Per-domain queues + concurrency cap, not global FIFO

Checksum skip: If content_hash unchanged on recrawl, skip re-parse and re-index.

6. Scale evolution

StageChange
MVPSingle machine, Redis queue, SQLite/Postgres metadata, cron fetch
Millions URLsSharded frontier (Kafka/SQS), Bloom filter in Redis, S3 blobs
Billions URLsDistributed metadata (Cassandra/DynamoDB), hierarchical scheduler
JS-heavy sitesHeadless Chrome pool (expensive); separate "render queue"
Global crawlRegional fetchers near targets; respect geo robots rules

7. Recap

  • Crawler = frontier queue + dedup + polite fetch + parse loop
  • Scale bottlenecks are URL metadata and fair scheduling, not HTTP alone
  • Bloom filters save memory; DB confirms duplicates
  • Always discuss robots.txt, rate limits, and retries โ€” interviewers care about ethics and ops

Practice: Draw the crawl loop from seed to new URLs. Explain how you'd prevent one viral domain from blocking the entire crawl.