How to design a web crawler
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Politeness and priority in the frontier
- Duplicates and crawler traps
- Failures and scaling out
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
The problem
A web crawler starts from a few seed URLs, downloads each page, pulls the links out of it and queues the ones it hasn't seen, until it has a copy of a large part of the web in storage. The design turns on a tension: the crawl as a whole has to be fast, about 400 pages a second for a billion pages a month, while each individual site must be touched slowly and politely. Most links on a page point to the host the page itself lives on, so a plain queue floods whichever site you just visited. The answer is a frontier that orders URLs by priority at the front and by host at the back, workers that cache DNS and robots rules so they aren't waiting on the network for things that don't change, a content hash and a seen-URL set to skip the roughly 30 percent of pages that repeat, and a fetch stage split from a parse stage so a crashed machine loses nothing it hadn't acknowledged.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
How much of the web, and how fast? Is this a continuous crawl or a one-off with a deadline? | The rate decides how many fetches must be in flight, how many hosts must be active at once, and whether a single machine can hold the frontier and the seen-URL set. | A billion pages a month, about 400 a second on average and 800 at peak, running continuously. Be ready for a stretch question: 10 billion pages in under 5 days. |
What counts as a page? HTML only, or also PDFs, images and pages that need JavaScript to render? | Rendering JavaScript makes each page far more expensive to fetch and changes the worker from an HTTP client into a headless browser. Other content types change parsing and storage. | HTML only, served as plain HTTP responses. Pages behind a login and pages that need JavaScript are out of scope. The design should leave room for new content types later. |
What politeness rules do we follow? robots.txt, a per-host rate, both? | Politeness is the constraint that shapes the frontier. A per-host rate means the frontier has to know which host each URL belongs to and when that host was last touched. | Obey robots.txt, including a crawl delay when a site sets one, and send no more than 1 request a second to any single domain. |
How fresh does the store have to be? Do we re-crawl pages, and how do we decide which ones? | Re-crawling means URLs go back into the frontier after a download, so the frontier needs a priority that reflects how often a page changes, not a simple first-in first-out order. | Yes, re-crawl pages that are new or edited often. Freshness is a priority signal, not a hard deadline. |
What do we do with duplicate content? Exact copies only, or near copies where just an ad or a timestamp changed? | Exact duplicates are a hash lookup. Near duplicates need a similarity hash and a tolerance, which is a different piece of work. | About 30 percent of pages repeat content already stored. Skip exact duplicates; near duplicates are a stretch goal to mention, not to build. |
How long do we keep pages, and who reads them? | Retention sets the storage bill, and the consumer decides the storage layout: an indexer reading in bulk wants large files, not one small object per page. | Keep pages for 5 years. The indexer is a separate system; store the HTML and the extracted links and stop there. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
1 billion pages a month (Alex Xu), or 10 billion pages in under 5 days (Hello Interview) | About 400 pages a second on average means hundreds of fetches in flight at any moment, a DNS and disk load that argues for several machines even though one well-connected link could carry the bytes, and a frontier and seen-URL set that outgrow memory and live mostly on disk. The stretch figure is about 58 times the base rate and turns bandwidth per machine into the sizing question. |
Polite: obey robots.txt and send at most 1 request a second to a domain | The frontier must know each URL's host and when that host was last touched, which is why it has one queue per host and a due-time heap in front of the workers. Robots rules are fetched once per host and cached. It also means the crawl needs at least 400 distinct hosts in progress at once to reach its rate. |
Keeps going through bad HTML, slow servers, crawler traps and crashed workers | Parsers tolerate broken markup, fetches have timeouts, URLs are filtered by length and depth with a block list, and a URL stays in the queue until a worker acknowledges it so a dead worker's work is picked up by another. |
Keeps pages for 5 years | About 30 PB before skipping duplicates. That is blob storage packed into large files, not a database, and the duplicate check pays for itself in storage costs. |
Extensible to new content types | The fetch stage and the parse stage are separate, so a new parser (PDF, images) plugs in behind the same raw-page store without touching the frontier or the fetchers. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Pages per second from a billion a month | A month is about 30 × 24 × 3,600 ≈ 2,592,000 seconds; 1 billion ÷ 2,592,000 ≈ 386 pages a second, rounded to 400; peak taken as twice that, 800 | About 400 pages a second, 800 at peak | The fetch rate the whole design has to sustain, and the figure the later estimates start from. |
Storage with and without the duplicate check | 1 billion × 500 KB = 500 TB a month; × 60 months = 30 PB. Skipping the 30% that repeat: 500 TB × 0.7 = 350 TB a month, about 21 PB over 5 years; the check saves 30 PB − 21 PB ≈ 9 PB | About 30 PB raw, about 21 PB after duplicates are skipped | Why page bodies belong in blob storage and why the content-hash check is worth a lookup per page: it saves about 9 PB. |
Fetches in flight and host queues | Example assumption: a fetch takes about 2 seconds of wall time end to end (DNS, connect, download of 500 KB, slow servers). 400 pages a second × 2 seconds = 800 fetches in flight; 1,600 at peak. Mercator's rule of roughly three host queues per worker thread gives about 2,400 to 4,800 host queues | About 800 to 1,600 fetches in flight, a few thousand host queues | How many worker threads and host queues the frontier must juggle, and why it spans several machines. |
Hosts needed at once under the politeness rule | The politeness limit of 1 request a second to each domain means 400 pages a second needs at least 400 distinct hosts being fetched in the same second; 800 at peak. A single site with 10,000 pages takes at least 10,000 seconds, about 2.8 hours, however many workers you have | At least 400 hosts active at a time, 800 at peak | Why a crawl of a few large sites falls short of the target rate, and why the frontier must spread work across many hosts rather than drain one. |
Bandwidth | 400 × 500 KB = 200 MB a second, about 1.6 Gbps on average; 800 × 500 KB = 400 MB a second, about 3.2 Gbps at peak | About 1.6 Gbps average, 3.2 Gbps at peak | The network a crawler machine or cluster must have; one well-connected machine is plausible, and a few machines give headroom. |
Worker time lost to DNS before and after a parallel resolver | The Mercator paper reports that DNS lookups took 87% of each thread's elapsed time with the system's resolver, which serialized lookups, and that a multi-threaded resolver of their own cut that share to 25% | 87% of each thread's elapsed time before the fix, 25% after | Why DNS is the first thing to cache and parallelize in a downloader, ahead of anything to do with HTML. |
Memory for the seen-URL set | Example assumption: over 5 years the crawl fetches 60 billion pages (1 billion a month × 60); take that as the upper bound on distinct URLs, since re-crawls revisit URLs already in the set. Mercator keeps a fixed-size checksum per URL instead of the text; at 8 bytes a checksum, 60 billion × 8 bytes = 480 GB before table overhead. A Bloom filter at a 1% false-positive rate needs about 9.6 bits a URL, roughly 1.2 GB per billion, so about 72 GB | Up to about 480 GB of checksums as a checksum set, up to about 72 GB as a Bloom filter | Whether the seen-URL check fits in one machine's memory: both are large for one machine, the checksum set especially, so the checksum set is sharded by host or kept on disk with a cache, and a Bloom filter is a memory trade, not a magic fix. |
The stretch target against the base rate | 10 billion pages ÷ (5 days × 86,400 seconds = 432,000 seconds) ≈ 23,150 pages a second, about 58 times the base rate of 400 | About 23,150 pages a second | How much of the design the stretch question changes: the frontier shape stays, but the number of machines, the bandwidth and the seen-URL set grow by about 58 times. |
Core entities
| Entity | What it holds |
|---|---|
URL | One row per address the crawl knows about: where it lives, how important it is, how far from a seed it was found, and when it was last fetched. |
Page | One row per stored download: which URL it came from, a hash of its content, and where the bytes sit in blob storage. |
Domain | One row per host: its robots rules, how long to wait between requests, and the last time we touched it. |
The API
| Endpoint | Notes |
|---|---|
| Seeds enter the frontier like any other URL, at the top priority and depth zero. A good seed list spans many hosts, since the politeness rule means a crawl of one host runs at one page a second no matter how many workers you have. |
| Raw HTML goes to blob storage; the links, the content hash and the fetch metadata go to the page and URL tables. The indexer is the consumer and reads the blobs in bulk, so pack many pages into each object rather than one object per page. |
| add places the URL in a front queue by priority; next pops the host whose wait has expired from the due-time heap and hands over the head of that host's queue. A URL handed out stays in flight until the worker acknowledges it, so a crash returns it to the queue. |
The high-level design
Step 1 of 4
The loop: frontier, fetcher, parser, store
Start with the loop that makes a crawler. Seed URLs go into a frontier, which is a queue of URLs to fetch. Fetcher workers take a URL, download the page and hand the HTML to a parser, which pulls out the links, adds them to the frontier and writes the page to blob storage, the cheap place for about 500 TB a month. This is enough to crawl, but the frontier is a single queue that hands out URLs in the order they arrived, and nothing yet stops the same URL from being queued again or the same site from being hit by hundreds of workers at once.
What breaks first
The deep dives
Politeness and priority in the frontier. A page links to thousands of URLs on the same small site and they all enter the frontier together. How does the crawl work through them without hammering the site, while still getting to the pages that matter most first?
A concept that helps here: robots.txt and crawl politeness
A concept that helps here: Fair queuing across tenantsA concept that helps here: Caching strategiesDuplicates and crawler traps. About 30 percent of downloaded pages repeat content you already hold, and some sites generate a new link each time you follow one. How does the design detect a repeat, decide whether a URL is new, and keep a calendar that links to the next day without end from eating the crawl?
Failures and scaling out. A crawler machine dies at night with thousands of URLs handed out but not fetched. What happens to those URLs, what happens to a host whose requests time out again and again, and how does the design grow from one machine to several?
Concepts this guide uses
Sources
- Slides summarizing Heydon and Najork, Mercator: A Scalable, Extensible Web Crawler (slidetodoc)
- Crawlex blog: URL frontier design, from Mercator to modern priority-queue crawlers
- Amazon SQS Developer Guide: visibility timeout
- AWS CloudFormation: AWS::SQS::Queue (RedrivePolicy and maxReceiveCount)
- Wikipedia: Bloom filter
- Wikipedia: Spider trap
- Wikipedia: Web crawler
- Patrick Stox: Crawl delay in robots.txt
- High Scalability: Consistent hashing algorithm