Design Search Autocomplete (Typeahead)
Sub-50ms top-5 suggestions at 200K keystrokes/sec: CDN edge caching for short prefixes, flattened O(1) Trie in Redis, Flink 1-minute streaming deltas, exponential time-decay, and dual-layer moderation.
The whiteboard
Trace <50ms read, stream & analytics, and trie builder below — building on Caching & Failure Modes, Sharding & Hotkeys, and Queues vs Event Logs.
whiteboard · press 1–4 to trace flows
Flattened O(1) reads + streaming decay: CDN absorbs 1-2 char hotkeys, Redis serves pre-computed top-5 maps, and S3 preserves long-tail frequencies.Never traverse a distributed Trie over the network per keystroke, and never rebuild from top-5-only Redis.
Scale & APIs
< 50 ms
p99 keystroke latency
200K RPS
prefix reads · CDN + Redis
50K RPS
completed search stream
Constraints: < 50ms p99 · Availability >> Consistency · 1B DAU · trending in minutesEdge: Client debounce (50-100ms) -> CDN (1-2 char prefixes) -> gateway
Read Path: GET /search/suggestion?q=text -> String[] (top 5)Write Path: GET /search?query=... -> 200 OK (emits to Kafka)Moderation: admin svc -> blocklist Redis (synced to typeahead + Flink)How it works
1. Sub-50ms read path (CDN -> typeahead -> Redis)
- Debounce + CDN hotkey shield: Client debouncing (
50–100ms) cuts burst noise, while CDN caching absorbs ultra-hot 1–2 char prefixes ("a","th") before they create hot-shard skew in Redis. - Flattened
O(1)Redis map: Sharded byhash(q), Redis stores pre-computedprefix -> [top 5]pairs so every keystroke is oneO(1)lookup instead of multi-hop pointer traversal. - Read-path blocklist:
typeaheadfilters results against a locally synced copy ofBlocklist Redisso urgent takedowns disappear in milliseconds.
2. Streaming ingestion (search -> Kafka -> Flink)
- 1-minute tumbling windows: Completed searches (
50K RPS) flow through Kafka into Flink, which normalizes text, strips banned terms, writes durable analytics to ClickHouse -> Glacier, and emits compact 1-minute deltas toTrie Builder.
3. Trie Builder, time-decay & S3 snapshots
- Full state in S3: Redis drops ranks
#6..N, soTrie Builderloads/saves the complete Trie snapshot in S3 to preserve long-tail queries that may suddenly trend tomorrow. - Exponential decay + bottom-up rollup: Each merge updates
new_score = (old_score * α) + new_count(α < 1, e.g.0.9) so dead trends fade and breaking news climbs in minutes; a bottom-up pass merges only children’s top 5s and pushes the flat map to Redis.
lesson
Dual-layer moderation is the senior differentiator: filtering in Flink keeps banned terms out of the Trie and analytics, while filtering again on the typeahead read path makes emergency bans take effect in milliseconds instead of waiting for the next Trie build.
Check yourself
interactive · stress-test this typeahead design against classic Google/Amazon probes
01 · Why flatten the Trie into Redis instead of traversing it live?
200K RPS reads · <50ms p99 · 100M prefixes
Your Trie has 100M prefixes. Why does Trie Builder pre-compute prefix -> [top 5] bottom-up and push flat key-value pairs to Redis instead of letting typeahead walk Trie pointers on every keystroke?
Select an option to check the trade-off.
02 · Why keep the full Trie snapshot in S3?
long-tail queries · breaking news · state recovery
Since Redis already holds the latest prefix -> top 5 results, why does Trie Builder load its previous state from an S3 snapshot instead of reading from Redis?
Select an option to check the trade-off.
03 · Why CDN-cache 1-2 character prefixes?
hash(q) sharding · 200K RPS · 26 single letters
Redis is sharded by hash(q). During peak hours, 40% of all keystrokes are 1- or 2-letter prefixes ('a', 't', 'th', 'we'). What happens without CDN caching?
Select an option to check the trade-off.