Autocompletado de Búsqueda

Typeahead

Escrito por Darjus Kazakevic · revisado el

Precompute the top answers for every prefix, then just look them up.

As a user types into a search box, show the 5 most popular past searches that start with the typed text. Popularity comes from the query logs. Suggestions follow the user's language, and new trends should appear within about an hour.

Every keystroke is a request, so the load is several times the search load. The round trip must stay under 100 ms at p99, or the suggestions arrive after the next letter. Suggestions may be slightly stale, but never offensive.

En esencia — Do the hard work offline: count past searches, build a prefix tree with the top 5 completions in every node, and serve each keystroke as one cached lookup.

Cuándo recurrir a él

Recurre a él cuando

  • Users type a prefix and expect suggestions on every keystroke
  • Popularity can be an hour old, so the index can be built offline
  • Many users type the same prefixes, so an edge cache absorbs most traffic

Reconsidéralo cuando

  • Suggestions must match words inside the text: use a search index
  • Every suggestion is personal, so a shared cache cannot hold it
  • The list is small, like 500 product names: filter it in the browser

The components

Query log aggregation (uses a message queue)
Every submitted search goes to a log such as Kafka. A batch job runs every hour, counts searches per query and language, gives recent days more weight, and drops blocked terms.
Trie with top-k per node (uses a prefix index)
A trie (prefix tree) has one node per character. Each node also stores the k best completions below it, chosen at build time, so a lookup reads one node.
Suggestion servers (uses database sharding)
Hold the trie in memory, split by first characters (a–f, g–m, …) and replicated three times. A new build loads beside the old one and the switch is atomic.
Edge and browser cache (uses a CDN)
Short prefixes are the most requested and the same for every user in one language. A CDN caches them for an hour, and the browser caches every answer it has already received.

Key decisions

Precompute the top 5 in every node
Searching the subtree under "a" on every request touches millions of nodes. Store the best 5 completions in each node when you build the trie, and a lookup reads a single node.
Build offline and swap in one step
Updating counts on every search turns the read path into a write path. Rebuild the trie from aggregated logs every hour, load it beside the old one, then switch.
Cache short prefixes at the edge
One- and two-letter prefixes are the most requested and the same for everyone in a language. Serve them from a CDN with a 1-hour TTL, so most keystrokes never reach you.
Wait and cancel on the client
Send a request only after about 100 ms without a keystroke, and cancel the previous one. That cuts requests to about one per four keystrokes, and an old answer cannot replace a newer one.

Errores habituales

  • Searching the subtree on every request. It is fast for "kubernet" and very slow for "a", which is the prefix users send most.
  • Counting keystrokes as searches. Partial prefixes like "ama" and "amaz" fill the log and the suggestions. Count only submitted searches.
  • No filter step. A trending offensive query, or one with a person's private details, appears for everyone. Filter with a block list at build time.
  • Answers arriving out of order. The answer for "ap" lands after the one for "app" and replaces it. Cancel old requests, or drop answers for a prefix the user has passed.