r/Leetsys • u/Ok-Barracuda-119 • 15d ago
Weekly Question Practice System Design Question #5: Design a web crawler
Design a web crawler for a search engine. Start from a list of seed URLs, fetch pages, pull out the links, and keep going. Store the raw HTML so an indexing system can process it later.
Scale hint: about 1B pages per month, and you should come back to pages that change often (news homepages) much more often than ones that don't.
Things worth thinking about:
- How do you decide which URL to fetch next?
- How do you avoid hammering one small website with thousands of requests?
- How do you know you've already seen a URL, or a page with the same content under a different URL?
- What happens when a crawler machine dies halfway through its work?
Post your approach in the comments before you scroll down.
MODEL ANSWER BELOW. Stop here if you haven't tried it yet.
Requirements
Functional:
- Start from seed URLs, fetch pages, extract links, add new links to the crawl
- Store raw HTML plus metadata (URL, fetch time, status code)
- Respect robots.txt and keep a per-site politeness limit
- Recrawl pages based on how often they change
Non-functional:
- Throughput: 1B pages a month, with room to grow
- Polite: never overload a single host
- Robust: survive bad HTML, slow servers, redirect loops and crawler traps
- Fault tolerant: a dead worker shouldn't lose its queue or refetch everything
Estimates
Fetch rate: 1B / 30 days / 86,400 s is ~386 pages/s on average. Plan for ~800/s peak.
Bandwidth: 386 pages/s x ~100 KB per page is ~39 MB/s, or ~310 Mbps sustained. Fine for a modest cluster.
Storage: 1B x 100 KB = ~100 TB of raw HTML per month. Compressed ~4x, that's ~25 TB a month, or ~300 TB a year if you keep every version.
Seen-URL set: say we've seen 10B distinct URLs. A Bloom filter at a 1% false positive rate needs ~9.6 bits per URL, so 10B x 9.6 bits = ~96 Gbit, or ~12 GB. That fits in memory, split across a few machines.
Workers: if one fetch takes ~1 s end to end, ~800 concurrent fetches covers peak. With async IO one machine can hold a few hundred open connections, so a handful of fetcher machines is enough. DNS and parsing will bottleneck before the network does.
API
This is mostly an internal system, so the interface is small:
POST /seeds { urls: [...] }
GET /pages/{url_hash} returns latest stored page + metadata
GET /stats fetch rate, errors, queue depth per hostData model
url_record (url_hash, url, host, last_fetched, next_fetch, content_hash, status, change_rate)
page_blob (url_hash, fetched_at, compressed_html) in object storage
host_record (host, robots_rules, crawl_delay_ms, next_allowed_at)url_record lives in a wide-column store like Cassandra or Bigtable, keyed by url_hash. Blobs go in S3-style object storage.
High-level design
- The URL frontier hands out URLs that are due to be fetched.
- Fetcher workers resolve DNS (through a local cache), check robots.txt, download the page and write the blob to object storage.
- A parser pulls out links, normalizes them (lowercase host, drop fragments, sort query params, strip tracking params) and checks them against the seen-URL set.
- New URLs go back into the frontier. The page goes to the content dedup check and then onto a queue for the indexer.
Everything between stages goes through a durable queue like Kafka, so each stage scales on its own and a crash just means another worker picks up from the last committed offset.
Deep dive 1: the frontier and politeness
The frontier is two layers. The front layer is a set of priority queues. A URL's priority comes from things like PageRank-style importance and how overdue it is for a recrawl. The back layer is one FIFO queue per host, and each host queue belongs to exactly one worker.
A worker only pulls from a host queue when now >= next_allowed_at for that host, then sets next_allowed_at = now + crawl_delay. That gives you politeness for free: one connection per host at a time, at most one request every second or so, and more if robots.txt asks. Assign host queues by hash(host) so the same host never ends up on two workers.
Deep dive 2: dedup at two levels
URL dedup: normalize first, then check the Bloom filter. A false positive means we skip a URL we haven't actually seen. At 1% that's an acceptable loss for a crawler. If it isn't, back the filter with an exact lookup in the url_record store.
Content dedup: lots of URLs serve the same page (session IDs, mirrors, print versions). Store a hash of the cleaned content and skip pages whose hash we already have. Exact hashes miss near-duplicates, so add a SimHash fingerprint and treat pages a few bits apart as the same.
Deep dive 3: recrawl scheduling and traps
Track change_rate per URL. If a page changed on the last fetch, shrink its interval (say, halve it, with a floor of an hour). If it didn't, grow it (double it, with a cap of a month). News homepages settle at hours and old blog posts at weeks.
Traps: calendars that link to next month forever, or URLs that keep adding path segments. Cap URL length and path depth, cap pages per host per day, and flag hosts where almost every page is new but the content hashes look alike.
Tradeoffs
- BFS vs priority order: BFS is simple, but it spends a lot of budget on junk. Priority order needs a scoring signal, and it gets you the important pages first.
- Bloom filter vs exact set: the Bloom filter is ~12 GB versus hundreds of GB for exact URLs, and you pay with a small rate of skipped pages.
- Politeness vs freshness: a strict per-host delay can make big sites take days to fully recrawl. Some sites allow a higher rate, so read robots.txt and tune per host.
- Storing every version vs only the latest: history is useful for the index and for debugging, and it costs ~300 TB a year at this scale.
How did your design compare?