Case study demo

Search autocomplete

Type a prefix and see how a trie returns the most popular completions.

An interactive demo of how this system behaves. It runs in your browser.

Case study · Live model

How search autocomplete behaves

As a user types, the browser asks for the 5 most searched phrases that start with the letters so far, the prefix. The answers come from a tree of 500 phrases, with a cache in front. A job counts past searches and builds a new tree. Change when the browser asks, what the tree stores, or start a trend.

Try it

Lab time 3.0 s
kept?top 5?new treeBrowsers, 2 searches a secondBrowsers2 searches a secondask on every keySuggest API, 58 requestsSuggest API58 requests58 key pressesPrefix cache, 43 prefixes keptPrefix cache43 prefixes keptfor 30 s eachTree servers, 500 phrases in a treeTree servers500 phrases in a treetop 5 kept at each nodeSearch log, 0 new searchesSearch log0 new searchesnot counted yetCounting job, rebuilds every 20 sCounting jobrebuilds every 20 snext in 17.0 s
  • Browsers — 2 searches a second — healthy — ask on every key
  • Suggest API — 58 requests — healthy — 58 key presses
  • Prefix cache — 43 prefixes kept — healthy — for 30 s each
  • Tree servers — 500 phrases in a tree — healthy — top 5 kept at each node
  • Search log — 0 new searches — healthy — not counted yet
  • Counting job — rebuilds every 20 s — healthy — next in 17.0 s
  • Suggestions for a prefix, please
  • The cache has the answer
  • Look the prefix up in the tree
  • The top 5
  • A finished search goes into the log
  • The log is counted
Requests per search
–
over the last 15 s; 0 key presses sent no request
From the cache
22%
of requests answered there; the rest walk the tree
Steps for one lookup
5
in the tree, last 3 s
Answers too late
0/ 55
the user had typed on
Trending phrase
–
press "Start a trend"
Next rebuild
17.0 s
0 searches wait to be counted

Numbers add up:58 key presses = 58 requests + 0 skipped + 0 waiting for a pause · 58 requests = 12 from the cache + 43 from the tree + 3 on the wayThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Typing
2 a second

The browser asks only when no key was pressed for 0.2 s.

The tree and the cache

Off: a lookup visits every phrase below the node and picks the 5 best each time.

The answer for a prefix is kept for a while, so the next request for it skips the tree.

30 s
every 20 s

Suggestions for "se" now

PlacePhraseSearches counted
1service pipeline21
2service server17
3service router14
4service limit13
5service store8

This is what the tree answers for the prefix "se". The counts change only when the tree is rebuilt.

Key presses and requests, each second
Key pressesRequests
012.52537.5501s2s3s
Show data table
TimeKey pressesRequests
1s00
1s44
2s1818
2s2020
3s3232
3s4242

What just happened

Nothing yet. Start traffic or run an experiment.

What this model simplifies
  • There are 500 phrases, made from 20 first words and 25 second words. A real service knows hundreds of millions, in many languages.
  • The tree is rebuilt every few seconds here. Real services rebuild it every few hours or once a day, and add a faster path for phrases that start trending.
  • Every search counts the same. Real rankings give more weight to recent searches, and remove phrases that should not be suggested.
  • One tree answers everyone. Real suggestions also depend on the place, the language and what the user searched before.
  • A lookup here costs a few milliseconds at most. With hundreds of millions of phrases, a visit to every phrase below a node would take far too long.
  • The cache is one box. A real service also lets the browser and servers near the user keep answers.

Design walkthrough: Search autocomplete

Autocomplete suggests full searches while you type. Typing "sys" might suggest "system design". The hard part is speed: an answer must arrive before the next key press.

Requirements

What it must do

  • Return the top 5 searches that start with what the user typed.
  • Rank suggestions by how often people searched them.
  • Pick up new popular searches within about an hour.

How well it must do it

  • Answer in under 100 ms, or the suggestions lag behind the typing.
  • Stay up even when the ranking job is down. Slightly old suggestions are fine.

Rough numbers

  • 10 million searches a day, at about 20 key presses each, is 200 million suggestion calls a day.
  • 200 million ÷ 86,400 seconds is about 2,300 calls a second on average.
  • Waiting 150 ms after the last key press before calling removes many of those calls.

How a request flows

Read pathSuggest for the prefix "sys"

  1. Browser
  2. Suggest API
  3. Prefix cache
  4. Trie servers
  1. Browser to Suggest API

    After a short pause in typing, the browser asks for suggestions for "sys".

  2. Suggest API to Prefix cache

    The API checks a cache keyed by prefix. Short, common prefixes are almost always there.

  3. Suggest API to Trie servers

    On a miss, a trie server walks down s, y, s. That node already stores its top 5 searches.

  4. Suggest API to Browser

    The API returns the 5 suggestions and saves them in the cache.

Write pathLearn from what people search

  1. Suggest API
  2. Search logs
  3. Aggregator
  4. Trie servers
  1. Suggest API to Search logs

    Each finished search is added to a log. This never slows the search down.

  2. Search logs to Aggregator

    Every hour a job counts how often each search appeared.

  3. Aggregator to Trie servers

    It builds a new trie with the fresh top 5 at every node, then swaps it in for the old one.

Scaling it

  • Split the trie by first letters across servers when it no longer fits in one machine's memory.
  • Cache one-letter and two-letter prefixes for a long time. They are the most requested and change the least.
  • Let the browser keep recent answers, so deleting a letter needs no new call.

What breaks, and what happens then

  • A trie server is down

    What users see
    Some prefixes cannot be looked up.
    What the design does
    Each part of the trie runs on several servers, and the API tries another copy.
  • The hourly job fails

    What users see
    No new trie is built.
    What the design does
    Servers keep serving the last good trie. Suggestions are a little old but still work.
  • A sudden event makes a new search popular

    What users see
    It does not appear until the next rebuild.
    What the design does
    Add a small list of trending searches that is updated every few minutes and merged in.

Trade-offs

  • Store the top 5 at every node, or work it out on each call
    A good default
    Store it. A read becomes one walk down the tree.
    What it costs
    Each rebuild does more work, and the trie uses more memory.
  • Update the trie on every search, or rebuild it in batches
    A good default
    Rebuild in batches. Reads never wait on writes.
    What it costs
    New popular searches show up late.

Common mistakes

  • Sorting the matches at request time. With millions of searches under "s", that is far too slow.
  • Calling the server on every key press with no pause, which multiplies the load for no gain.

Questions an interviewer might ask

How would you remove offensive suggestions?

Keep a block list. Filter during the rebuild, and again in the API so a new entry takes effect at once.

How would you make suggestions personal?

Merge the shared top 5 with a short list of the user's own recent searches. Keep that list on the device or in a small store per user.

What changes for a language with no spaces between words?

The trie still works on characters. The harder part is counting: the logs must first be split into meaningful terms.