Design Typeahead Autocomplete
Understanding the Problem
🔎 What is typeahead? Typeahead (also called autocomplete or search suggestions) is the dropdown that appears under a search box as you type. After "how to t" it offers "how to tie a tie" and "how to take a screenshot," ranked by how likely you are to want each one. It has to feel instant, which means a fresh answer on almost every keystroke, faster than the user can type the next character.
The question comes in several flavors: Google-style query suggestions, Amazon's product search box, Meta's search bar that suggests people, pages and groups, "instant search" that shows actual results while you type, typo-tolerant ("fuzzy") suggestions, and next-word suggestions. They're all the same core problem: map a partial input to a short, ranked list of completions, very fast, from an index built offline out of what people search for. We'll design the general version and handle each variant in the deep dives.
Functional Requirements
Start by agreeing on what the box actually does. The search results page behind it is a different system, so it's worth saying out loud that we're only designing the suggestions.
Core Requirements
- Users should be able to see up to 10 suggestions for the text they've typed so far, refreshed as they type.
- Suggestions should be ranked by popularity, and the ranking should follow what people are searching for, including new queries that suddenly take off.
- Users should still get useful suggestions when they make a small typo.
Below the line (out of scope)
- The search results themselves (ranking documents, the results page). See Design a Web Search Engine.
- Ads and sponsored suggestions.
- Languages that need special input handling (Chinese pinyin input, Japanese IME). We'll assume space-separated languages.
Non-Functional Requirements
Ask your interviewer for scale. We'll assume a large general-purpose search product: 1 billion searches a day, suggestions in every major locale.
Core Requirements
- The system should return suggestions in under 100ms at p99, end to end. Anything slower and the dropdown lags behind the typing and feels broken.
- The system should handle on the order of 150k suggestion requests per second at peak.
- The system should be highly available. If suggestions are down, search still works, but the product feels noticeably worse.
- The system should pick up a new trending query within a few minutes, and general popularity shifts within a day. Suggestions don't need to be strongly consistent; two users seeing slightly different lists is fine.
Below the line (out of scope)
- Exact counts. "Roughly the most popular" is the goal.
- Strict per-user privacy controls beyond not leaking one user's history to another.
Now the math. Users type about 20 characters per query, but the client waits for a short pause before calling us (a debounce) and skips requests it can answer from its own cache, so call it 5 suggestion requests per search. That's 5 billion requests a day, or 5B / 86,400 ≈ 58k requests per second on average, and with a 2-3x daily peak, around 150k QPS. Meanwhile, the write side is the search log: 1B searches a day is about 12k events per second.
How big is the index? Most queries are long tail. If we keep only queries searched at least a handful of times in the last month, we're left with roughly 100 million distinct queries per large locale, averaging about 20 characters. That's 2 GB of raw query text. Even with a tree structure and a precomputed top-10 list at every node (more on that below), it lands in the low tens of gigabytes.
That last number is the important one. The whole index fits in the memory of a single server. So we don't need a distributed database on the read path at all: we can build the index offline and give every serving machine its own full copy. Scaling reads becomes "add replicas," which is the easiest kind of scaling there is.