LOAD-BEARINGfree
Architecture labTier 13 · unit 2 of 31

Typeahead search

As the user types, return the most popular completions of their prefix. Suggestions come from what people actually searched for, so the index must be rebuilt from query logs.

Envelope50k QPS · p99 < 100 ms · top 5 suggestions per prefix

What the design must do

  • Return top-k completions for any prefix
  • Serve from memory, never from disk
  • Rank by real query popularity
  • Rebuild the index offline from logged queries

The worked envelope

Assumed. 50k QPS, 100M distinct logged queries of which the top 10M are worth indexing, average query 20 characters.

Worked envelope
Trie nodes10M queries × 20 chars, heavy prefix sharing ≈ 30M nodes
Node costtop-5 postings (5 × 12 B) + child map ≈ 150 B
Index in RAM30M × 150 B ≈ 4.5 GB — one machine
Work per querywalk ≤20 nodes, return a stored list: microseconds
Response5 suggestions × ~40 B ≈ 200 B
Egress50k × 200 B = 10 MB/s
Log volume100M searches/day × 50 B = 5 GB/day to rebuild from

What the numbers decide. The index fits in memory on one box with room to spare, and the lookup itself is microseconds. So nothing about this system is compute-bound or storage-bound — the 50k QPS is entirely a matter of how many network sockets and serialisation cycles you can service, which is a replication problem, not a data problem.

That is the whole design in one observation: replicate a read-only in-memory structure as many times as you need, and rebuild it offline. The moment you notice the index is read-only between rebuilds, every hard distributed-systems question disappears.

The decisions, and what each one costs

Trie with the top-k completions precomputed at every node

Because
Computing "most popular completion of ca" at request time means walking every descendant; storing the answer at the node makes it a pointer read
The cost
The index is larger and only correct as of the last rebuild; a query that spiked an hour ago is not there yet
Rejected
Ranking at request time, which turns a microsecond walk into a subtree scan and cannot hold 50k QPS

Serve from an in-memory replica set, rebuild offline from the query log

Because
4.5 GB is one machine, and the structure is read-only between rebuilds, so replicas need no coordination and scale linearly with QPS
The cost
Suggestions lag reality by a rebuild cycle; every replica needs the whole index, so it is memory per box, not memory divided by boxes
Rejected
A shared datastore behind the API, which reintroduces a network hop into a budget measured in microseconds

A small cache in front of the trie

Because
Prefix popularity is extremely skewed — one- and two-character prefixes are a large share of all traffic and their answers never change between rebuilds
The cost
One more component and one more thing to warm after a deploy
Rejected
Nothing — this one is close to free, which is why it is worth doing even though the trie is already fast

Merge personal history at request time rather than indexing per user

Because
Per-user tries multiply memory by the user count, and the shared trie is the thing that fits in 4.5 GB
The cost
Request-time merge costs a lookup into a per-user store and complicates the cache key
Rejected
Personalised indexes, which turn a 4.5 GB problem into a petabyte problem for a ranking tweak

Components in the reference design

Client
Browser or mobile app
Load Balancer
L7 routing, TLS termination, health checks
App Service
Stateless business logic
Cache
Redis / Memcached, sub-ms lookups
Search Index
Inverted index / trie, text and prefix
Event Log
Kafka-style, ordered, replayable
Worker
Async consumer, background jobs

What not to draw

  • SQL DB — A relational LIKE prefix% query cannot hold 50k QPS under 100 ms. The serving path must be an in-memory prefix structure.

The reference design

Serving structure. A trie where every node stores the precomputed top-k completions for the prefix ending at that node. A lookup walks the prefix — at most a few dozen pointer hops — and returns a ready-made list. No ranking work at request time. Production systems compress this into an FST to fit the whole index in memory.

Why precompute top-k. Computing "most popular completion of ca" at request time means traversing every descendant. Storing the answer at the node turns a subtree walk into an array read, at the cost of a larger index and a rebuild whenever popularity shifts.

The rebuild pipeline. Every executed search is written to an event log. A batch job aggregates counts over a trailing window (recency-weighted, so yesterday's news does not outrank a decade of history), rebuilds the trie, and ships the new snapshot to the serving fleet. Hourly is normal; the index is deliberately slightly stale.

Cache. Prefix popularity is extremely skewed — short prefixes are hit constantly. A small cache in front of the trie absorbs most of the traffic before it does any work at all.

Two things the naive design misses: personalisation and safety. Blended personal history changes the ranking per user, so the top-k must be merged at request time rather than served raw. And a blocklist has to be applied at serve time, not at index time, because it changes faster than the rebuild cycle.