Sub topic 1
The big picture: crawl, index, serve
A search engine does not look at the live web when you type a query. It answers from a copy it built earlier. This chapter gives you the model that the rest of the tutorial fills in. You will see the three stages that take a page from "unknown" to "returned in a result", what each stage produces, and where a page can drop out.
Prerequisites
This chapter assumes you can:
- Read HTTP requests, responses, and status codes such as 200, 301, 404, and 503.
- Read basic HTML, including the
<a href>link element. - Follow short Python snippets that use dictionaries, sets, and queues.
You do not need to know how ranking works. This tutorial stops at the point where the index feeds results.
Three stages, three different questions
Every web search engine splits its work into three stages. Each stage answers a different question and stores its own output.
- Crawl: Which URLs exist, and what does the server return for them? A crawler (also called a spider or bot) is a program that discovers URLs, fetches them over HTTP, and saves the responses.
- Index: What is on each fetched page, and how can it be looked up quickly? The indexer parses the saved responses, extracts text and links, removes duplicates, and writes the result into a searchable data structure.
- Serve: Given a query, which pages should be shown, and in what order? The serving system reads the index, finds candidate pages, ranks them, and builds the results page.
The stages run continuously and independently, not as one batch job. A crawler might fetch millions of pages an hour while the indexer works through an earlier batch and the serving system answers queries from an index built hours or days ago.
The loop from the stored links back to the crawler is the key structural point. Crawling feeds itself: each fetched page can reveal new URLs, and those URLs go back into the queue.
What each stage produces
The table lists the main output of each stage and the questions that belong to it. Later chapters cover each row in detail.
| Stage | Main input | Main output | Typical questions it answers |
|---|---|---|---|
| Crawl | Seed URLs, links, sitemaps | Raw HTTP responses, newly found URLs | Is the URL known? Is it allowed? What status did it return? |
| Index | Raw responses | Tokenized text, canonical URL, inverted index entries | What does the page say? Is it a duplicate? Is it allowed in the index? |
| Serve | Query, inverted index | Ranked list of results | Which pages match? Which should come first? |
Keep the outputs in mind, because they define where a problem lives. A page that was never fetched has no raw response. A page that was fetched but not indexed has a response but no index entries. A page that is indexed but never appears has entries but loses at the serving step.
A toy pipeline you can run
The following program models all three stages with a fake in-memory web, so it needs no network access. The WEB dictionary stands in for servers. The /orphan page exists but nothing links to it, so a link-following crawler never reaches it.
import re
from collections import defaultdict, deque
WEB = {
"https://example.com/": {
"text": "Home page about web crawlers",
"links": ["https://example.com/a", "https://example.com/b"],
},
"https://example.com/a": {
"text": "Crawlers follow links to find pages",
"links": ["https://example.com/b"],
},
"https://example.com/b": {
"text": "An index maps words to pages",
"links": [],
},
"https://example.com/orphan": {
"text": "Nothing links here so a link crawler never finds it",
"links": [],
},
}
def tokenize(text):
return re.findall(r"[a-z0-9]+", text.lower())
def crawl(seed):
"""Stage 1: follow links breadth-first and return url -> text."""
frontier = deque([seed])
seen = {seed}
fetched = {}
while frontier:
url = frontier.popleft()
page = WEB.get(url)
if page is None:
continue
fetched[url] = page["text"]
for link in page["links"]:
if link not in seen:
seen.add(link)
frontier.append(link)
return fetched
def build_index(fetched):
"""Stage 2: map each token to the set of URLs containing it."""
index = defaultdict(set)
for url, text in fetched.items():
for token in tokenize(text):
index[token].add(url)
return index
def serve(index, query):
"""Stage 3: return URLs that contain every query token."""
sets = [index.get(t, set()) for t in tokenize(query)]
return sorted(set.intersection(*sets)) if sets else []
fetched = crawl("https://example.com/")
index = build_index(fetched)
print(sorted(fetched))
print(serve(index, "crawlers pages"))
print(serve(index, "never finds"))The three print calls show:
- The three URLs the crawler reached. The orphan page is missing.
['https://example.com/a'], the only page that contains both "crawlers" and "pages".[]. The words "never finds" do appear on the orphan page, but that page was never crawled, so the index has no entry for them.
The third result is the most useful one. The serving code is correct, and the index code is correct. The failure happened earlier, in discovery. This pattern, where a symptom at the last stage has its cause at the first, is common in real systems.
Where the stages are decoupled
Real systems differ from the toy in one important way: the stages are separated by storage and time, not by function calls. This has three practical consequences.
- Lag between stages. A page can be fetched today and appear in the index later. Changing a page does not change search results until it is recrawled, reindexed, and the new index reaches the serving system.
- Different decisions at each stage. Being allowed to crawl a page does not mean it will be indexed. Being indexed does not mean it will rank for any query.
- Different controls at each stage. The levers a site owner has depend on the stage, as the next section shows.
Which control acts on which stage
Site owners influence the pipeline through a small set of mechanisms. Mixing them up is the most common intermediate-level mistake, so it helps to place them early.
| Mechanism | Stage it acts on | What it does |
|---|---|---|
| Internal and external links | Crawl (discovery) | Give the crawler paths to your URLs |
| XML sitemap | Crawl (discovery) | Lists URLs as a hint, not a command |
| `robots.txt` | Crawl (fetching) | Tells crawlers which URLs they may request |
| `noindex` directive | Index | Asks the engine to keep the page out of the index |
| Canonical link | Index | Suggests which URL represents a group of duplicates |
| HTTP status codes | Crawl and index | Signal success, moves, removals, and server errors |
The two controls most often confused are robots.txt and noindex. A robots.txt rule stops the fetch. A noindex rule can only work if the fetch succeeds, because the crawler must read the page to see it. Blocking a page in robots.txt and also putting noindex on it therefore hides the noindex from the crawler. A later chapter covers this interaction in full.
Why the pipeline is built this way
Splitting the work this way is not arbitrary. It follows from the cost of each operation.
- Fetching is slow and shared. Each request waits on a remote server, and one crawler hitting a site too hard can degrade it. So crawling has to be rate-limited per site and prioritized.
- Parsing and indexing are CPU and storage work. They can run in bulk on stored responses without touching the network.
- Serving must answer in milliseconds. It cannot scan pages at query time, so it reads a structure that was prepared in advance, the inverted index.
Each stage trades freshness for speed. The more work moved ahead of query time, the faster the answers, and the older the data behind them.
A first diagnostic habit
When a page does not show up in search, resist guessing. Ask the stage questions in order:
- Does the crawler know the URL exists (linked or listed in a sitemap)?
- Is it allowed to fetch it, and does the server return a usable response?
- After fetching, was the page kept in the index, or dropped as a duplicate or by a directive?
- If it is indexed, does it match the query at all?
Stopping at the first "no" saves time. The last chapter of this tutorial turns this into a full checklist.
What comes next
The next chapters follow the page through the pipeline in order: HTTP fetching, robots.txt, link extraction, URL discovery, sitemaps, the frontier, and politeness. After that come rendering, canonicalization, tokenization, the inverted index, and the controls that govern it.
Exercise
A page is linked from your home page, is not blocked in robots.txt, and returns 200. Search results never show it. Name the stages where the cause could be, in the order you would check them, and say why a crawl log alone cannot settle the question.
Show answer
Check crawl first (was it actually fetched, and did the fetch return the expected content), then index (was it dropped as a duplicate, kept out by noindex, or canonicalized to another URL), then serve (does it match the query and rank high enough to appear). A crawl log only shows that a fetch happened. It says nothing about whether the indexer kept the page or whether it ranks for a query, so a successful fetch does not prove the page is in the index.
Exercise
In the toy pipeline, add a fifth page to WEB at https://example.com/c whose text contains "crawlers" and which is linked only from the orphan page. What does serve(index, "crawlers") return, and what does this show about discovery?
Show answer
It still returns only https://example.com/, https://example.com/a. Page c is reachable only through the orphan page, and the orphan page is not reachable from the seed, so the crawler never fetches either of them. Discovery depends on a chain of links from known URLs, so a page whose only inbound links come from undiscovered pages stays undiscovered. A sitemap or an added link from a crawled page would fix it.