Autocomplete system design: how it works

Suggests the top searches for what you've typed so far, in milliseconds, from a prefix index rebuilt from recent searches.

Loading the interactive diagram…

How Autocomplete works, step by step

Suggests the top searches for what you've typed so far, in milliseconds, from a prefix index rebuilt from recent searches.

People

Search box
Client · Hypothetical. Someone typing "iph" into a search box.

Front door

Edge cache
Cache · Hypothetical. Keeps the answers for popular prefixes for a minute or two, close to users.
API gateway
Gateway · Hypothetical. Receives the cache misses and finished searches.

Services

Suggestion service
Service · Hypothetical. Looks up the prefix and returns its top five suggestions. No sorting at request time.
Search log
Queue · Hypothetical. Every finished search, as an event, for counting later.
Trend aggregator
Service · Hypothetical. Counts searches over time, filters out offensive ones, and rebuilds the prefix index.

Data

Prefix index
Cache · Hypothetical. A trie: one entry per prefix, each holding its top five suggestions. Kept in memory.
Query counts
Database · Hypothetical. How often each search was made, per hour and per day.

Flow: Type a few letters

A prefix the edge cache doesn't have: one lookup in the prefix index, and the answer is cached on the way back.

  1. Search box → Edge cache (GET /suggest?q=iph). The person pauses after typing "iph". The box asks for suggestions.
  2. Edge cache → API gateway (q=iph). The edge cache doesn't have a fresh answer for "iph", so it passes the request on.
  3. API gateway → Suggestion service (Top 5 for iph). The gateway sends it to the suggestion service.
  4. Suggestion service → Prefix index (Prefix "iph"). One lookup: the top five for "iph" were worked out in advance.
  5. Prefix index → Suggestion service (iphone 17, iphone 16…). The five suggestions come back.
  6. Suggestion service → API gateway (5 suggestions). The same answer suits everyone typing "iph" in this language and region.
  7. API gateway → Edge cache (5 suggestions). The edge cache keeps a copy for the next minute or two.
  8. Edge cache → Search box (5 suggestions). The search box puts any of your own recent matching searches first, from history kept on your device, and shows the dropdown, well under a tenth of a second after the pause.

Flow: A popular prefix (cache hit)

The usual case for short prefixes: the edge cache answers without reaching the servers.

  1. Search box → Edge cache (GET /suggest?q=we). Someone types "we", like many people every second.
  2. Edge cache → Search box (weather, weekend plans…). The edge cache has a fresh answer and returns it straight away.

Flow: Searches shape the suggestions

Each finished search is counted, and the index is rebuilt every few minutes. Background work: nobody waits on these steps.

  1. API gateway → Search log (Searched "ipl score"). When someone runs a search, the event is logged.
  2. Search log → Trend aggregator (Searched "ipl score"). The aggregator reads the stream of searches.
  3. Trend aggregator → Query counts (ipl score +1). It adds to the counts, weighting recent searches more.
  4. Trend aggregator → Prefix index (New index v812). Every few minutes it rebuilds each prefix's top five from the counts, drops anything on the blocklist, and swaps the new index in. "ipl score" now shows up for "ip" during a match.

HowItScales is not affiliated with the companies shown. Components marked Assumption or Hypothetical are not confirmed by the company.