Lesson 3 · Minutes 20-30

Web Crawler: Scaling It

Problem

From minute twenty to minute thirty, one loop becomes 150, and everything the loop got for free has to be bought. The interviewer gets there in four moves:

  • The ceiling you volunteered is still on the board, so they ask what actually runs at three billion.
  • Many workers on one queue pile onto a small site, and its owner complains.
  • Every page brings links, and each one has to be told apart from billions already met.
  • A worker goes quiet with a batch of URLs, and those URLs cannot be left to chance.

150 Workers

"One loop does twenty million a month. You need three billion. What runs?" A hundred and fifty loops. The rate is 1,200 a second and a worker does 8, so 150 workers, each on a small machine of its own, so that a dead machine costs one worker. They take URLs from one shared frontier service, the first design's queue with a network in front of it, and they write pages straight to the object store at 1,200 writes a second, well inside the 3,500 a prefix allows.

One large machine with enough threads looks cheaper, and it does not get you there. Threads help with waiting on sockets, which was cheap already. Parsing needs cores, and a machine with 150 of them is still one failure for the whole crawl.

What the 150 cost is the thing the single loop got free. No worker knows what the others are fetching, so the host's clock is gone. Nothing on the board now keeps a second between two fetches of one site, and the interviewer has already spotted that.

One Connection per Host

"A small site just got forty requests in one second from you, and its owner is on the phone. What do you change?" You give every host one owner. Politeness is a fact about a host, so the host is the key, for the reason in Chapter 3's Picking the Partition Key: the access that matters most is the one that names it.

Split the frontier into 600 partitions by a hash of the host, the By Hash section of Range, Hash and Directory. Each worker leases about four of them. There are more partitions than workers for the reason the fixed slots in Chapter 3's Consistent Hashing outnumber its nodes: when a worker joins or dies, a share moves whole, and no URL is hashed again. The cost is the one that lesson names: the slot count caps the cluster, here at 600 workers.

One owner per host means one connection and one clock. Robots rules, the host's resolved address and the next allowed time now live with the partition, so only the worker holding that partition ever reads or moves them. The first design's politeness is back, without a lock, because inside a partition nothing else is fetching. When the interviewer asks why you did not simply spread URLs evenly, the answer is short: "Politeness needs one owner per host, so the partition key is the host. That costs me uneven backlogs and gives me a hard cap on every key's rate, which is why nothing here runs hot."

That line has two halves, and both need their numbers. Partitions come out uneven in backlog, though no one host can overload them. A partition holding a huge site has millions of URLs waiting, and one holding only small sites can run dry, which leaves its worker with less to do until a lease moves. Load cannot pile up, because a host is capped at one fetch a second, an eighth of one worker. So no key can run hot in the sense of Chapter 3's Hot Partitions.

The second cost is a site you cannot finish. At a second a page, one host gets at most 2.6 million pages a month. A 10-million-page site takes about four months. You say so, because the interviewer can do that division as fast as you can.

Seen URLs

"Sixty thousand times a second, a worker has a link in its hand. How does it know it's new?" It asks a Bloom filter, the structure Chapter 2's When the Cache Hurts put in front of a store: a compact array of bits that answers "definitely absent" or "probably present". How it works is Computer Science Deep Dive's lesson on probabilistic structures. Here you need its sums and what it gets wrong.

Assume 10 bits a URL, which gives about one wrong yes in a hundred when the filter is full. Thirty-six billion URLs at 10 bits is 45 GB, against 3.6 TB of strings. Split over 600 partitions, that is 75 MB each. The worker holding a partition keeps its filter in memory, so a check takes no network call. That only works because a host has one owner, which puts all of a host's URLs in one place. A popular host draws more than its share of links, and each of those is still a look at local memory.

A link found on one worker usually belongs to another, though. So links travel through a log partitioned by host, the shape from Chapter 6's Queues, Logs and Pub-Sub, and each partition's owner reads its own. Sixty thousand links a second at about 100 bytes is 6 MB a second, a sliver of the 600 MB a second one vendor reports for three brokers, so the log's size decides nothing.

A worker writes a page's links to the log before it marks that URL done, so a crash loses no links. The same order means a link can arrive twice, and that is harmless, the kind of duplicate Chapter 6's Delivery Semantics at Scale calls idempotent by nature: the second check finds the URL already in the filter and drops it.

The price is the wrong yes. A new URL the filter wrongly calls seen is never queued, so once the filter is full, one new URL in a hundred is skipped, and a skipped URL stays skipped for as long as the filter lives. Against three billion new URLs a month, that is about 30 million pages a month at the end of the filter's year, and far fewer before. Name the price and park it: "Up to thirty million skipped pages a month is what the filter costs me. Whether that's worth paying, I'll settle in the deep dive."

The Worker That Dies

