Web Crawler
A polite, massively parallel crawler — the engine behind search and AI training data — via the TOGAF ADM.
Web crawler, the ADM way, in 30 seconds
- A (Vision): fetch and store a huge slice of the web; principles — be polite, never get stuck, scale horizontally.
- B (Business): capabilities — fetch a page, extract links, avoid re-crawling, respect site rules.
- C (Data/App): a URL frontier (queue), a fetcher, a parser, a dedup set, a content store.
- D (Technology): the loop — frontier → fetch → parse → dedup (bloom filter) → store → enqueue new links; sharded, rate-limited, polite.
- E–H: start single-site, scale to many workers; govern with politeness and freshness policies.
- Lesson: 'be polite' and 'never get stuck' (Phase A) force the per-domain rate limiting and dedup of Phase D.
Exploring a library with no map
Imagine you're sent into the largest library on earth with no catalogue and one rule: read every book, and every book lists other books you should also read. You start with a handful of titles, read them, write down the new titles they mention, and keep going. That's a web crawler — start from a few seed pages, fetch them, pull out the links they point to, fetch those, and repeat until you've covered a huge slice of the web.
The loop is trivial. What's hard is doing it without wrecking yourself or the library. You must not read the same book a thousand times (some books cite each other in circles), you must not camp in the photocopier room hammering one librarian (a small site), and you must not fall into a shelf of infinitely-generated books that never ends (a crawler trap). At web scale — billions of pages, thousands of parallel readers — those three disciplines, don't repeat, be polite, don't get stuck, are the entire design.
What makes it hard — the problem before the solution
- The web is cyclic and huge. Pages link in circles, so without memory of what you've seen you loop forever and never finish.
- 'Have I seen this?' can't fit in memory. An exact set of billions of URLs would need enormous RAM — you need a space-cheap approximation.
- Sites are fragile and watchful. Hit one site too fast and you get IP-banned or cause real harm; politeness is mandatory, not optional.
- Traps exist. Infinite calendars and session-ID URLs generate endless fake pages designed (or accidentally able) to swallow a naive crawler.
Why the ADM fits
A crawler sounds like a simple loop — fetch, find links, repeat — but at web scale it's a swarm that must avoid trapping itself, avoid hammering sites, and avoid re-crawling the same pages forever. The ADM keeps those concerns in order: the politeness and no-stuck principles from Phase A dictate the rate limiting and dedup that dominate Phase D.
Phase A — Architecture Vision
- Vision: systematically fetch and store a large portion of the web for search or data pipelines.
- Stakeholders: the indexing/ML pipeline downstream; the websites being crawled (who must not be overwhelmed); operators (who need it to run for days without wedging).
- Principles: be polite (honor robots.txt and per-site rate limits); never get stuck (avoid infinite loops and traps); scale horizontally (thousands of fetchers).
Phase B — Business Architecture
- Value stream: start from seed URLs → fetch pages → extract new links → fetch those → ... → store content.
- Fetch a page — download HTML (and handle errors, redirects, timeouts).
- Extract links — parse out URLs to crawl next.
- Avoid re-crawling — don't fetch the same URL twice.
- Respect site rules — robots.txt, crawl-delay, per-domain limits.
Phase C — Information Systems Architecture (Data & Application)
Data architecture
- URL frontier: the queue of URLs waiting to be crawled (prioritized).
- Seen-set: which URLs we've already crawled (must be space-efficient at billions of URLs).
- Content store: the fetched pages (and extracted text) for downstream use.
- Per-domain state: last-crawl time and crawl-delay to stay polite.
Application architecture
- A pipeline of stages — frontier, fetcher, parser, storage — connected by queues.
- Many parallel fetcher workers for throughput.
- A scheduler that enforces politeness per domain.
Phase D — Technology Architecture (the core)
D1 — The crawl loop
- Pull a URL from the frontier (a queue).
- Fetch the page (with timeouts, retries, redirect handling).
- Parse out content and new links.
- Store the content; enqueue the new links back into the frontier — the loop that grows the crawl.
def crawl(seed_urls):
for url in seed_urls:
frontier.enqueue(url) # frontier = the BFS queue
while not frontier.empty():
url = frontier.dequeue()
if seen.contains(url): # skip anything already crawled
continue
wait_for_domain_delay(domain_of(url)) # politeness: honor crawl-delay per site
html = fetch(url) # with timeouts / retries / redirects
seen.add(url) # mark before enqueuing children
store(url, html)
for link in extract_links(html): # breadth-first growth
if not seen.contains(link):
frontier.enqueue(link)D2 — Avoiding re-crawl (dedup at scale)
- Billions of URLs make an exact 'have I seen this?' set too big for memory.
- Use a bloom filter (see Bloom filters): it answers 'definitely new' or 'probably seen' in tiny space.
- 'Definitely new' → crawl it; 'probably seen' → skip (accepting a rare missed page as the cost of huge memory savings).
- This directly serves the never get stuck principle — without dedup, the crawler loops on the same pages forever.
def seen_before(url):
normalized = normalize(url) # strip fragments, sort params, lowercase host
bits = [h(normalized) % SIZE for h in HASHES] # k independent hash positions
if all(bloom[b] for b in bits):
return True # ALL bits set -> probably seen (rare false +)
for b in bits:
bloom[b] = 1 # definitely new -> record it
return False # tiny memory for billions of URLsWhy accept a data structure that can be wrong? Because the exact alternative doesn't fit. A precise set (a hash set or a database of every URL seen) is always correct, but billions of URLs at ~100 bytes each is hundreds of gigabytes to keep hot — and every crawl step pays a lookup against it. A bloom filter answers the same question in a few bits per URL, fast and in memory. Its only error is a rare false positive: it occasionally says 'probably seen' about a genuinely new URL, so you skip a page you could have crawled. For a crawler aiming at most of the web, skipping a handful of pages is a trivial price for cutting memory by orders of magnitude.
| Seen-set option | What it buys | The gap it can't fill |
|---|---|---|
| Exact in-memory set | Never wrong. | Hundreds of GB for billions of URLs — won't stay in RAM. |
| Database of seen URLs | Exact and durable. | A disk lookup per crawl step — far too slow at web scale. |
| Bloom filter (chosen) | Tiny memory, fast, 'definitely new' is reliable. | Rare false positives skip a few real pages — acceptable for broad coverage. |
D3 — Politeness (rate limiting per domain)
- Honor robots.txt and any crawl-delay the site specifies.
- Rate limit per domain (see Rate limiting) so a thousand workers don't all hit one small site at once.
- Partition the frontier by domain so each domain's politeness is easy to enforce, and spread domains across workers.
def may_fetch(url, now):
domain = domain_of(url)
rules = robots_cache.get(domain) # fetched + cached once per domain
if not rules.allows(path_of(url)):
return False # robots.txt says keep out
last = last_fetch_time.get(domain, 0)
delay = max(rules.crawl_delay, DEFAULT_DELAY) # honor site's crawl-delay, else our floor
if now - last < delay:
return False # too soon -> requeue, go fetch another domain
last_fetch_time[domain] = now
return TrueD4 — Scale & traps
- Shard the frontier and seen-set (see Sharding) across nodes so no single structure is a bottleneck.
- Avoid crawler traps: infinite calendars, session-ID URLs, and cycles — cap depth, normalize URLs, and detect near-duplicate pages.
- Prioritize the frontier (important/fresh pages first) rather than blind breadth-first.
def normalize(url):
u = url.lower().split("#")[0] # drop fragment, lowercase host
params = drop_tracking(sorted_params(u)) # sort params, strip session/tracking ids
return rebuild(u, params) # one canonical form per real page
def should_enqueue(url, depth):
if depth > MAX_DEPTH:
return False # infinite calendars can't recurse forever
n = normalize(url)
return not seen_before(n) # dedup on the CANONICAL url, not the raw oneScaling & bottlenecks — what breaks first
A crawler is a swarm, so its failures are about the swarm wedging, overcrawling, or tipping over — not raw throughput. Here's the order.
| Breaks first as you scale | The symptom | The fix |
|---|---|---|
| Frontier skew by domain | A few huge domains dominate the queue; small sites starve. | Partition the frontier by domain; round-robin across domains for fairness. |
| Politeness vs throughput | Per-domain limits idle your fetchers waiting on one slow site. | Interleave many domains per worker so waiting on one doesn't stall the rest. |
| Bloom filter fills up | False-positive rate climbs; you start skipping real pages. | Size it for the target URL count up front; shard or roll to a fresh filter. |
| Crawler traps | Infinite calendars / session URLs flood the frontier with junk. | Cap depth, normalize URLs, detect near-duplicate pages, blacklist trap patterns. |
The tension to name out loud is politeness versus throughput: being polite means waiting between hits to one site, but waiting idles an expensive fetcher. The resolution is to give each worker a wide mix of domains so that while it waits on one site's crawl-delay, it's busy fetching from fifty others — politeness and utilization at the same time.
Phases E & F — Migration Planning
- Start with a single-worker crawl of a few seed sites to prove the loop and politeness.
- Scale to many workers with a sharded frontier and distributed seen-set.
- Add prioritization and freshness re-crawling once breadth works.
Phases G & H — Governance & Change
- Governance (G): politeness policies (never exceed per-domain limits), freshness SLOs (how often to re-crawl), and storage budgets.
- Change (H): adding content types (PDFs, JSON APIs) is a new parser behind the same loop — a bounded change.
A web crawler is a lesson in bounded, polite, massively-parallel work — a loop that must never run away with itself.
Why it matters
- It's the engine behind search indexes and much AI training/retrieval data.
- It forces dedup-at-scale, politeness, and trap-avoidance — real constraints, not toy ones.
- It shows a pipeline of queue-connected stages scaling horizontally.
If ignored
- No dedup and the crawler loops forever on the same pages, never making progress.
- No politeness and you get IP-banned and harm the sites you crawl.
- No trap handling and infinite calendars/session URLs swallow the whole crawl.
Where it fits
- Phase D holds the crawl loop, bloom-filter dedup, per-domain rate limiting, and sharding.
- Phase A's politeness and no-stuck principles drive those D mechanisms.
- Sits beside the interview framework as a second way to structure the answer.
Related topics
- Message queues: the URL frontier and stage pipeline
- Bloom filters: space-efficient 'have I seen this URL?' dedup
- Rate limiting: per-domain politeness
- Sharding: distributes the frontier and seen-set at scale
Interview lens
Questions it unlocks
- Design a web crawler.
- How do you avoid crawling the same URL twice at web scale?
- How do you crawl politely without getting banned?
60-second answer
I frame it as an ADM walk. Phase A's principles — be polite, never get stuck, scale horizontally — pre-decide the hard parts. Phase B names fetch, extract links, avoid re-crawl, and respect site rules. Phase C sets up a URL frontier, a seen-set, a content store, and per-domain state. Phase D is the loop: pull from the frontier, fetch, parse, store, and enqueue new links — with a bloom filter for space-efficient dedup so we never loop forever, per-domain rate limiting plus robots.txt for politeness, a sharded frontier for scale, and trap handling for infinite calendars and session URLs. E–H start single-worker then scale out, governed by politeness and freshness policies. Every Phase D mechanism traces back to a Phase A principle.
Common traps
- No dedup, so the crawler loops forever and never progresses.
- Hammering sites without per-domain politeness and getting banned.
- Ignoring crawler traps like infinite calendars and session-ID URLs.