CodingNodes Space play · ←→ step · R restart

The problem

Autocomplete is the search that starts before the user has finished thinking. Three characters in, a shortlist of suggestions is already waiting. That feels instant, but behind it is a lookup that must turn a partial word into a ranked list of completions — for millions of distinct prefixes, on every keystroke, across a corpus that keeps changing.

The design that makes it feel free has two halves. On the read side, a prefix index answers 'what words start with this' in a single walk, a cache absorbs the popular prefixes that make up most of the traffic, and a debounce keeps the client from firing a request per keypress. On the write side, a quiet pipeline feeds the index so it stays current without ever blocking a reader. We will build both halves, then close the loop with one typed prefix racing from the box to a ranked list.

Requirements

Functional — what it must do

  • Suggest: as a user types a prefix, return a shortlist of the most likely completions — the first few characters are enough to start.
  • Rank: order the completions by how likely they are to be chosen, so the top result is the one most users would tap.
  • Stay current: a search that lands, or a new popular term, should surface in suggestions quickly, without restarting the service.

Non-functional — how well it must do it

  • Suggestions answer within about 100 milliseconds, and the common case — a hot prefix — answers in single-digit milliseconds from the cache.
  • The read path must not depend on the index being rebuilt: new documents feed the index in the background, and readers never wait for it.
  • The prefix space is large enough to shard: one trie will not hold it, and a prefix must always land on the same shard so a read never fans out.

Scale — how much it must take

  • On the order of a few thousand suggestion requests per second at peak, drawn from a corpus of billions of historical searches and a large document set.

Assumptions — the numbers this design is sized for

A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.

Requests
~3,000 / s
Suggestion lookups at peak, after debouncing. Debouncing is what keeps this a few thousand rather than a few million.
Prefixes
tens of millions
The distinct prefixes the trie must hold. Large enough that one machine cannot keep them hot.
Hot share
~80% of traffic
A small set of popular prefixes accounts for most lookups. That is the cache's whole business case.
Latency
< 100 ms
The budget for a cold prefix. A cached hot prefix lands well under it.
Corpus
billions of searches
Historical queries are the raw material for popularity ranking and for the candidates themselves.
Shards
by prefix hash
The prefix space is cut so each prefix always maps to one shard — a read is a single hop, not a scatter.
Debounce
~200 ms
The client waits for a pause in typing before firing. That collapse is what turns keystrokes into a handful of requests.

Takeaways

Part 1

What a suggestion is

A suggestion is a completion with a score. Fixing that — a prefix in, a ranked list of full terms out — makes the rest of the design a lookup problem with a popularity ordering.

  1. 1
    Prefix in, completions out

    The input is a short prefix — the characters the user has typed. The output is a shortlist of complete terms that begin with that prefix, ordered from most to least likely. There is no free text, no match-anywhere; it is strictly 'what starts with this'.

  2. 2
    It is a lookup, not a search

    Because it is a prefix match, the structure needed is a prefix index — a trie — not a full inverted index over the corpus. A walk down the trie by the prefix's characters lands exactly on the set of terms that begin with it. That is the entire lookup; ranking is a separate, cheap pass over that set.

  3. 3
    Popularity is the score

    Among the terms that share a prefix, order is decided by how often users have chosen them. 'google' beats 'goa' under the prefix 'go' because the historical click data says so. Popularity is computed from the corpus of past searches and attached to each node, so it is already there when the prefix is walked.

Part 2

Don't fire per keystroke

The cheapest optimization is at the client: wait for a pause in typing, then send one request for the whole prefix so far. Debouncing collapses a burst of keypresses into a handful of lookups.

  1. 1
    Keystrokes are not requests

    A user types 'g', 'o', 'o' in quick succession. If each keypress fired a lookup, the server would answer three times for a word the user has not finished. The client instead watches for a pause and fires once, with the full prefix typed so far.

  2. 2
    A short window is enough

    The debounce window is short — a couple hundred milliseconds — so the user does not feel a lag, but it is long enough to catch the rest of a fast word. The result is that a few thousand requests per second at the service is already the post-debounce number; the raw keystroke rate is far higher and never reaches the server.

  3. 3
    Cancel the stale request

    If the user keeps typing before the first lookup returns, that response is already stale — it is for a shorter prefix. The client discards in-flight responses for prefixes it has since moved past, so an old result never overwrites a newer one on screen.

Part 3

The read path: walk the trie

