Courses System design · Part 6 · Case study 6
Case Study: A Web Crawler
A Persian search engine wants to collect web pages so it can make them searchable. A crawler’s job looks simple: fetch a page, find its links, fetch those too. But with millions of pages, three hard questions appear: which URL next? How do we avoid knocking over a small site? How do we avoid fetching a page twice?
1. Requirements
Functional:
- Start from a few URLs (seeds) and fetch pages by following links.
- Store each page’s text for the next stage (building the search index).
- Fetch pages again from time to time, to see changes.
Non-functional:
- Politeness: never overload a site, and follow each site’s
robots.txtrules. - Scale: billions of pages over time.
- Robust against bad pages: endless pages, infinitely generated links, slow servers.
2. Numbers
- Goal: 1 billion pages a month. 1,000,000,000 ÷ (30 × 86,400) ≈ 390 pages a second.
- An HTML page is 100 KB on average: 1 billion × 100 KB = 100 TB a month, before compression.
- If each fetch takes 1 second (slow networks and servers), 390 pages a second needs at least 390 requests in flight. With 40 machines, about 10 concurrent requests each.
The result: the work mostly waits for the network, not the CPU. So each machine needs many concurrent requests, and storage must be cheap and large (for example an object store like S3).
3. Components
- URL frontier: the URLs waiting to be fetched.
- Fetchers: take a URL and fetch the page.
- Parser: pulls out the links and the text.
- “Seen it before?” filter: drops duplicate URLs.
- Storage: page text for indexing.
4. Deep dive: the frontier and politeness
One simple queue is not enough. If a page has 300 links to its own site, 300 URLs from one site arrive in a row, and the fetchers rush at that site together. A small site on a cheap server cannot take that.
The fix: one small queue per site (per domain). The rules:
- Only one request to a site at a time, with at least a few seconds between requests (or whatever that site’s
robots.txtsays). This is a rate limiter per site, this time on our side. - If a site answers slowly or returns 503 or 429, increase the gap: backoff with jitter. This is backpressure that the site sends us.
- If a site fails again and again, leave it alone for a while: a circuit breaker per site.
Priority: not all URLs are worth the same. The front page of a news site that changes every hour matters more than page 500 of a blog’s archive. The frontier has several priority levels.
Sharing the work between machines
We have 40 fetcher machines. If any URL could go to any machine, several machines might fetch from one site at once, and the “one request at a time” rule breaks. So all URLs of one site go to one machine: machine = consistent hash of the domain name. When a machine is added or removed, only a few sites move. That same machine can keep the site’s robots.txt and DNS address in a cache.
5. Deep dive: duplicates
Duplicate URLs
Billions of seen URLs do not fit in a simple in-memory list: 1 billion URLs × 100 bytes = 100 GB. Two ways:
- Hash each URL and keep only the 8-byte hash: 8 GB. This can be split between machines.
- A Bloom filter: a compact structure that, with about 1.2 GB for 1 billion URLs and a 1% error rate, says “definitely not seen” or “probably seen”. The cost of the error: 1% of new pages are never fetched. Usually acceptable.
Before any check, the URL is normalised: lower case, remove #..., sort the parameters, drop tracking parameters such as utm_source. Otherwise one page with ten different URLs is fetched ten times.
Duplicate content
Many pages have the same content under different URLs (print versions, mobile versions, copy sites). Keep a hash of each page’s text; if it is a duplicate, do not store it.
Traps
Some sites, usually by accident, generate endless URLs: a calendar whose “next month” goes on forever. Set a page limit per site, a limit on link depth, and skip very long URLs.
6. Trade-offs
- Freshness versus coverage: every re-fetch of an old page takes the place of a new one. Re-fetch pages that change often sooner.
- Bloom filter: much less memory, at the price of missing a small share of pages.
- Politeness versus speed: longer gaps between requests mean slower crawling of one site, but a crawler that sites do not block.
Labs used
Message queue · Rate limiter · Consistent hashing · Retries · Backpressure · Circuit breaker · Cache