Web Crawler: Deep Dives and Curveballs
You left a plain first-in, first-out queue on the board with a promise attached, and a filter that can lose one new page in a hundred. Both debts come due from minute thirty to minute forty-five, and then you go out onto the open web, where a calendar never ends and a server can send you a byte a second. Four curveballs follow, and the last of them asks you to stop.
The Frontier: Two Orders at Once
The priority you parked comes due here. A URL has an importance, a host has a clock, and a queue has one order. Importance is what you can know before fetching: the standing of its host, from the links other sites give it, and how shallow its path is. Name that clash before you draw anything, because the structure only makes sense as the answer to it.
One global priority queue with a politeness check stalls. Its top fills with pages from the most important hosts, and hardly any of those hosts are ready, because each gives up only a page a second. The worker keeps popping, checking and putting back, while hosts that are ready sit further down.
So keep two structures in each partition: a list per host, sorted by priority, and a heap of hosts, sorted by next allowed time. A heap is a structure that always hands you its smallest entry. A worker pops the host that is due, takes its best URL, fetches, and pushes the host back a gap. Each order now holds in the place it means something.
The cost is that priority holds within a host and not across the web. The best page on a host with a long crawl delay waits out that delay however much it matters, while a minor page on a host that is due goes straight out. A crawl measured over a month can live with that.
Then comes the subtraction from Chapter 6's Backpressure and Load Shedding. Assume one link in ten is new. Of 60,000 checks a second, that is 6,000 new URLs a second, against 1,200 drained. The web is offering fifteen billion new URLs a month, and Scope agreed to take three. The gap is 4,800 a second, and over a day of about 100,000 seconds the frontier gains 480 million URLs. After a month it holds over 12 billion, which is more than four months of fetching, and the monthly freshness you agreed has gone.
So the frontier sheds, at the door. A cap per host, assume 100,000 URLs waiting, keeps the highest priority. A URL that ranks below everything on a full list is refused, and it is not marked seen, because a URL is marked only when admitted, so a later link can find it again. A URL that outranks the bottom of the list pushes that one out. A filter cannot forget, so the one pushed out stays marked until the rebuild that the next section comes to. Those caps do not close the gap alone, since any number of hosts can sit under theirs. So each partition also caps its total at a month of fetching: three billion over 600 partitions is 5 million URLs waiting. Past that, a new URL must outrank the lowest one waiting in that partition, or it is refused. The one pushed out was admitted and will not be fetched this time, so the month's promise holds for what stays in the frontier, and you say so.
The clock has a trigger of its own. You keep it per host because a host is what a site owner sees. Thousands of sites can share one server, though, and when fetches to that one address start to slow, you move the clock from the host to the address, and those sites have to hash to one partition by that address, because a clock needs one owner. Those sites then share one gap between them, and you pay for that in how fast they get crawled.
Deduplication at Three Billion
Here is the filter's price, at worst thirty million skipped pages a month, set against the alternatives at 36 billion URLs. Exact strings take 3.6 TB and give no wrong answers. Hashes of 16 bytes, the width of a UUID, take 576 GB and more, about 4 GB a worker, and at that width they are as good as exact. The filter takes 45 GB and gives up to one wrong yes in a hundred.
Choose the filter while losing up to one new page in a hundred is cheaper than the memory. That memory is about 530 GB, under six cache nodes, so expect the interviewer to call it cheap. Treat the filter's 45 GB as a floor. Every admitted URL takes its bits, and past the 36 billion it was sized for, the wrong yeses climb.
The trigger for an exact answer is an index that promises coverage. Hashes on each partition's owner come first, about 4 GB a worker and no call. A store behind the filter is for when they outgrow that. With a store, every maybe gets confirmed, and nearly everything is a maybe. Nine links in ten are repeats, and the filter says maybe to all of those, so about 54,000 lookups a second go through to the store. It saves you only one call in ten. One key-value partition takes 3,000 reads a second, so that is 18 partitions, or 36 at half their limit. At half their limit, those 36 also take the 6,000 new URLs a second as writes, with room to spare.
Where you land, in a sentence or two: "I'd keep the filter and accept losing up to one new page in a hundred. If the index promises coverage, I'd hold hashes on the workers, about 4 GB each, and move to a key-value store, about thirty-six partitions and a call per link, only when they outgrow that."
A yearly rebuild gives skipped URLs another chance. A filter cannot list what it holds, so the new one is built from the URLs in the frontier and the page store, with a fresh salt, a value mixed into its hashes. The wrong yeses now fall on different URLs, and the ones pushed out at the door are forgotten as well.
Pages get checked as well, since different URLs can turn out to be one page. An exact hash of the body catches copies. A fingerprint, a short value that barely changes when the text barely changes, catches near-duplicates. Assume 64 bits, which over 36 billion pages is 288 GB. How that lookup is indexed does not come up in the room, so say that and keep moving.
A Bloom filter holds 36 billion URLs in 45 GB with no trip, and skips up to one new URL in a hundred until it is rebuilt. Use it when a skipped page is cheaper than the memory.
An exact set holds them in 576 GB of hashes or more and never skips, at the price of memory, and a call per link once it outgrows that. Use it when the index promises coverage.
The Open Web
Everything so far has assumed that sites behave. A calendar's next-month link never ends: each page is a new URL, each URL passes the filter, and the site will feed you for ever. No URL rule spots every trap, and claiming one is the mistake. A budget per registered domain admits that it will not catch them, and stops them anyway. You count it per domain and not per host, because a trap can mint a new hostname with every link, and each would arrive with a fresh budget. The hosts of one domain can sit on different partitions, so their owners add their counts in batches to one shared counter. Assume 100,000 pages a month for a domain with no standing, raised as it earns links from other sites. On one host, at a second a page, that is about a day of one connection, and then the trap is over. A limit on path depth catches the site whose URLs grow a directory with every click.
What the budget costs is a real site cut off at 100,000 pages until its standing grows.
Slow servers get a deadline: assume 30 seconds and 10 MB for one fetch. A server dripping a byte a second costs one connection for those 30 seconds. Since a host gets one connection, that server cannot hold a second, and the worker's loop carries on with its other fetches, so it cannot occupy a worker. Politeness protects the crawler too. The deadline's cost is a real page that takes longer or runs larger than that, which comes back cut short or not at all. A link can also point inside your own network, at a private address or the cloud's metadata service. So a worker checks the address it is about to connect to, on every redirect as well, and refuses private and local ones, and the rendering tier keeps the same rule.
Curveballs
"News sites need re-crawling hourly, the rest monthly." A URL gets a due time, not a queue of its own, and the due time was in the entity from Scope. After a fetch the URL stays on its host's list with a new time, and the worker passes over it until that time comes, so it never goes near the filter. Assume 100,000 such pages. Hourly, over about 730 hours in a month, is 73 million fetches, 2 percent of the budget, or 28 a second. At that size it fits inside the three billion, and at a million such pages it would take a quarter of it, so the count is what you ask for next. The same sum limits the rest: a monthly pass fits three billion pages at most, so past that the budget has to choose what comes round again.
Adapt the interval to how often a page changes, and send a conditional request, which quotes the version tag you stored and asks for the page only if it has changed, so an unchanged page costs a reply and not a body.
"Half the web is built by JavaScript." Do not render everything. Assume 5 percent of pages come back thin, with little text in their HTML, and those go on a work queue to a rendering tier, the shape from Chapter 6's Queues, Logs and Pub-Sub. That is 60 a second. At an assumed one a second per rendering worker, that is 60 more workers, 40 percent on top of the 150, for one page in twenty. They obey the same host's clock: the partition's owner releases a rendering job only when that host is due, because a rendered page is more requests to that site. If half of all pages really came back thin, the same sum would give 600 workers, four times the crawl, so you measure the share before you build the tier.
"One site generates infinite links." The budget ends it, at the cap, because it counts the whole domain. The filter cannot help, because every URL is new.
"A site asks you to stop. Now." Waiting for robots rules is no answer, since your cached copy can last up to a day. Put the host on a block list that workers watch, and check it before every fetch, not only at the frontier, since a worker may hold thousands of that host's URLs. The rendering tier checks it too. One fetch may be in flight when the news lands, and you say so, because nothing can recall it. The block lives apart from the seen set, to survive rediscovery: links to that site keep arriving, and a rebuilt filter would have forgotten it.
How Each Level Answers
A mid-level candidate gets from three billion to 1,200 a second and 150 workers, and partitions by host once the small site is raised. They reach for a Bloom filter when asked how to remember 36 billion URLs. They need a hint before they say what a wrong yes costs, and they treat a dead worker as a retry.
Senior candidates raise politeness in Scope and make it the reason for the key. They size the filter and say what it loses. They say, with nobody asking, that a seen URL that is lost is gone for good, and they put the fencing check in the frontier.
Staff candidates treat the crawl as a budget to spend: which sites get how much of three billion fetches. They raise what the crawl owes the web as a standing cost of being allowed to crawl, down to how a site's owner reaches a human. And they ask what the index does with duplicates, because that sets how far the deduplication has to go.
- Drawing the frontier in fine detail and never saying what it costs a page on a slow host. The interviewer asks, and the structure has no answer of its own.
- Waiting for the robots rules to expire when a site asks you to stop. That is up to a day of fetches after someone said stop.
- Rendering every page because some need it. At one a second a worker, that is 1,200 rendering workers for the sake of one page in twenty.
- Claiming a detector that catches every trap. The interviewer names one it misses, where a budget admits it will not catch them and still ends them.
- The two orders of the frontier named as the problem before the structure is drawn.
- A budget given a number, and who it cuts off.
- One thing the design cannot do, said aloud, such as recalling a fetch in flight.
Knowledge Check
"A site asks you to stop. Now." What is your best move?
- Wait for its new robots rules, as the cached copy expires within a day
- Put the host on a block list that workers check before every fetch
- Mark all of its URLs in the seen set, so that none are queued again
- Delete its URLs from the frontier, and let workers finish what they hold
"News sites need re-crawling hourly, the rest monthly." Assume 100,000 such pages. What do they cost?
- About 2.4 million fetches a month, since a day holds 24 hourly passes
- About 100,000 fetches a month, too small a share of the budget to price
- About 73 million fetches a month, 2 percent of the budget, 28 a second
- About 730 million fetches a month, a quarter of the budget, 280 a second
"Half the web is built by JavaScript." Which pages go to the rendering tier?
- All of them, because an index ought to see what a browser shows
- Every page of any site where one page has been found to need it
- None, because the scope you agreed for this crawl was HTML alone
- Those whose HTML comes back thin, assumed to be one page in twenty
"One site generates infinite links." What ends it?
- The budget per domain, which stops that site at its cap for the month
- The seen filter, which notices that its URLs all look much the same
- A URL rule, written to recognise calendars and other traps like them
- The politeness gap, which holds that site to a page every second
You got correct