Web Crawler: Scope
Three billion pages a month sounds like the hard part, and by minute ten it is the easy one: one division gives you about 1,200 fetches a second, and another gives you 150 workers. What those first ten minutes really have to bring out is what the question leaves unsaid, namely what you owe the sites you fetch from, and how many links come back with every page.
The question does name its reader: "for a search index". So something else builds the index, and your job ends when a page is handed over. That takes ranking off the board before you have asked anything, and your questions can go to the crawl itself.
Five Questions Before Any Sizing
Five answers fix the shape of this crawl. The third is the one a candidate has to raise alone, because no interviewer will raise it for you.
- Is three billion the fetches, or the distinct pages? This sets how fast the seen set grows, the record of every URL you have met. Agreed: three billion fetches a month and at most three billion new URLs. Re-crawling comes back as a curveball.
- What does the index want: the HTML, or images, scripts and a rendered result? This sets the bytes and the CPU per page. Agreed: HTML only, and assume 100 KB a page. The median page with its images and scripts is about 2.3 MB, which would be about 7 PB a month of bytes the index never reads.
- What do we owe the sites? This sets the whole shape of the frontier, the list of URLs still to fetch. Agreed: obey robots rules, the file a site publishes to say what crawlers may fetch; keep one connection per host; and leave at least a second between fetches, or longer where a site asks for a crawl delay, a common extension that the robots standard leaves out.
- How fresh must the index be? This sets whether URLs carry a schedule. Agreed: monthly for now, meaning a URL you admit is fetched within a month. Re-fetching what you already hold spends the same three billion, so it comes back with re-crawling as a curveball.
- How long do we keep a page, and who reads it? This sets the store. Agreed: the latest copy, for a year at most, read by the index builder.
The first of those comes before any arithmetic, and the reason can come with it: "Is three billion fetches or distinct pages? I want that before any sizing, because it tells me how fast the seen set grows."
What the Crawler Promises
The functional list is short. Start from seeds, the hand-picked URLs a crawl begins with. Fetch each page, store it and parse it. Queue the links not yet seen. Honour robots rules.
The non-functional list carries more weight. Never overload a site. Reach three billion fetches a month. Lose no URLs when a worker dies. And keep a trap, a site that generates endless links, from eating the crawl.
Leave four things out by name: building the index, ranking, logins and images. The crawler hands pages over, and what happens next is someone else's design. Politeness stays in, and it stays in now, because twenty minutes from here it becomes the reason for the partition key. Raised later, it sounds like a feature you forgot.
A Rate You Set Yourself
A month is about 2.6 million seconds. Three billion over 2.6 million is about 1,150 a second, so call it 1,200. No factor for peaks goes on top. A peak describes users who arrive when they like, and here nobody arrives: you choose this pace, and you can hold it flat.
Next, what one worker does. Assume a worker is one loop on one core, handling about 8 pages a second. Waiting on many sockets at once costs it little, so nearly all of that time goes on parsing. 1,200 over 8 is 150 workers. Turn the sum around and one worker alone does about 20 million pages a month, a number First Design leans on.
Storage settles the kind of store. Three billion pages at 100 KB is 300 TB a month, and 3.6 PB after a year, before the store's own copies. Whole pages, written once and read later by a batch job, belong in an object store. One takes at least 3,500 writes a second per prefix, the leading part of an object's key, and you need 1,200.
Bandwidth is the sum to drop, and you drop it on purpose. 1,200 pages at 100 KB is 120 MB a second. A 10-gigabit link carries 1.25 GB a second, so the crawl takes a tenth of one link. Tell the interviewer so, and point at the number that replaces it: "I'm dropping bandwidth: a hundred and twenty megabytes a second on a ten-gigabit link decides nothing. Sixty thousand link checks a second does."
The estimator below runs these sums from a day's pages, about 100 million. Its answers come out at about 1,200 a second and 145 workers, and the 150 you say in the room is the rounded 1,200 over 8. It calls a fetch a write, because every fetch stores a page. Drag the page size toward a whole page's 2.3 MB and watch what images would cost you: bandwidth outgrows one link, and storage grows more than twenty times.
60,000 Link Checks
Now the sum candidates miss, because the question never mentions links. Assume 50 links a page. At 1,200 pages a second, that is 60,000 times a second that something has to ask whether it has met this URL before, fifty times the fetch rate. The deduplication is built for this number, and every design from here on gets judged against it.
Then its size. Up to three billion new URLs a month is up to 36 billion after a year. Assume about 100 bytes a URL, and that is 3.6 TB. At about 100 GB a node, that is thirty-six mid-size cache nodes of memory, just to remember strings. Nothing needs solving yet, but the interviewer has heard you notice.
A Queue, a Store and No API
There is no public API, and you can say that plainly, because nobody calls a crawler. A queue sits on one side and an object store on the other, and the contract is what those two hold. Three entities carry it. A URL is a normalised string, with its scheme and host lowercased and its fragment dropped so that one address is not queued under five spellings, though the path keeps its case, because capitals there can name a different page. It also holds its host, a priority and when it is next due. A host holds its robots rules, its crawl delay and the next time it may be fetched. A stored page holds its URL, when it was fetched, the version tag the site sent with it, and the body.
url: normalized_url, host, priority, due_at host: robots_rules, crawl_delay, next_allowed_at page: url, fetched_at, etag, body
- Designing a 7 PB month around images nobody reads. The interviewer asks who uses them, and the store you just sized has no reader.
- Putting a factor of two for peaks on a rate you set yourself. The interviewer asks who is peaking, and it shows the estimate was a habit.
- Counting fetches and never links. The seen set's load is fifty times the fetch rate, and the design gets sized for the smaller number.
- Leaving politeness out of Scope. It arrives as a late feature, when it is the thing that shapes the frontier.
- What the crawl owes the sites comes up without being asked.
- Three billion a month becomes 1,200 a second and 150 workers, aloud.
- 60,000 link checks is named as the number that matters.
Knowledge Check
You have time for one question before sizing anything. Which answer reshapes this crawler most?
- Whether three billion counts the fetches or the distinct pages
- How many bytes of images and scripts come with each fetched page
- What the crawl owes the sites: robots rules, one connection, a gap
- How long the store has to keep the latest copy of each stored page
Three billion pages a month. What rate do you say out loud?
- About 2,400 a second: three billion over a month, doubled for peaks
- About 35,000 a second: three billion over one day of 86,400 seconds
- About 100 a second: three billion over a year of 31.5 million seconds
- About 1,200 a second: three billion over 2.6 million seconds, rounded
Why does the 1,200 a second carry no factor for peaks?
- You set this pace yourself, and no users arrive to bunch it up
- Sites spread their replies evenly across every hour of the day
- A peak would only matter to bandwidth, and that sum gets dropped
- The object store takes any burst at 3,500 writes for each prefix
One of these sums gets said and dropped. Which one?
- 60,000 link checks a second, fifty times the rate of the fetches
- 120 MB a second of pages on a link that carries 1.25 GB a second
- 3.6 PB of pages after a year, before the store's own copies are made
- 3.6 TB of URL strings in the seen set after twelve months of crawl
You got correct