"One of your workers stopped answering, with ten thousand URLs in hand. Now what?" Start with what is at stake. At 8 a second, ten thousand URLs is about twenty minutes of work. The seen set says those URLs were seen, so if they are lost, nothing ever queues them again. Every later link to them gets "probably present" and is dropped. Lost here means lost for good, and you say that before the interviewer has to.

So a worker never holds the only copy. It holds leases on its partitions, and the URLs it has read stay in the frontier until they are marked done. The frontier keeps them on replicated storage apart from any worker, which also ends the first design's shared fate. A URL is marked done only after its page is stored and its links are in the log. Marked any earlier, a crash in between loses the page.

A lease lasts 10 seconds and has to keep being renewed. When one lapses, another worker takes it, with a fencing token, a number that grows with every grant. It loads the partition's last saved filter from the frontier's storage, replays the link log from that point, and reads the URLs not yet marked done. Admission keeps the same rule in its order: a URL goes into the frontier before its bits are set, so a crash between the two queues it twice, never zero times.

This is the story Chapter 5's Leases, Locks and Fencing opened with. A worker pauses and its lease lapses. The new holder takes the partition with token 34. The paused worker wakes with token 33 and writes, and the frontier, which has seen 34, refuses it. The check sits in the frontier, the resource, because the worker that just woke has no way of knowing it was gone.

A token cannot protect a site, though, because no site checks tokens. So a worker stops fetching a margin before its own deadline, and in ordinary running two workers are never on one host. A pause that falls between that last check and the fetch can still send one stray request, and you state that as the size of the gap.

This costs you in three places. Renewing 600 leases every few seconds is a few hundred small writes a second on a coordination service, which sits beside the data path, as in Chapter 5's Leader Election and Consensus. Each crash means up to 10 seconds of waiting on that worker's partitions. And done marks go in batches, so the new holder repeats whatever was done since the last one. Assume up to 1,000 fetches, each a harmless overwrite in the store and one extra request for a site. Replaying the log can re-queue a few more at the same price. A lease takes seconds to lapse, longer than a host's usual one-second gap, so those requests still respect it.

600 Partitions, 150 Workers

Seeds go into the frontier: 600 partitions by host, each with its URLs, its host clocks and a Bloom filter. A hundred and fifty workers hold leases on those partitions and fetch from the web. Pages go to the object store. Links go through a log by host back to their partition, where the filter lets the new ones in. A coordination service beside the workers grants the leases and hands out the tokens.

Everything the first design relied on is still here in another form. The queue on disk became the frontier, replicated, with its first-in, first-out order still waiting for the deep dive. The hash set became a filter per partition. The clock the loop kept for nothing became a clock per partition owner. Two of the three limits you named are gone, and which page matters first is the one left.

The full design, with what changed marked new
600 partitions by host Seedsstarting URLs The webnot yours FrontierURLs, host clocksNEW Workers150, 4 leases eachNEW Bloom filters75 MB a partitionNEW Object storepages, 100 KB each Link logpartitioned by hostNEW Coordinationleases, tokensNEW next fetch links by host new page lease readwriteasyncNEWsince the first design
Interview Pitfalls
  • Partitioning by URL because it spreads best, then adding a lock to get politeness back. That is a network call per fetch, and workers queue behind one site's lock.
  • Marking a URL done when the fetch starts. A worker dies between the two, the seen set says the URL was met, and the page is gone.
  • Quoting Bloom filters without what a wrong yes does. The interviewer's next word is "so", and the answer is up to thirty million pages a month.
  • Treating a lease as if it settled the paused worker. A lease only expires, and it is the token that refuses.
What Interviewers Listen For
  • Why a lost URL never returns, said without a hint.
  • The filter sized aloud: ten bits, thirty-six billion, forty-five gigabytes.
  • The check for an old holder placed in the frontier, the resource, not in the worker.

Knowledge Check

What does partitioning the frontier by host cost you?

  • Hot partitions, since one busy host can outrun the worker that owns it
  • Uneven backlogs, and big sites that cannot finish inside one month
  • A network call on each fetch, to learn which worker owns that host
  • A new hash of every URL whenever a worker joins or leaves the fleet

The filter gives a wrong yes for a new URL. What happens to that page?

  • It is queued twice, and the second fetch overwrites the first copy
  • It waits in the log until a later check of the filter comes back no
  • It is fetched in the end, once a worker confirms the yes in a store
  • It is never queued, and stays skipped for as long as the filter lives

The log delivers one link twice. What does that cost you?

  • Almost nothing, as the second check finds it in the filter and drops it
  • A second fetch of that page, with one more request made of the site
  • A lost URL, because two copies collide when they reach the owner
  • A stall on that partition until the log has removed the second copy

What does the fencing token stop that the lease alone does not?

  • A crashed worker keeping its partitions so that nobody can take over
  • Two workers ever sending a request to one host in the same moment
  • A paused worker's late write to the frontier after another took its lease
  • A repeat of all the fetches made since the last URLs were marked done

You got correct