A prefix lookup walks the trie to the node for that prefix and collects the top terms beneath it. A cache in front answers the popular prefixes without touching the index at all.

  1. 1
    Walk down by the prefix

    The prefix 'g', 'o', 'o' is walked one character at a time down the trie, each character choosing the next edge. At the node for 'goo', the subtree below is exactly the set of terms that begin with 'goo' — nothing else. The lookup cost is the length of the prefix, not the size of the corpus.

  2. 2
    Collect and rank the subtree

    From the node for the prefix, the top terms in the subtree are gathered, each carrying its popularity score, and the shortlist is the highest-scoring handful. Because the scores were attached at index time, this is a sort over a small set, not a computation over the corpus.

  3. 3
    The cache answers the hot prefixes

    The same popular prefixes are looked up over and over, so their shortlists are cached. A hot prefix never walks the trie at all: the answer is served from the cache in single-digit milliseconds, and the index only sees the long tail of uncommon prefixes.

Part 4

Shard the prefix space

Tens of millions of prefixes will not fit hot on one machine. The trie is cut into shards by the prefix, so each lookup is a single hop to the shard that owns that prefix.

  1. 1
    One trie is not enough

    The full prefix index is too large to hold in fast memory on a single box, and a single box has a hard throughput ceiling. The index must be split, and the split must be chosen so a prefix always lands in exactly one place.

  2. 2
    Hash the prefix, land on one shard

    A router hashes the prefix and sends the lookup to the shard that owns it. Because the same prefix always hashes to the same shard, a read is a single hop — there is no scatter, no merge, no fan-out. The shard boundary follows the prefix, not the document.

  3. 3
    Rebalance by prefix range

    Shards are ranges of the hash space, so adding a shard reassigns only the slice of prefixes between it and its predecessor — the same bounded hand-off the consistent-hash ring gives a key-value store. Scaling out is a steady trickle of prefix-range moves, not a rebuild.

Part 5

Keep the index current

The index is only as good as its last feed. A background pipeline turns new documents and new popular searches into trie nodes with popularity scores, without ever blocking a reader.

  1. 1
    Documents and searches feed it

    Two sources keep the index alive. New documents contribute their terms as candidate completions, and the stream of historical searches contributes the popularity signal that orders them. Both flow into an ingestor that turns them into the (term, score) pairs the index stores.

  2. 2
    Build a batch, swap it in

    The index builder accumulates changes into a batch and, when the batch is ready, builds a new version of the affected shard slice and swaps it atomically into service. Readers never see a half-updated index: they see either the old slice or the new one, never a mix.

  3. 3
    Readers never wait for the write

    Because the index is built and swapped in the background, a read path that takes under a hundred milliseconds is independent of how long the next batch takes. The two paths share the data structure but never share the clock — that separation is what keeps suggestions feeling instant even as the corpus grows.

★ Finale

End to end

One typed prefix, from box to ranked list: debounced, routed to its shard, walked down the trie, ranked by popularity, with the cache absorbing the hot ones and the writer keeping the structure current behind it all.

  1. 1
    The whole system

    Every piece in one frame. The typed prefix is debounced, routed to the shard that owns it, answered from the cache when hot or the trie when cold, ranked, and returned. Documents and searches feed the index in the background, off the read path entirely.

  2. 2
    A hot prefix, from the cache

    The user types a popular prefix. After a pause, the debounced request arrives, the cache already holds the ranked shortlist for it, and the answer goes straight back. The trie is never touched — this is the case the whole cache exists to absorb.

  3. 3
    A cold prefix, through the trie

    An uncommon prefix misses the cache. The router hashes it to its shard, the shard walks the trie to the node for the prefix, collects the top terms in the subtree, and ranks them by their stored popularity scores before the shortlist goes back. Under a hundred milliseconds, end to end.

  4. 4
    Current, without blocking

    While those reads flow, the write path is quietly doing its job: new documents and the latest searches are turning into (term, score) pairs and being swapped into the shards in the background. A prefix the user typed today can already be a hot, cached suggestion by tomorrow — and no reader ever waited for it to happen.

🌐 In the wild

In the wild

Prefix suggestion is one of the most ubiquitous interactions on the web. The boxes below are anchored to the classes of well-known systems that run each piece — an illustrative mapping, not a claim about any one product's internals.

  1. 1
    The pattern, piece by piece

    Each box is a standard building block of a prefix-suggestion service. Anchoring them to the classes of production systems that run that piece turns the abstract pipeline into 'the shape behind the suggestions you tap'.

  2. 2
    Why the same shape keeps winning

    Independent search products converge on the same pieces because each one removes a real cost: the trie removes the full-corpus scan, the cache removes the hot-prefix latency, the debounce removes the per-keystroke load, the shard removes the single-box ceiling, and the background writer removes the read/write clock collision. That convergence is the evidence the design is load-bearing.