Lesson 2 · Minutes 10-20

Web Crawler: First Design

Problem

The smallest crawler that keeps every promise from Scope is a single loop, and between minute ten and minute twenty that loop is the whole board. It takes a URL, asks whether the host may be fetched yet, fetches, stores, parses, and queues whatever it has not met before. Two URLs go through it, one fetched and one put back; then it gets its defence, and then you name its shortfall before anyone asks.

A hundred and fifty workers and a partitioned frontier are already in your head, and drawing them now would throw away the reason for them. This design is polite only because it is alone, and that is worth showing while it is still true. What the interviewer does to it comes next: the quiz closing this page is their side of the following ten minutes, and Scaling It keeps to its order.

One Loop on One Machine

One worker runs on one machine. A URL queue on disk holds what is still to fetch, and it starts out holding the seeds. A hash set in memory holds every URL met so far. Pages go to the object store Scope chose. On the far side is the web, which you do not control and which has made you no promises.

The loop has six steps. Take a URL from the queue. Check the host's robots rules and its next allowed time, and put the URL back if it is too soon. Fetch the page. Store it. Parse it and normalise its links. Check each link against the seen set, and queue the new ones. A host met for the first time has its rules fetched before anything else, that fetch counts against its clock, and the copy is kept for up to a day. If that fetch finds no file, a 404, there are no rules. If the server errors or does not answer, the host is off limits until it does.

The loop never sits blocked on one slow site. It holds many fetches open, at most one to a host, and deals with each reply as it lands, so its one core goes on parsing and not on waiting. That is how it reaches 8 pages a second.

The queue is the plainest kind, and you flag that as a choice and move on: "The queue is first in, first out on this pass. Which page matters most gets its turn in the deep dive."

The first design: one loop on one machine
Seedsstarting URLs The webnot yours URL queueon disk, in order Workerone loop, host clocks Seen sethash set in memory Object storepages, 100 KB each next fetch links new page readwrite

One URL, Fifty Links, One Put Back

Follow a URL on a host last fetched two seconds ago. Its rules allow the path, and the second's gap has passed, so the worker moves the host's next allowed time a second ahead and fetches. About 100 KB comes back and goes to the object store. Parsing finds fifty links. Each one is normalised and checked against the seen set. Those already there are dropped, and the rest are added to the set and go to the back of the queue. One URL has become one stored page and fifty answers from the set.

Now a second URL on the same host, half a second after that fetch. The check finds the next allowed time still half a second away. Nothing is fetched, the URL goes back on the queue, and the loop takes the next one. The site never knows it was considered. Here politeness costs one look at one number in memory.

Twenty Million Pages Is a Real Crawl

Defend it before you fault it. It starts from seeds, stores pages, queues new links and honours robots rules, so every functional requirement is met. And it is polite by construction: one loop knows every host's clock, because nothing else is fetching. There is no lock and nothing to coordinate.

At 8 pages a second it does about 20 million pages a month, a real crawl for a vertical search engine, one that covers a single subject. Its seen set fits as well. Twenty million a month is 240 million URLs after a year, and at about 100 bytes each that is 24 GB in memory. For a crawl of that size, you would ship this.

150 Times Too Slow

Three limits get named now and left alone. One loop is 150 times too slow for three billion. The queue and the seen set share one machine's fate, so losing that machine loses the crawl's memory of where it was. And nothing says which pages matter first.

The first of the three is the one to offer unprompted, as a ratio, along with how it would show up: "Twenty million pages a month is this loop's ceiling. Three billion is a hundred and fifty times that, and I'd see it first as the queue growing faster than the loop drains it."

Interview Pitfalls
  • Drawing 150 workers and a partitioned frontier at minute ten. Politeness never gets a reason, and the interviewer asks what forced the partitions.
  • Skipping the robots check as a detail. It is a requirement, and adding it later sounds like an apology.
  • Storing pages in a table because the first design has one to hand, with 300 TB a month coming. The interviewer does that multiplication at once.
  • Not saying the limit: twenty million against three billion. If the interviewer gets there first, the ratio goes in their notes and not in your answer.
What Interviewers Listen For
  • One URL walked from the queue to a stored page, every check named.
  • Why one loop is polite by construction, said before anything breaks it.
  • The shortfall given unasked, as a ratio: 150 times too slow.

Knowledge Check

"Three billion pages a month. How many fetches a second is that, and how many machines do they take?"

  • "About 12,000 a second once you allow for peaks, so about 1,500 workers on their own queues."
  • "About 1,200 a second, which one loop on one core reaches once it runs 150 threads, not one."
  • "About 1,200 a second, so cut the seed list into 150 slices, one for each worker to crawl."
  • "About 1,200 a second, and at 8 pages a second a worker, 150 workers sharing one URL queue."

"A hundred and fifty workers share that queue, and forty of them land on one small site in the same second. What changes?"

  • "Have every worker sleep for a second between fetches so that no site ever sees a burst."
  • "Split the queue by host, so that one owner holds each site and spaces its fetches out."
  • "Take a lock per host in a shared cache, which every worker must get before each fetch."
  • "Read each site's robots rules and rely on them to say what rate that site can take."

"Each page brings about fifty links. Have you seen this one before? Answer that at three billion URLs."

  • "Hold every URL in a hash set in memory, since 300 GB fits on three mid-size cache nodes."
  • "Look every URL up in the page store, because that already knows what has been fetched."
  • "Put a unique index on the URL column and let the duplicate inserts fail when they clash."
  • "Keep a compact filter that can say no for certain, and accept a few wrong answers of yes."

"A worker dies holding ten thousand URLs. Where do those URLs go now?"

  • "They stay in the frontier until marked done, and the next lease holder reads them again."
  • "Back to the frontier, because a shutdown handler on the worker returns them as it exits."
  • "Nowhere, which is fine: the next cycle of the crawl finds them through the links to them."
  • "To its neighbour, as soon as a supervisor sees that the worker has stopped heartbeating."

You got correct