A keystroke becomes suggestions
Each server holds, in memory, the ten best completions for every (language, prefix) of the queries worth suggesting, built ahead of time by the hourly job. A trie whose nodes carry their best list works the same way, and so does a prefix table in a sharded key-value store. Nothing is searched, sorted or counted while the user types, so “s” costs the same as “weathe”.
The trap is the obvious query: WHERE query LIKE 'wea%' ORDER BY hits DESC LIMIT 10. The index finds the matching rows, but for “s” that is millions of rows to sort on every keystroke. Walking every query under a trie node costs the same, in memory.
- The app calls
GET /v1/on its nearest region’s host.suggest? q= wea& lang= en - The CDN answers if it has this prefix; otherwise any suggestion server does.
- The server makes one lookup of (en, “wea”) in its table and returns the ten entries with their texts.
- The app shows them if they still match the box.
Diagram as text
Components: App (Client); Load balancer (Load balancer); Suggestion server (Service); In-memory table (Cache). Connections: App → Load balancer (q=wea, lang=en); Load balancer → Suggestion server (any server); Suggestion server → In-memory table (one lookup: (en, “wea”)).
Read:Learn: Read-heavyLearn: Indexes and search fundamentals
Check yourself
Why does the prefix “s” cost the same as “weathe”?
Show answerHide answer
Both are one lookup of a list built ahead of time; the sorting happened in the build.