BOOKS DEEP-READ · SYSTEM DESIGN INTERVIEW VOL 1 · CH 9

Design a Web Crawler

System Design Interview Vol 1 · Ch 9 · Alex Xu · 2020

中文 →

What is this chapter about?

You type two words into a search box and ten thousand results appear in half a second. Those pages obviously were not read while you were typing — a program had already copied them down, one by one, and filed them away. That program is a web crawler, and chapter 9 of System Design Interview asks you to build one from scratch.

An analogy

Think of the crawler as a copyist holding nothing but a list of addresses. He goes to the first address, copies down everything he finds there, and notices that the text mentions twenty other addresses — so he appends those twenty to the end of his list. Then on to the next address, and around again.

The strange part is that the list keeps getting longer. The more he copies, the longer it grows. That is how the entire web gets pulled in, one loop at a time.

The hard part is not copying — it is queueing

Downloading a page is the easy step. The hard question is where to go next, and there are tens of billions of addresses waiting. Two things go wrong.

First, addresses come in clumps. Most links on a page point back at the same site — navigation bars, sidebars, related articles. So if you simply work through the list in order, you get hundreds of consecutive addresses belonging to one host, and your copyist sprints over and hammers on their door hundreds of times. They get annoyed and lock you out, and now you can copy nothing at all.

Second, not all addresses deserve equal treatment. A news home page changes every hour; a static page nobody has touched in ten years needs copying once. A list that ignores importance will spend its best hours in the least useful corners of the web.

The core idea: queue twice

The soul of this chapter is one move: split "who goes first" and "whom must we not pester" into two separate rows of queues.

The front row sorts by importance — pages that matter, and pages that change often, get called sooner. The back row sorts by street address: every site gets its own private queue, only one messenger is sent per site, and he has to wait a while between knocks. Two requirements that pull against each other, solved by two rows of queues instead of one.

The honest cost: politeness is slowness. The better behaved you are, the longer a full sweep of the web takes — and the only way to speed up is to work on more sites at once, never to knock harder on any one of them.

Two potholes along the way

One is copying the same thing twice. The same article often hangs off several addresses. So the copyist needs two checkpoints: before setting out, "have I been to this address?"; after copying, "do I already have this text?"

The other is walking into a maze. Some sites can generate directory levels forever, and a copyist who wanders in never comes out. The cheapest defence is to cap the address length and refuse anything longer; the rest has to be a hand-maintained blacklist of known mazes.

Remember this

A crawler is hard not because downloading is hard but because queueing is. Two tiers of queues separate "fetch the important things first" from "do not knock any one site over"; then two dedup checkpoints — one before fetching, one before storing — throw away addresses you have visited and text you already hold.

Want the mechanisms, the notation and the diagrams? → Switch to the deep read