Design a distributed web crawler - fetch 10 billion pages in five days, keep the text of each page once, and never send a site more than one request a second, however many workers are running. Almost all of a crawler's time is spent waiting for other people's servers. The staff-level signal is counting fetches in flight instead of CPUs, keeping every shared fact in one shared place, and deleting a queue message only when the page is completely done.
Written by a Staff+ Engineer and an Engineering Manager
12+ years of experience in distributed systems and real-time messaging. Tech lead for large-scale chat applications serving billions of users. Over 12 years of industry experience with 6 years in engineering leadership, building and scaling tech infrastructure teams that deliver end-to-end large-scale distributed systems.
Finished this lesson?
Sign in to save your progress across devices.
Tip: use ← and → to move between lessons.
Was this lesson helpful?
Rehearse this exact question with the author
45-minute mock with a Staff+ engineer who interviews at your target company. Premium members save $20.
What to Design?
Could you answer these today? Tick what you already know.
The loop is quick to draw: take a URL, fetch the page, store the text, queue the links. The interview is decided by four questions about what happens when that loop runs on 40 machines at once. Each one is a deep dive later on this page, with three designs side by side: the one that sounds right, a better one, and the one that holds.
Swipe to see all columns
| The four questions, and the answer that sounds right | |||
|---|---|---|---|
| # | The interviewer asks | The answer that sounds right | What it misses |
| DD1 | 23,000 pages a second. Where does the speed come from? | Faster machines | The crawler waits 200 ms for a page and computes for 5. Speed is the number of fetches in flight: 4,600 |
| DD2 | 40 workers, one site, one request a second. Who keeps count? | Each worker limits itself | 40 workers that each keep the rule send 40 requests a second |
The same three habits answer all four:
Do not spend your questions on HTML parsing. Spend them on what the crawl promises.
Swipe to see all columns
| Scope Decisions | ||
|---|---|---|
| Question | Agreed answer | Why it matters |
| What do we crawl? | The public web, from a list of seed URLs, following links freely | The list of known URLs grows to billions. Every link must be checked against all of them (DD3) |
| How much, how fast? | 10 billion pages in 5 days | 23,000 pages a second. Far more than one machine can do (DD1) |
| What do we keep? | The extracted text. Not the HTML | 50 TB of text, not 300 TB of markup |
Out of scope (say this out loud): the search index or training pipeline that reads the text, pages that need JavaScript to show their content, images, video and audio, pages behind a login, and the legal review of what may be crawled.
FR1 — Crawl from seeds. Start from the seed URLs and follow links, roughly breadth-first. Every URL carries its depth: the number of links between it and a seed.
FR2 — Keep the text. Strip the markup from every page and store the text.
FR3 — Feed the links back. Every link on a page goes into the frontier. The frontier is the list of URLs that are known but not fetched yet.
FR4 — No duplicates. A URL is fetched once. A text is stored once, also when several URLs return it.
FR5 — Be polite. Obey each site's robots.txt, a file in which a site says what crawlers may fetch. Send a site at most one request a second.
The key principle behind all five
Swipe to see all columns
| Five facts and the conclusions they do not supportRead each row as: the fact on the left does not, by itself, imply the claim in the middle. | ||
|---|---|---|
| Fact | Does not imply | Why the gap exists |
| A worker took the URL | The page is done | The worker may die a second later. The work counts when its result is stored |
NFR1 — Speed. 10 billion pages in 5 days: about 23,000 pages a second, around the clock.
NFR2 — Politeness. At most one request a second per site for the whole fleet, and never a path that robots.txt forbids.
NFR3 — No lost work. When a worker dies, every URL it held is fetched by another worker. Nobody has to step in.
NFR4 — No waste. A URL is fetched once and a text is stored once, as far as that can be known in advance.
NFR5 — Easy to grow. Adding a worker means starting a machine. Nothing is copied or rebalanced.
| Capacity Thought Experiment | |
|---|---|
| Parameter | Value |
| Pages | 10 billion in 5 days, which is 432,000 seconds |
| Speed needed | About 23,000 pages a second |
| One fetch | About 200 ms, nearly all of it waiting for the site. About 5 ms of CPU |
| Fetches in flight | 23,000 a second times 0.2 s: 4,600 at every moment |
| One worker | 150 threads: 750 pages a second |
The number that identifies the real system is the gap between waiting and working:
one fetch ~200 ms about 5 ms of it is CPU, the rest is waiting
needed 23,000 /s = 4,600 fetches in flight, at every moment
one site allows 1 /s so at least 23,000 different sites are in flight
A crawler is a machine for waiting on thousands of servers at once. Every number above is an illustrative assumption.
Interviewer signal: candidates who ask for bigger machines have missed the number. Candidates who say "one page costs 200 ms of waiting and 5 ms of work, so I need 4,600 fetches in flight, spread over thousands of sites" have found the system.
Three ideas shape everything:
One thread fetches 5 pages a second and its CPU is idle over 95% of the time. So the first lever is not a faster CPU. It is more fetches at the same time: 150 threads on a machine, then 40 machines.
With 40 workers, anything that only one worker knows is wrong for the other 39. So every fact that matters lives outside the workers: the URLs to fetch in a queue, the URLs already seen in a table, the stored texts in a table, and whose turn it is at a site in one lock. Any worker can take any URL, and any worker can die.
A queue that never loses a message will sometimes deliver one twice. So every step is built to give the same result the second time: insert a row only if it is missing, name a text by its hash, and never move a finished URL back.
The core recognition
This is a loop of stateless workers around one shared queue. Stateless means: a worker keeps nothing that must survive it. Three small shared stores keep the workers honest: one knows what has been seen, one knows whose turn it is at each site, one holds the text.
| Design Decisions | |
|---|---|
| Design choice | Reason |
| 150 threads per worker, 40 workers | The time goes to waiting, and waits can overlap |
| One frontier queue with a visibility timeout | A message that is not acked comes back by itself, so a dead worker loses nothing |
Two tables carry the design:
urlsandpages. The fields to explain arestatus, which only moves forward, andfound_by, which lets a retry repair a half-finished page.
One row per URL the crawler has ever seen, fetched or not.
Swipe to see all columns
| urls | ||
|---|---|---|
| Field | Type | Comment |
url_hash | STRING | Key. SHA-256 of the normalised URL. Shown here by its first four characters, for example 7a1c |
url | STRING | The URL itself, for example https://blog.example/posts/42 |
One row per distinct text, however many URLs return it.
Swipe to see all columns
| pages | ||
|---|---|---|
| Field | Type | Comment |
content_hash | STRING | Key. SHA-256 of the extracted text, for example c3d9 |
first_url | STRING | The url_hash that brought this text first |
Swipe to see all columns
| Supporting records | ||
|---|---|---|
| Record | What it holds | Why it exists |
| Queue message | url, url_hash, depth | One URL waiting in the frontier. A worker takes it, and deletes it when the page is done |
| Text object | The extracted text, stored as text/c3d9.txt | The name is the hash of the text. The same text always gets the same name |
Normalising a URL means writing it in one standard form before hashing: lower-case scheme and host, no #fragment, no default port, query parameters in sorted order. https://Blog.example/posts/42#comments and https://blog.example/posts/42 then give the same hash.
Swipe to see all columns
| Storage | ||
|---|---|---|
| Store | Data | Consistency |
| SQS | The frontier: one message per URL to fetch | At least once. A message that is not acked within 300 s is handed out again |
| DynamoDB | urls, pages | The source of truth. A write to one row is atomic and can carry a condition |
| S3 | Text objects, named by their hash | Written once, never changed |
A crawler has no public API. Its interfaces are the calls between its own parts.
Swipe to see all columns
| The calls between components | ||
|---|---|---|
| Call | Sent to | What it does |
ReceiveMessage(max 10, timeout 300 s) | Frontier queue | Hands out up to 10 URLs and hides them from other workers for 300 s |
SendMessage(url, url_hash, depth) | Frontier queue | Adds a URL, at once or after a delay |
DeleteMessage | Frontier queue | |
The visibility timeout is the rule that makes the queue safe: a message that a worker has taken is hidden, not deleted. If the worker does not delete it within 300 seconds, the queue shows it again to the next worker that asks.
Present the design as four short stories, each in its own colour on one whiteboard: Phase 1 fetch (black), Phase 2 store (blue), Phase 3 links (green), Phase 4 a worker dies (purple). Phases 1 to 3 follow one page through one worker. The number on an arrow is the same number in the animation and in the step table.
Swipe to see all columns
| The four phases | |||
|---|---|---|---|
| Phase | Colour | Story | What it leaves behind |
| Phase 1: Fetch (FR1, FR5) | Black | Worker W1 takes a URL, checks the rules of the site and fetches the page | A hidden queue message and a one-second lock |
| Phase 2: Store (FR2, FR4) | Blue | W1 extracts the text and stores it, unless that text is already stored | One text object and one pages |
How to read the picture
https://blog.example/posts/42, with URL hash 7a1c. Its text is 5 KB and hashes to c3d9.Each phase below is an interactive walkthrough of the same picture. It plays on its own. You can pause it, step with the arrow keys, and click any component to read its role.
Goal: take a URL without losing it, and fetch the page without breaking the site's rules.
ReceiveMessageCrawler worker W1 → Frontier queue10 messages. One of them:7a1c and 9 othersupdateSwipe to see all columns
| Phase 1 step by step (black arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | A thread of W1 asks the frontier queue for up to 10 URLs. One of them is https://blog.example/posts/42, hash 7a1c, depth 2. | The 10 messages are hidden for 300 s. They are not deleted |
| 2 | W1 reads the robots rules of blog.example from Redis. The path /posts/ is allowed. | Nothing |
Why it is built this way
robots.txt (FR5). On a miss in Redis, the worker first fetches https://blog.example/robots.txt and keeps the rules for 24 hours. A URL that the rules forbid is marked DONE without a text and its message is deleted. If the file asks for Crawl-delay: 5, the site's lock lasts 5,000 ms.
The timeout must be longer than a batch normally takes. Ten pages at 200 ms are 2 seconds, and even with a few slow sites a batch is done within a minute or two. If the timeout is too short, a second worker starts a page that the first one is still working on: wasted, but harmless. If it is too long, the URLs of a dead worker wait longer than needed. 300 seconds is a fair middle.
Goal: keep the text of the page, and keep every distinct text exactly once.
strip the markup, hash the textCrawler worker W1GetItem pages[c3d9]W1 → Metadata storenot found: a new textNothing is written in this step.
Swipe to see all columns
| Phase 2 step by step (blue arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | W1 strips the markup: 30 KB of HTML become 5 KB of text. The hash of the text is c3d9. W1 looks for pages[c3d9]. There is no such row: the text is new. | Nothing |
| 2 | W1 stores the text as text/c3d9.txt. | Text object text/c3d9.txt |
Why it is built this way
pages row promises that the text is stored. If a worker dies between the two, there is an object without a row, and the retry simply writes both again.?ref=home returns the same text without reading it. The fetch is spent. The storage and the 50 link checks are saved.The HTML of one article differs from load to load: a timestamp, an ad, a session token. The text is the part we keep, so the text is the part we compare. Even so, an exact hash only finds exact copies. One changed word gives a new hash (see the follow-ups below).
Goal: put every new link into the frontier exactly once, and only then call the page done.
normalise and hash each linkCrawler worker W1PutItem urls[2b8e], urls[9f4d]W1 → Metadata storeboth inserted: new URLs2b8e, 9f4dnew rowSwipe to see all columns
| Phase 3 step by step (green arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | The page links to https://blog.example/posts/43. W1 normalises and hashes it: 2b8e. It inserts the row, on the condition that it is missing. The write succeeds: the link is new. The same happens for https://shop.example/item/9, hash 9f4d. | urls rows 2b8e and 9f4d: QUEUED, depth 3, found by 7a1c |
| 2 | ||
Why it is built this way
Seeds enter the same way. A small script runs steps 1 and 3 for every seed URL, with depth 0.
Because the queue already knows who is working on a URL: the message is hidden. A status in the table would be a second copy of that fact, and it goes stale the moment a worker dies. Then every retry has to guess whether IN_PROGRESS means "someone is on it" or "someone was on it". It also costs one more write for each of 10 billion pages. The table only records what is finished.
Goal: lose no URL when a worker dies, without anyone watching the workers.
text/8be2.txtW1 → Text store200 OK. Then the machine stopstext/8be2.txtnew rowe31aread onlySwipe to see all columns
| Phase 4 step by step (purple arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | W1 works on https://news.example/today, hash e31a. It stores the text as text/8be2.txt. Then the machine stops. No ack was sent. | Text object text/8be2.txt. The message is still hidden |
| 2 | 300 seconds after the hand-out there is still no ack. The queue shows the message again, and W2 gets it. | The message is hidden again, now for W2 |
Why it is built this way
DONE and only delete the message.Yes, and it is the price of losing nothing. A worker holds at most a few hundred URLs when it dies, out of 10 billion. The other choice is a queue that deletes a message when it is handed out: no page is ever fetched twice, and every crash silently loses pages (DD4).
The deep dives are the four questions from the top of the page. Each one compares three designs for the same scenario: a Bad one, which is usually the answer that sounds right; a Good one, which still leaves a gap; and a Great one, which holds. The answers start covered. Read the scenario, decide what you would build, then click each card.
Swipe to see all columns
| Deep Dive Map | |||
|---|---|---|---|
| Deep Dive | Topic | Scenario | NFR |
Scenario: 10 billion pages in 5 days is 23,000 pages a second. One fetch takes about 200 ms, and for nearly all of that time the crawler does nothing but wait for the site to answer. Where does the speed come from?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. One loop: take a URL, fetch the page, store the text, queue the links, repeat.
Where it breaks. The loop spends 200 ms on every page, so it does 5 pages a second. 10 billion pages take 63 years. And the machine is not even busy: the CPU works for about 5 ms per page and is idle over 95% of the time. A faster CPU changes nothing.
The idea. Run the same loop in 150 threads. While one thread waits for its site, the others send their requests.
What it fixes. The waits overlap. 150 fetches are in flight at once, and the machine does 750 pages a second: 150 times more on the same hardware.
What it still misses. 750 a second is still 31 times too slow: 10 billion pages take 154 days. One machine cannot run 4,600 threads well, and its network card would have to carry 690 MB every second. And it is one machine: when it stops, the crawl stops.
The key idea. Speed is the number of fetches in flight. Threads multiply it on one machine, machines multiply it again, and a shared queue is all that connects them.
How it works
The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
ReceiveMessageW1 → Frontier queue1 message: https://blog.example/posts/42Nothing is written in this step.
Swipe to see all columns
| DD1 side by side | |||
|---|---|---|---|
| Bad: one thread | Good: 150 threads | Great: 40 workers | |
| Fetches in flight | 1 | 150 | Up to 6,000 |
| Pages a second | 5 | 750 | Up to 30,000 |
| 10 billion pages take | |||
Check it against the scenario
| 23,000 pages a second | |
|---|---|
| Situation | Outcome with the Great option |
| The target needs 4,600 fetches in flight | 40 workers hold up to 6,000. About three in four threads are busy |
| A site takes 20 seconds to answer | One thread waits 20 seconds. The other 149 threads of that worker do not notice |
| A site never answers | The thread gives up after 30 s without a connection, or 60 s without the full page. The URL is tried again later |
| W2 dies | Its hidden messages come back after 300 s. The crawl is 2.5% slower until W2 is replaced |
| The crawl is behind on day 3 | |
The trade-off to name. Threads are not free: each one holds memory, and past a few hundred a machine spends its time switching between them. That is why the next step is more machines. And a fleet creates new problems: 40 workers must now agree on which URLs are known and whose turn it is at a site. Those are DD2 and DD3.
Yes. Python's lock, the GIL, stops two threads from computing at the same moment. A thread that waits for the network gives the lock back. So 150 threads can all wait at once, and waiting is over 95% of the job. The 5 ms of parsing per page take turns.
"The crawler is bound by I/O, not by CPU. One fetch is about 200 milliseconds of waiting, so a single thread does 5 pages a second. For 23,000 pages a second I need 4,600 fetches in flight at every moment. I get there in two steps: a pool of about 150 threads per machine, which gives 750 pages a second, and then about 40 identical machines that all pull from one frontier queue. The workers are stateless: the frontier is in the queue, what has been seen is in a table, the text is in object storage. So scaling out is just starting machines, and a dead machine only means its messages reappear."
Follow-up: "Why not async I/O instead of threads?" — It is the same idea with less overhead: one thread watches thousands of connections, so fewer machines are needed. I start with threads because they are simple to explain. The design around the workers does not change.
Scenario: 100,000 URLs of wiki.example wait in the frontier. The site allows our crawler one request a second, all 40 workers together. 6,000 threads take URLs from the same queue. Who keeps count?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Each worker remembers when it last asked each site, and waits a second before it asks that site again.
Where it breaks. Every worker keeps the rule, and the fleet breaks it. In one second, 40 workers can each send one request to wiki.example: 40 requests a second. The site answers with 429, too many requests, and then blocks our addresses. The site is lost for the crawl, and the promise is broken.
The idea. One lock per site in Redis, set with a lifetime of 1,000 ms. A worker may send a request only if it set the lock. A thread that does not get the lock waits and tries again.
What it fixes. The count. Only one SET per second can succeed, whichever worker sends it. wiki.example sees one request a second.
What it still misses. The waiting threads. The frontier hands out wiki.example URLs faster than one a second, and every thread that takes one waits. They are served one per second, so the group of waiting threads only grows. Soon most of the 6,000 threads wait on one site, and the crawl slows from 23,000 pages a second towards 1.
The key idea. A site may make a URL wait. It may never make a thread wait. A thread without the lock gives the URL back and takes another one.
How it works
SET lock:wiki.example NX PX 1000. NX means: only if it is not set. PX 1000 means: gone after 1,000 ms.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
W1's own timer for wiki.exampleCrawler worker W1free: W1 sent nothing in the last seconda page of wiki.exampleW1 → wiki.exampleNothing is written in this step.
Swipe to see all columns
| DD2 side by side | |||
|---|---|---|---|
| Bad: a timer per worker | Good: shared lock, wait | Great: shared lock, move on | |
| Where the count lives | In every worker | In one Redis key | In one Redis key |
| Requests to wiki.example | Up to 40 a second | 1 a second | 1 a second |
Check it against the scenario
| 100,000 URLs of one site | |
|---|---|
| Situation | Outcome with the Great option |
| W1 and W2 want wiki.example in the same second | One SET succeeds. The other worker gives its URL back and moves on |
| robots.txt asks for Crawl-delay: 5 | The lock lasts 5,000 ms: one request every 5 seconds |
| W1 dies right after it set the lock | The lock is gone after 1,000 ms. Nobody has to clean up |
| A fetch takes 3 seconds | The lock was gone after 1. The next request may start: the rule counts requests started per second |
| All 100,000 URLs | Done after about 28 hours, at one a second. The site sets that pace, not the design |
The trade-off to name. Bounces. A wiki.example URL can be taken and given back many times before its turn comes. Each bounce is two cheap queue calls, and the 10-second delay keeps them rare. If a few huge sites fill the frontier, give each site its own queue and let a scheduler release one URL a second: no bounces, more parts.
That keeps the rule with no shared state at all. But the URLs of a small site may reach only three workers, and then the site gets 3 requests in 40 seconds where 40 were allowed. Most of the crawl runs many times slower than it could. And the number 40 changes whenever a worker dies or one is added. It is the right fallback when Redis is down. It is not the main design.
"The limit is per site for the whole fleet, so the count cannot live in a worker. I keep one key per site in Redis and set it with NX and a lifetime equal to the crawl delay, one second by default. If the SET succeeds, the worker fetches. If not, it does not wait: it puts the URL back on the queue with a short delay and takes the next one, so no thread is ever blocked by one site. The lock expires by itself, so a dead worker cannot hold a site. robots.txt is cached per site, and its Crawl-delay sets the lifetime of the lock."
Follow-up: "What if Redis is down?" — Redis is not the source of truth for anything. Each worker falls back to a local timer of crawl delay times the number of workers, so the fleet stays inside the limit, only slower.
Scenario: Within a minute the crawler meets the same blog post in three ways. W1 and W2 both find the link https://blog.example/posts/42 at the same moment, on two different pages. A third page links to https://Blog.example/posts/42#comments. A fourth links to https://blog.example/posts/42?ref=home. How many fetches should that cost, and how many stored copies?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Each worker keeps the URLs it has seen in memory. A link that is not in the set is new: add it and enqueue it.
Where it breaks. W1's set knows nothing about W2's. Both enqueue the post, and it is fetched twice. With 40 workers it can be 40 times. When a worker restarts, its set is empty and it enqueues everything again. And the set does not fit: 10 billion hashes of 32 bytes are 320 GB.
The idea. One urls table for the whole fleet. For every link, the worker normalises the URL, hashes it and inserts a row on the condition that it is missing. Only a successful insert is followed by a queue message.
What it fixes. The same URL. W1's insert succeeds and W2's is refused, so there is one message. The #comments form normalises to the same URL and is refused too. Reading first and writing second would not do: both workers would read "missing".
What it still misses. The same text under another URL. ?ref=home is a different URL, so it is fetched, and its 5 KB of text are stored a second time. If one page in four is such a copy, that is 2.5 billion texts nobody needs, and all their links are checked again.
The key idea. A URL is a name and a text is the thing. Check the name before the fetch, because that saves the fetch. Check the thing after it, because that is the only moment it can be known.
How it works
pages.DONE and points at the text that is already there.pages row.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
is 7a1c in my set?W1 → its own memoryno: add itSendMessageW1 → Frontier queue7a1cnew row7a1c (first)new rowSwipe to see all columns
| DD3 side by side | |||
|---|---|---|---|
| Bad: a set per worker | Good: one shared table | Great: two layers | |
| Where "seen" lives | In each worker's memory | In the urls table | In urls and in pages |
| Two workers find the same new link | |||
Check it against the scenario
| One post, met in three ways | |
|---|---|
| Situation | Outcome with the Great option |
| W1 and W2 find posts/42 at the same moment | One conditional write succeeds. One row 7a1c, one message, one fetch |
| The link with Blog.example and #comments | It normalises to the same URL. The hash is 7a1c again: refused, skipped |
| The link with ?ref=home | A new URL, d04b. It is fetched. Its text is c3d9 again: nothing is stored, no link is followed |
| W1 and W2 store the same new text at the same moment | Both write text/c3d9.txt with the same bytes. One object |
The trade-off to name. Layer 2 comes late: a copy costs one download before anyone can know it is a copy. Layer 1 costs a write for every link, 1.2 million a second, most of them refused. And an exact hash finds only exact copies.
A Bloom filter is a compact array of bits that answers "maybe seen" or "surely new". For 10 billion URLs and 0.1% wrong answers it needs about 18 GB. It has two problems here. A wrong "seen" makes the crawler skip a new page: 0.1% of 10 billion is 10 million pages that are never crawled, and nothing reports it. And 40 workers need the same filter. Forty copies drift apart, and one central filter is a shared store again. It can sit in front of the table as a first check. It cannot replace the table.
"I deduplicate twice. Before a URL is enqueued, I normalise it, hash it and do a conditional insert into a URL table keyed by that hash. The condition makes check and insert one atomic step, so two workers that find the same link cannot both enqueue it. After a fetch, I hash the extracted text and look it up in a second table keyed by content hash. If it is there, I store nothing and do not follow the links. If not, I store the text under its hash and then record it. Because the object name is the content hash, two workers that store the same text write the same object."
Follow-up: "Why a table and not a Bloom filter?" — A false positive silently drops a page, and the filter would have to be shared by all workers anyway. The table gives an exact answer with one point write.
Scenario: W1 works on https://blog.example/posts/42, hash 7a1c. It has stored the text. It has inserted the row for the new link /posts/43, hash 2b8e. Before it can send the queue message for 2b8e, the machine dies. What is lost?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. A worker deletes a message as soon as it has taken it. No message is ever handed out twice, so no page is ever fetched twice.
Where it breaks. When W1 dies, nothing is left that says "this page is not finished". The row of 7a1c says QUEUED for ever. Its links are never queued, so /posts/43 and every page that only it leads to are never crawled. No error shows up anywhere. The crawl just ends smaller than it should.
The idea. A taken message is only hidden for 300 s. The worker deletes it after the text is stored, the links are queued and the row is DONE.
What it fixes. The page. W1 never acked, so after 300 s the queue hands 7a1c to W2, and W2 does the page again.
What it still misses. The link that was half done. W2 tries to insert the row for 2b8e and is refused, because W1 already inserted it. W2 reads that as "this link is already queued" and skips it. But W1 died before it sent the message. Row 2b8e says QUEUED, no message exists, and /posts/43 is never crawled.
The key idea. A row and a message cannot be written in one step. So the retry must be able to tell which rows its own page created, and send their messages again.
How it works
found_by: the hash of the page whose link created it.found_by this page and QUEUED, the worker sends the message again. The first attempt may have died before it did.DONE means: delete the message and stop. So a message that exists twice does no harm.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
ReceiveMessageW1 → Frontier queue7a1c: https://blog.example/posts/42DeleteMessageW1 → Frontier queue, right away7a1cdeleteSwipe to see all columns
| DD4 side by side | |||
|---|---|---|---|
| Bad: delete on take | Good: delete last | Great: delete last, repair | |
| The message is deleted | When it is taken | When the page is done | When the page is done |
| W1 dies in the middle of a page | The page is never finished | W2 does the page again | W2 does the page again |
Check it against the scenario
| W1 dies on posts/42 | |
|---|---|
| Situation | Outcome with the Great option |
| W1 dies during the fetch | No ack. After 300 s W2 gets the message and fetches the page |
W1 dies after storing the text, before the pages row | W2 finds no row and writes text/c3d9.txt again. One object |
| W1 dies between the row and the message of 2b8e | W2's insert is refused and returns: found by 7a1c, QUEUED. W2 sends the message |
The trade-off to name. This is at-least-once processing: no URL is lost, but some pages are fetched twice and some messages are sent twice. That is affordable only because every write in the design gives the same result the second time.
Because they live in two systems: the row in DynamoDB, the message in SQS. No transaction covers both, so one of them is written first and a crash can fall in between. Message first would send a message for every link, also for the 49 in 50 that are already known. Row first leaves a row without a message, and the retry repairs that. A third way is to let the database send the message: a stream of new rows that a small forwarder turns into queue messages. That is one write for the worker and one more part to run.
"A worker deletes the queue message last: after the text is stored, the new links are enqueued and the URL row is marked done. If it dies anywhere before that, the visibility timeout expires and another worker repeats the page. Repeating is safe because every step is idempotent: the text object is named by its content hash, link rows are conditional inserts, and done stays done. The one gap is between inserting a link's row and sending its message, since those are two systems. So each row records which page found it, a refused insert returns the existing row, and a retry re-sends the message for rows that its own page created and that are still queued. A duplicate message is harmless, because a worker checks the row before it fetches."
Follow-up: "What about a page that kills every worker that reads it?" — The queue counts how often a message was handed out. After three times it goes to a dead-letter queue and the row is marked failed, so one bad page cannot circle for ever.
| If the interviewer keeps going | |
|---|---|
| Question | Short answer |
| Pages change. When do you crawl one again? | The simple way: every 7 days. The better way adapts: if the text hash changed, halve the page's interval, down to 1 hour. If not, double it, up to 30 days. A background job puts the pages that are due back into the frontier |
| A calendar links to "next month" for ever | That is a crawler trap. Stop at depth 15, cap every site at 10,000 pages, and let the content hash catch pages that repeat. Spotting URL patterns that differ in one parameter is a later refinement |
| A page crashes every worker that reads it | The queue counts hand-outs. After 3 the message moves to a dead-letter queue and the row becomes FAILED. A size cap and the 30 s and 60 s limits stop most of these early |
| 6,000 threads look up host names | |
The work is waiting, not computing. Count fetches in flight: 23,000 pages a second times 200 ms is 4,600.
Forty polite workers are one rude crawler. The count lives in one lock that all of them share.
A waiting thread is a lost thread, and one busy site can collect them all. Give the URL back and take another.
It is not shared, it dies with the worker, and 10 billion hashes are 320 GB. Use one shared table.
Two workers both read "missing" and both enqueue the link. One conditional write must decide.
The same text hides behind many URLs. Hash the text after the fetch and store it once.
A crash then loses the page and every link on it, without any error. Delete the message last.
A calendar with a "next month" link never ends. Cap the depth and the number of pages per site.
The shape of a strong answer: start with one loop and show that it waits. Overlap the waits with threads, then with machines, around one shared queue. Keep what has been seen in a table and decide it with one conditional write. Name each text by its hash. Keep one lock per site and never wait for it. Delete the queue message last, and make every step safe to run twice.
Wait in parallel
One fetch is 200 ms of waiting and 5 ms of work. 4,600 fetches in flight give 23,000 pages a second: 150 threads per worker, 40 workers, one frontier.
Keep shared facts in one shared place
Which URLs are known is one conditional write to a table. Whose turn it is at a site is one lock that expires by itself. No worker decides alone.
Finish last, and make every step safe to repeat
The message is deleted when the page is completely done. Until then anything may run twice, and a retry repairs what the first attempt left half done.
| What the interviewer expects at each level | |
|---|---|
| Level | A strong answer shows |
| Junior | The loop: fetch, extract, follow links. A queue, workers and storage. robots.txt, a rate limit, and a set of visited URLs |
| Mid-level | Walks from one thread to a thread pool to a fleet, with the numbers. Explains the shared lock, both dedup layers, and why the message is deleted last |
| Senior | Starts simple and names the hard parts unasked: the fleet-wide rate limit, dedup over 10 billion rows, the gap between a row and its message, traps, DNS. Covers what to monitor, when to crawl again, and what it costs |
These aren't toy problems — they reflect real infrastructure challenges at companies that collect data at web scale. If you want to walk through this design in a mock interview or get feedback on your own approach, book a session with us. We're here to help you sharpen your thinking, build your confidence, and land your next offer.
Pick an answer for each question, and you see at once whether it is right. Show explanation opens the answer, why it is right, and why each of the other options fails.
Question 1 of 10
One fetch takes about 200 ms. How many fetches must be in flight at every moment to reach 23,000 pages a second?
Question 2 of 10
A crawler thread waits for a site over 95% of the time. Why do 150 threads on the same machine give about 150 times the speed?
Question 3 of 10
A site allows the whole crawler one request a second. Each of 40 workers keeps its own one-second timer per site. What does the site see?
Question 4 of 10
W2 asks Redis for the lock of wiki.example and does not get it. What does its thread do?
Question 5 of 10
W1 and W2 find the same new link at the same moment. What makes sure it is enqueued only once?
Question 6 of 10
A URL ending in ?ref=home is new, but it returns the same text as a page that is already stored. What happens?
Question 7 of 10
When does a worker delete the queue message of a page?
Question 8 of 10
A worker dies after taking a message and before deleting it. What happens to the URL?
Question 9 of 10
W2 repeats a page after W1 died. Its insert of a link row is refused, and the existing row says: found by this page, QUEUED. What does W2 do?
Question 10 of 10
A Bloom filter for 10 billion URLs gives a wrong "seen" answer 0.1% of the time. What does that cost?
| DD3 | The same post shows up under three URLs. How often is it fetched and stored? | Keep a set of visited URLs | A set inside a worker is not shared and dies with it. And a new URL can carry an old text |
| DD4 | A worker dies in the middle of a page. What is lost? | Nothing, the queue retries | Only if the message is deleted last, and the retry repairs what was half done |
| Two URLs return the same text? | Store it once | Two checks: the URL before the fetch, the text after it (DD3) |
| One request a second per site: for each worker, or in total? | In total, for the whole fleet | The count must live in one place that all workers share (DD2) |
| A worker dies in the middle of a page? | Another worker repeats it. No manual step | The queue message is deleted last (DD4) |
| My worker waited a second | The site got one request a second | 39 other workers waited the same second |
| The URL is new | The text is new | One page can have many URLs: a tracking parameter, a mirror, a print view |
| The row for a link exists | Its queue message exists | The row and the message live in two systems. A crash can fall between them |
| A page links to it | We may fetch it | robots.txt, the depth limit and the site's budget all have a say |
| 31 at the very least. Run 40: up to 30,000 pages a second |
| Download | 30 KB a page: 690 MB every second |
| Stored text | About 5 KB a page: 50 TB before duplicates. The HTML would be 300 TB |
| URL rows | About 200 bytes each: 2 TB |
| Link checks | About 50 links a page: 1.2 million a second. 49 of 50 are already known |
| One site | One request a second: at most 86,400 pages a day |
A urls table, written with a condition | One atomic write answers "have we seen this URL?" for the whole fleet |
| Text stored under the hash of the text | The same text gets the same name, so it is stored once and writing it twice is harmless |
| One lock per site in Redis, gone after 1,000 ms | One request a second, counted in one place |
| No lock: send the URL back and take another | No thread ever waits for a site |
| The queue message is deleted last | A crash repeats work. It never loses it |
found_by on every URL row | A retry can tell which links it may have left half done |
| robots.txt cached per site for 24 hours | One extra fetch per site per day, not one per page |
statusENUM |
QUEUED, then DONE or FAILED. It never moves back |
depth | INT | Links between this URL and a seed. A seed has depth 0 |
found_by | STRING | The url_hash of the page whose link created this row (DD4) |
content_hash | STRING | Set when the page is done: which text this URL returned |
fetched_at | TIMESTAMP | When the page was fetched |
size |
INT |
| Bytes of text |
stored_at | TIMESTAMP | Written only after the text object is stored |
| Robots rules | robots:blog.example: the allowed and forbidden paths, and the site's Crawl-delay | Read before every fetch. Kept 24 hours |
| Site lock | lock:blog.example, gone after 1,000 ms | Whoever sets it may send one request. Everyone else moves on (DD2) |
| Redis |
| Robots rules for 24 h, site locks for 1,000 ms |
| Disposable. Lost rules are fetched again, lost locks expire anyway |
| The ack. The message is gone for good |
PutItem urls[hash] if missing | Metadata store | Inserts a new URL. Refused if the row exists, and the refusal returns that row |
GetItem pages[hash] | Metadata store | Has this text been stored before? |
PUT text/{content_hash}.txt | Text store | Stores the text under its own hash |
SET lock:{site} NX PX 1000 | Domain state | Sets the lock only if it is not set (NX), for 1,000 ms (PX). OK means: your turn |
| Phase 3: Links (FR3, FR4) | Green | W1 queues the links that are new, marks the page done and deletes the message | Two new URLs in the frontier. The page is DONE |
| Phase 4: A worker dies (NFR3) | Purple | W1 dies in the middle of another page. W2 does that page again | The same result as if nothing had happened |
| 3 | W1 tries to set the lock of blog.example for 1,000 ms. It gets it: this second belongs to W1. | lock:blog.example, gone after 1,000 ms |
| 4 | W1 sends GET https://blog.example/posts/42. This thread waits. The other 149 threads keep working. | Nothing |
| 5 | The site answers after about 200 ms: 200 OK and 30 KB of HTML. | Nothing. The HTML is only in memory |
| 3 | W1 records the text, with a write that only succeeds if the row is missing. | pages row c3d9: first URL 7a1c, 5 KB |
| 4 | Later, W1 fetches https://blog.example/posts/42?ref=home, hash d04b. Its text hashes to c3d9 again. This time pages[c3d9] is found. | Nothing |
| 5 | Nothing is stored and the links of that page are not followed. The URL is finished. | urls row d04b: DONE, content hash c3d9 |
The page also links to https://blog.example/posts/41, hash 5e06. The same write is refused: the row exists. W1 skips the link. |
| Nothing |
| 3 | W1 sends the two new URLs to the frontier queue. | Two new messages: 2b8e and 9f4d |
| 4 | W1 marks the page done. | urls row 7a1c: QUEUED to DONE, content hash c3d9 |
| 5 | W1 deletes the queue message of 7a1c. Only now is it gone. | The message is deleted |
| 3 | W2 reads urls[e31a]. It says QUEUED, so the page is not finished. DONE would mean: delete the message and stop. | Nothing |
| 4 | W2 fetches the page again, with the rules and the lock of Phase 1. | Nothing |
| 5 | W2 stores the text again: the same name, the same bytes, still one object. Then it records the text. | pages row 8be2 |
| 6 | W2 handles the links as in Phase 3, marks the page done and deletes the message. | urls row e31a: DONE. The message is deleted |
| DD1 |
| Keep 4,600 fetches in flight |
| 23,000 pages a second, 200 ms per fetch |
| NFR1, NFR5 — Speed, growth |
| DD2 | One request a second per site, for the whole fleet | 100,000 URLs of one site, 40 workers | NFR2 — Politeness |
| DD3 | Fetch a URL once, store a text once | One blog post, met in three ways | NFR4 — No waste |
| DD4 | A worker dies in the middle of a page | W1 dies between a row and its message | NFR3 — No lost work |
| 63 years |
| 154 days |
| Under 5 days |
| One machine dies | The crawl stops | The crawl stops | 39 workers carry on |
| Start more workers. Nothing is copied or rebalanced |
| A thread that is refused |
| Is never refused |
| Waits |
| Gives the URL back and takes another |
| The rest of the crawl | Fine, until sites block us | Slows down to the pace of one site | Full speed |
| Redis is down | Every worker uses its own 40-second timer per site. Slower, still polite |
| Two messages, two fetches |
| One message |
| One message |
| A worker restarts | Its set is empty | Nothing changes | Nothing changes |
| The same text under a second URL | Fetched and stored again | Fetched and stored again | Fetched. Not stored, links not followed |
A worker dies after storing the text, before the pages row |
| The retry finds no row, writes the object again under the same name, then the row |
DONE and delete its message.| The link with a row and no message |
| Lost |
| Lost |
| Its message is sent again |
| The price | None, until a worker dies | Some pages are fetched twice | Some pages are fetched twice, a few messages are sent twice |
DONEW2 reads urls[7a1c]: DONE. It deletes the message and stops |
| W1 is only slow, and W2 starts the same page | Both finish. Every write is the same write twice |
| The message of 2b8e exists twice | The first copy does the work. The second finds DONE and is deleted |
| DNS becomes a bottleneck. Cache the answers in each worker for as long as the record allows, typically 300 seconds, and run a local resolver on every machine. Do not keep one address longer than that: sites rotate them |
| 1.2 million link checks a second is a lot of writes | Each worker keeps a small in-memory set of hashes that the table has already confirmed. A hit there is always right, and it answers most repeats without a network call |
| SQS or Kafka for the frontier? | SQS: its visibility timeout is exactly "fetch, then ack", and there are no partitions to manage. Kafka is worth it if you need to replay the crawl or keep one site in one partition |
| DynamoDB or PostgreSQL for the tables? | Every access is a lookup by hash on 10 billion rows, which a key-value store spreads over machines by itself. PostgreSQL is fine up to about 100 million rows, and nicer for the re-crawl scan |
| Threads or async I/O? | Async I/O lets one thread watch thousands of connections, so it needs less memory and fewer machines. Threads are simpler to explain. The design around the workers is the same |
| Two pages differ only in a date line | An exact hash sees two texts. SimHash gives similar texts fingerprints that differ in only a few bits, so near-copies can be found |
| Should we keep the HTML too? | The question said text only. Keeping it costs 300 TB, and buys the chance to extract again later without a new crawl |