Designing a Web Crawler and Search Engine

Difficulty: Advanced · Prerequisites: Message Queues, Consistent Hashing, and Bloom Filters


TL;DR

Two coupled systems in one question: a crawler that discovers and downloads billions of pages, and a search service that indexes them and answers queries in milliseconds. They are joined only by storage — the crawler writes pages, the indexer reads them — which is what lets them scale independently.

The shape of the answer: a two-level URL frontier for politeness, a Bloom filter for dedup, an offline indexing pipeline, and a sharded inverted index fronted by a query aggregator.

The single most important framing: crawling is a write-heavy batch problem, serving is a read-heavy latency problem. Treat them as one system and you will design both badly.


Understanding the Problem

Two coupled systems: a crawler that discovers and downloads billions of web pages, and a search service that indexes that content and answers user queries in milliseconds. The hard parts: crawling politely without hammering any single site, avoiding re-crawling unchanged content, building a massive inverted index, and serving ranked results at sub-200ms latency.

Crawling is throughput-bound and can be arbitrarily slow per page as long as aggregate rate holds. Serving is tail-latency-bound, where a single slow shard ruins the query. Because they share nothing but the page store, the crawler can fall hours behind without the search path noticing.


Naive First Cut

flowchart LR
    SEED["Seed URLs"]
    CRAWLER["Single Crawler"]
    DB[("One DB<br/>pages + index")]
    USER["Search User"]

    SEED --> CRAWLER
    CRAWLER --> DB
    USER --> DB

New components:

  • Single Crawler: One process running the whole loop - fetch a page, store it, harvest its links, repeat.
  • One DB (pages + index): Both the raw pages and the search index in one store. These are two completely different workloads, which is the first thing the rest of the document separates.
  • Seed URLs: The handful of addresses you start from. Everything else in the crawl is discovered by following links out of pages you already have.

Why this breaks:

  • Single crawler — at 1 page/sec, crawling 10B pages takes 317 years
  • No URL deduplication — the same page is fetched thousands of times via different links
  • No politeness — hammering a single domain causes your IP to be blocked
  • One database can’t hold billions of documents AND serve as a search index
  • No ranking — results are returned in insertion order, not by relevance

The rest of the doc splits this into two independently scalable halves.


Prior Art We’re Drawing From

  • Mercator / the original Google crawler — introduced the two-level frontier (front queues for priority, back queues for per-domain politeness) that remains the standard answer to “how do you crawl fast without hammering anyone.” (The Anatomy of a Large-Scale Hypertextual Web Search Engine)
    💡 A frontier is just the crawler’s to-do list of URLs. “Two-level” means it answers two separate questions with two separate structures: which site is worth visiting next, and when is that site next allowed to be touched.
  • Apache Nutch / Heritrix — open-source crawlers that show the frontier, fetcher, parser, and dedup as separable services with durable handoffs between them. (Apache Nutch)
  • Google’s index serving — document-sharded index with a query aggregator scatter-gathering across shards, which is why P99 depends on the slowest shard rather than the average. (Web Search for a Planet)
    💡 P99 is the latency 99% of requests beat. Scatter-gather means one query is sent to every shard and the answers are merged, so the request cannot finish until the slowest shard replies - which is why a wide fan-out makes the tail, not the average, the thing you design against.
  • Bigtable — built in large part to store crawled pages and their versions at web scale; the canonical answer for “where do billions of pages live.” (Bigtable paper)
    💡 Bigtable is Google’s distributed store for enormous numbers of rows keyed by a single string - here, the URL. It is the ancestor of HBase and Cassandra, and it exists because 750TB of pages is not a database-shaped problem.

Functional Requirements

Core (top 3)

  1. Crawl the web — discover, download, and store web pages at scale (1B+ pages)
  2. Build an inverted index — map every word to the pages that contain it
  3. Serve search queries — return the top 10 most relevant results for a query in <200ms