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.
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"
- Browser
- Suggest API
- Prefix cache
- Trie servers
Browser to Suggest API
After a short pause in typing, the browser asks for suggestions for "sys".
Suggest API to Prefix cache
The API checks a cache keyed by prefix. Short, common prefixes are almost always there.
Suggest API to Trie servers
On a miss, a trie server walks down s, y, s. That node already stores its top 5 searches.
Suggest API to Browser
The API returns the 5 suggestions and saves them in the cache.
Write pathLearn from what people search
- Suggest API
- Search logs
- Aggregator
- Trie servers
Suggest API to Search logs
Each finished search is added to a log. This never slows the search down.
Search logs to Aggregator
Every hour a job counts how often each search appeared.
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.