Skip to content
SD-8 · Back to the write-up

Design a Typeahead Autocomplete

Precompute the top 10 for every prefix offline, keep the whole trie in memory on every server, and let the edge answer the common prefixes.

Follow a request

Clients
Edge
Serving
Pipeline
Search boxDebounced
Search serviceRuns searches
Edge cache60% of prefixes
Suggest serversTrie in memory
Trending storeRising queries
Search logEvent stream
Trend counterLast 15 min
Query counts30 days
Trie builderNightly
Snapshot storageBuilt tries
1box → cdn · GET ?q=new yoA longer prefix, “new yo”, is less common, and this edge’s copy has expired.1 / 5

The numbers, at a glance

For every search run……6 suggestion requests arrive
1 search ≈ 50,000/s6 requests ≈ 300,000/s
Where requests go600,000 keystrokes/s at peak
The edge does most of itShort prefixes are the same for everyone
So 12 servers are enough for 300,000 requests a second.

A keystroke, in order

Edge miss shown. On a hit, the edge answers and the suggest server and trending store disappear.

Search box
Edge cache
Suggest server
Trending store
1 GET ?q=new yo&locale=en-US
2 miss
3 refresh local copy (every 30 s)
4 top 10 + trend, filtered
5 200, max-age 60
RequestResponseAsync, does not block

How a prefix becomes 10 answers

The decision this question is really about.

Database prefix query
Too slow
GET ?q=ne
LIKE 'ne%' ORDER BY count
2,000/s per DB
+ Always current.− Reads and sorts millions of rows per key.
Live-updated index
Writes everywhere
search
update 12 prefixes × N replicas
reads wait on locks
+ Fresh on every search.− Every replica takes every write.
Prebuilt trie + top 10
Our pick
nightly build
n → e → top 10+ trends
edge caches short prefixes
+ Microsecond lookups, read-only.− Needs a pipeline and a trend layer.

How it grows

Add a box only when a number forces you to. Highlighted boxes are new at each stage.

Stage 1 · ≈ 1,000 keystrokes/sMake it work
Search box
App server
SQL database
A prefix query and a sort on a SQL table of counts.
Stage 2 · ≈ 100,000 keystrokes/sPrecompute
Search box
Load balancer
Suggest servers
Search logTrie builderSnapshot storage
A nightly trie with a top 10 per prefix, replicated in memory; searches go to a log.
Stage 3 · ≈ 600,000 keystrokes/sEdge and trends
Search box
Edge cache
Load balancer
Suggest serversTrending store
Search logTrend counterTrie builder
Short prefixes cached at the edge; rising queries merged in within minutes.
Was this walkthrough useful?Your vote tells us what to improve next.