Skip to main content

Search Autocomplete

1. Executive Summary​

A search autocomplete (typeahead) system returns ranked prefix suggestions as a user types, typically within tens of milliseconds, across millions of queries per day. Principal-level design covers prefix indexing (trie, n-gram, or inverted index), ranking (popularity, personalization, freshness), caching at multiple tiers, debouncing and request coalescing, and failure degradation when the index or ranking service is slow.

This chapter designs a Google/Amazon-class autocomplete serving 500M+ suggestions per day with p99 latency under 50 ms globally. Trie-based or inverted-index prefix retrieval, popularity-weighted ranking, CDN edge caching for top prefixes, and explicit behavior when personalization backends fail are mandatory interview topics—not optional polish.

2. Why This Topic Matters​

Autocomplete appears in e-commerce, social search, developer tools, and enterprise knowledge bases. Architects must explain:

  • Why prefix trees or edge n-grams beat naive LIKE 'prefix%' on relational databases.
  • Ranking signals and how to blend popularity with personalization without latency blowups.
  • Cache key design for partial prefixes and per-user variants.
  • Thundering herd when a trending query spikes.
  • Privacy implications of logging keystrokes.

Poor autocomplete design causes search abandonment, database overload from prefix scans, and embarrassing suggestions during incidents. Principal reviews ask "what happens when the ranking model is 200 ms behind"—the answer must include static fallback lists and circuit breakers, not only index topology. Review Caching Fundamentals and System Design Methodology before mock interviews on this topic.

3. Problems Being Solved​

ProblemSolution
Low-latency prefix matchTrie, inverted index, or dedicated prefix store
Relevance rankingPopularity, CTR, personalization blend
High read QPSMulti-tier cache; read replicas
Fresh trending queriesStreaming popularity updates; short TTL
PersonalizationUser history sidecar; don't block on miss
Global latencyGeo-distributed cache; regional index replicas
Abuse / injectionRate limits; blocklist; input sanitization
Stale suggestionsIncremental index updates; versioned cache

4. Assumptions and System Model​

Phase 1: Clarify Requirements​

Functional:

  • Return top-K (typically 5–10) suggestions for prefix string (min length 2–3 chars).
  • Support locale, category context (e.g., "products" vs "users").
  • Optional: spell correction, trending badge, sponsored slots.
  • Log impressions and selections for ranking feedback.

Non-functional:

  • p99 latency < 50 ms same region; < 150 ms cross-region.
  • 50K–500K autocomplete QPS at peak (large retailer scale).
  • 99.99% availability for read path.
  • Index update latency < 5 minutes for popularity shifts; < 1 hour for new catalog terms.

Non-goals: Full-text search results page; complex semantic ranking (separate search service).

AssumptionImplication
Read-heavyOptimize GET path; async index updates
Skewed prefixesCache hot prefixes aggressively
Short prefixes ambiguousRequire min length; broader matches need better ranking
Eventual consistency OKBrief staleness on trending acceptable
User-specific optionalPersonalization must degrade gracefully

5. Essential Terminology​

TermDefinition
TypeaheadUI pattern showing suggestions while typing
Prefix searchMatch strings beginning with query prefix
TrieTree structure where each edge is a character
Compressed trie (radix tree)Merged single-child paths for memory efficiency
Edge n-gramToken substrings indexed for prefix retrieval
Top-KK highest-ranked matching suggestions
CTRClick-through rate—selection / impression ratio
DebounceClient delay before sending request
SingleflightCoalesce concurrent identical requests
Bloom filterProbabilistic structure for existence checks
Sponsored suggestionPaid placement with disclosure
Query logStream of prefixes and selected completions

6. Core Mechanism​

6.1 Phase 5: High-Level Architecture​

Figure 1: Autocomplete read path with CDN, prefix index, popularity ranking, and offline rebuild pipeline.

6.2 Phase 3: Define APIs​

Autocomplete API:

GET /v1/suggest?q={prefix}&locale=en&category=products&limit=8
→ { suggestions: [{ text, score, type, metadata }], request_id }

Admin / index API:

POST /v1/index/terms (batch upsert terms with weights)
DELETE /v1/index/terms/{id}
GET /v1/index/health
POST /v1/blocklist (emergency remove offensive terms)

Analytics callback (client):

POST /v1/events
{ request_id, selected_index, selected_text, prefix }

6.3 Phase 4: Model Data​

Prefix index (options):

  1. In-memory trie per shard — O(prefix length + K log K) retrieval; memory-bound.
  2. Elasticsearch/OpenSearch completion suggester — FST-based; operational maturity.
  3. RocksDB + custom FST — Used at very large scale (implementation choice).

Per suggestion record:

{ term_id, display_text, normalized_text, popularity_score,
category, locale, last_updated, sponsored_flag }

Popularity store (Redis/KeyDB):

  • Key: pop:{locale}:{normalized_prefix} → sorted set of term_ids by score.
  • Updated every 1–5 minutes from streaming aggregator.

Query log (Kafka):

  • { timestamp, user_id_hash, prefix, suggestions_shown, selected }

6.4 Phase 6: Deep Dives​

Prefix retrieval algorithm:

  1. Normalize input: lowercase, Unicode NFKC, strip dangerous chars.
  2. If prefix length < min_length → return empty or curated defaults.
  3. Lookup trie/FST for all terms with prefix (cap candidate set at 100–500).
  4. Join popularity scores; apply business rules (blocklist, category filter).
  5. Rank: final_score = α·popularity + β·personalization + γ·freshness.
  6. Return top-K; attach request_id for analytics.

Caching strategy:

TierKeyTTL
CDNsuggest:{locale}:{category}:{prefix}60s–300s
App Redissame30s–120s
In-process LRUhot prefixes5s

Shorter TTL for short prefixes (more volatile); longer for 4+ char prefixes.

Personalization without latency spike:

  • Fetch personalization in parallel with index lookup; 10 ms budget.
  • On timeout → popularity-only ranking (degrade).
  • Never block autocomplete on ML model inference > 20 ms.

Figure 2: Read path with parallel index and popularity fetch; cache populate on miss.

Trending query handling:

  • Spark/Flink job counts prefix selections per minute.
  • Boost terms with velocity spike (compare to 7-day baseline).
  • Emergency pipeline can inject blocklist updates in < 60s via pub/sub.

Figure 3: Offline popularity and nightly index rebuild with hot delta reload.

6.5 Index sharding​

Shard by hash(locale + first_char) or category. Cross-shard prefix queries need fan-out only for global search—usually scoped by category to avoid it. Rebalance using consistent hashing when adding index nodes.

7. Step-by-Step Walkthrough​

7.1 Normal typeahead​

  1. User types "lapt" after debounce 150 ms.
  2. CDN miss; API receives request.
  3. Redis miss; prefix index returns 200 candidates starting with "lapt".
  4. Popularity ZSET ranks "laptop", "laptop bag", "laptop stand" in top 8.
  5. Response in 18 ms; cached in Redis and CDN.

7.2 Personalization timeout​

  1. User with purchase history types "iph".
  2. Index returns candidates including "iphone case".
  3. Personalization service times out at 10 ms.
  4. API returns popularity-ranked list without personalization boost.
  5. Log degradation metric; no user-visible error.
  1. Celebrity event causes spike in prefix "tayl".
  2. Trend detector boosts "taylor swift tickets" within 2 minutes.
  3. Hot reload pushes score delta to index servers.
  4. Cache keys for "tay", "tayl" invalidated via pub/sub.
  5. New suggestions appear without full index rebuild.

7.4 Offensive suggestion incident​

  1. Offensive term enters index via bad vendor feed.
  2. Operator adds term to blocklist API.
  3. Pub/sub propagates blocklist to all API nodes within 30s.
  4. Post-incident: add human review gate on index ingest.

7A. Design Phase Summary​

PhaseSectionKey decisions
Requirements§4top-K prefix, <50ms, locale
Scale§10500K QPS; CDN + Redis
APIs§6.2GET suggest + analytics
Data model§6.3FST/trie + popularity ZSET
Architecture§6.1edge cache → index + rank
Deep dives§6.4degrade personalization
Reliability§8–9static fallback lists
Security§13rate limit; blocklist
Operations§12cache hit ratio; index lag
Tradeoffs§16trie vs ES completion

8. Invariants and Guarantees​

PropertyGuarantee
Read availabilityDegraded static lists if index down
ConsistencyEventual; popularity lag 1–5 min
OrderingDeterministic rank given same scores
SafetyBlocklist applied before response
IdempotencyRead-only suggest API is naturally idempotent

9. Failure Scenarios​

ScenarioMitigation
Index shard downReplica failover; partial results
Redis outageDirect index query; higher latency
Ranking timeoutPopularity-only fallback
CDN stale bad suggestionShort TTL; emergency purge API
Index builder failureServe previous version; alert
DDoS on suggest APIRate limit; CDN absorb
Memory pressure on trieShard; compress; cap candidates

10. Performance Characteristics​

Phase 2: Estimate Scale​

500M suggestions/day ≈ 6K QPS average
Peak 10× → 60K QPS (plan 100K+ with headroom)
Prefix index lookup: 1–5 ms in-memory FST
Redis GET: 1–2 ms
Ranking blend: 2–5 ms
CDN hit ratio target: 40–60% for top prefixes
Componentp99 budget
CDN hit< 10 ms
API + Redis hit< 25 ms
Full miss path< 50 ms
Personalization< 10 ms (hard cap)

11. Scalability Limits​

LimitMitigation
Trie memoryFST compression; shard by locale
Short prefix fan-outMin length; cap candidates
Personalization QPSBatch features; local cache
Global index sizeRegional indexes
Log ingest volumeSample non-selected impressions

12. Operational Considerations​

Phase 9: Operations​

  • Metrics: QPS, p50/p99 latency, cache hit ratio, index lag, degradation rate.
  • Alerts: p99 > 100 ms; index version > 2h stale; blocklist sync failure.
  • Runbooks: emergency blocklist; CDN purge; rollback index version.
  • Dark launch new ranking weights with shadow traffic comparison.

13. Security Considerations​

Phase 8: Security​

  • Rate limit per IP and per session; prevent enumeration attacks.
  • Sanitize input; max prefix length 64 chars.
  • Do not log raw PII in query logs—hash user IDs.
  • Blocklist for offensive, regulated, and legally restricted terms.
  • Sponsored suggestions must be labeled; audit trail for compliance.

14. Cost Considerations​

CDN and Redis dominate at high QPS. Precompute top 10K prefixes per locale nightly into CDN cache.warm files. Index in memory is costly—FST compression reduces RAM 5–10× vs naive trie. Sample query logs at 10% for ranking if volume extreme. Managed OpenSearch completion vs self-hosted FST is ops vs cost tradeoff.

15. Production Implementations​

SystemNotes
Elasticsearch Completion SuggesterFST-based; common enterprise choice
Redis + custom trieLow latency; ops burden
Google SearchProprietary; multi-signal ranking
Amazon search suggestionsCatalog + behavioral blend
Algolia Query SuggestionsManaged SaaS autocomplete

Distinction: Public descriptions of ranking are implementation anecdotes; latency budgets and degradation paths are architectural requirements regardless of vendor.

16. Alternatives and Tradeoffs​

Phase 10: Tradeoffs​

ApproachProsCons
In-memory trieFastest readsMemory; rebuild cost
ES completionOps maturityHeavier; ms-level latency
DB LIKE prefix%SimpleDoes not scale past ~1K QPS
N-gram indexFuzzy matchLarger index; false positives
Client-only dictionaryZero latencyCannot update dynamically
Full personalizationBetter CTRLatency and privacy cost

17. Common Misconceptions​

MisconceptionReality
"Query DB with LIKE"Prefix scan does not scale
"One global ranking"Locale and category matter
"Always personalize"Must degrade on timeout
"Longer cache TTL always better"Stale trending and incidents
"Trie fits everything in RAM"Shard or use FST compression
"Autocomplete is stateless"Ranking needs logs and feedback loop

17A. Failure scenario drill​

Deploy disables CDN; 60K QPS hits origin; API and index overheat; site search box hangs. Mitigation: CDN is not optional at scale; origin autoscaling with queue depth limits; static fallback JSON per locale. Principal owns capacity planning for cache bypass events.

18. Principal Architect Perspective​

  • Degradation hierarchy: personalized → popularity → static curated → empty.
  • Min prefix length prevents expensive 1-char fan-out.
  • Blocklist propagation is incident-critical—test quarterly.
  • Analytics loop closes ranking quality; without it suggestions rot.
  • Privacy review for keystroke logging in regulated industries.
  • Sponsored slots need product/legal alignment separate from organic rank.

19. Architecture Review Exercise​

Scenario: Autocomplete hits PostgreSQL with WHERE term LIKE 'prefix%' at 20K QPS; p99 800 ms.

Review: Replace with FST/trie or ES completion; add Redis and CDN; move popularity to streaming aggregator. Load-test prefix "a" separately.

20. Whiteboard Explanation​

"Client debounces keystrokes and calls suggest API. CDN caches responses for hot prefixes. On miss, API fetches prefix matches from sharded FST index and popularity scores from Redis in parallel. Ranking blends popularity, optional personalization with 10 ms timeout, and freshness boosts. Top 8 returned and cached. Offline pipeline ingests query logs, updates popularity every few minutes, and rebuilds index nightly with hot delta reload. Blocklist pub/sub for emergencies. Personalization failure never blocks the read path."

21. Interview Questions​

  1. Design autocomplete for Amazon-scale search. — Signals: trie/FST, CDN, popularity ranking. Red flags: SQL LIKE.
  2. Trie vs Elasticsearch completion? — Signals: latency, ops, memory. Follow-up: when ES wins.
  3. How to rank suggestions? — Signals: popularity, CTR, personalization blend. Red flags: alphabetical only.
  4. Handle trending queries? — Signals: streaming counts, velocity boost, cache invalidation.
  5. Min characters before suggest? — Signals: fan-out cost vs UX. Follow-up: "a" prefix problem.
  6. Personalization without latency hit? — Signals: parallel fetch, timeout, degrade.
  7. Cache key design? — Signals: locale, category, prefix normalization.
  8. Prevent offensive suggestions? — Signals: blocklist, ingest review, fast purge.
  9. Shard prefix index? — Signals: locale/category; avoid cross-shard fan-out.
  10. Measure suggestion quality? — Signals: CTR, null-selection rate, A/B tests.
  11. Debounce on client or server? — Signals: client debounce; server rate limit.
  12. What if index is 1 hour stale? — Signals: hot reload path; popularity still fresh.
  13. Deduplicate concurrent requests? — Signals: singleflight per prefix.
  14. Spell correction in autocomplete? — Signals: separate fuzzy layer; latency budget.

22. Interview Follow-Ups​

  1. Multi-language prefixes. — Unicode normalization; per-locale indexes.
  2. Sponsored suggestions. — Separate auction slot; disclosure; do not break latency.
  3. Federated autocomplete across entities. — Parallel queries; merge ranks.

22A. Production Low-Level Design (Interview + Game Day)​

Autocomplete LLD: prefix index + ranking at the edge; offline trie/build in control plane.

Context diagram​

Step-by-step flow:

StepAction
1–2Ultra-low latency suggest
3Periodic rebuild from logs + catalog

Production stack​

LayerChoiceRole
Edge cacheCDN / RedisHot prefixes
IndexTrie / n-gram / FSTPrefix→candidates
RankML + rulesPersonalization light
OfflineSpark / FlinkTrend updates
AbuseRate limitTypeahead floods

API + schema​

GET /v1/suggest?q=pre&limit=10&locale=en
→ { suggestions: [{ text, score, type }] }
StructureNotes
Prefix keyNormalized lowercase
PostingTop-K docs per prefix
PersonalizationRe-rank top-K only (latency)

Happy-path sequence​

Observability + SLO​

SLIIdea
Suggest p99< 50 ms
Empty rateQuality
Cache hitCost/latency
Stale index ageFreshness

22B. Game Day Playbooks​

Catalog​

IDDrillInjectSignal
GD-1Trending prefixCelebrity eventHot key
GD-2Bad deploy indexEmpty trieEmpty rate
GD-3Personalization slowML timeoutFallback static
GD-4Typo floodRandom qRate limit
GD-5Locale skewWrong dictQuality
GD-6Build lagPipeline failStaleness alert

GD-3 — Personalization timeout (detailed)​

Pass criteria: never block typeahead on ML; degrade rank quality only.

23. Strong Answer Example​

Question: "How do you handle a sudden trending prefix?"

Answer this as a 45-minute design discussion using the PRACTICE framework. Below is how a strong student sounds aloud—edge cache + precomputed top-K + shed personalization, not bigger DB.

Figure A: PRACTICE map — walk the board in this order.


Student interview script (PRACTICE)​

P — Problem framing​

"A viral event makes one prefix (olym, celebrity name) get 100× QPS. The risk is hot keys, ranking fan-out, and p99 blowups. Treat typeahead as a latency SLO product: cache aggressively, serve precomputed top suggestions, and disable expensive personalization under load."

R — Requirements​

TypeRequirement
FRRelevant suggestions still appear
NFRHold p99 under spike
OpsDetect trend; pre-warm

A — Assumptions​

AssumptionWhy
Read-heavyCache works
Offline build existsCan pin top lists
Personalization optionalShed first

C — Capacity​

Hot prefix: nearly 100% cacheable
Edge TTL short enough for freshness (10–60s)
Limit = 10 results fixed payload

T — Topology​

I — Invariants​

  1. Suggest never waits on heavy ML under shed.
  2. Results bounded (limit).
  3. Abuse rate-limited per IP/user.

C — Constraints​

ConstraintResponse
Freshness vs cacheShort TTL + event pre-warm
PersonalizationAsync or top-K rerank only
Multi-localeSeparate cached keys

E — Evolution​

Static trie → streaming trend updater → event playbooks for launches.


Closing line (interview)​

"On a trending prefix: edge-cache the query, serve a precomputed top-K, shed personalization, and pre-warm for known events so p99 stays within typeahead SLO."

Scoring signals​

StrongWeak
Cache + shed + pin"Scale SQL LIKE"
Latency SLO firstPerfect rank under spike
Pre-warm eventsNo ops story
Bounded payloadUnbounded result sets

Compact outline (if time-boxed to 2 minutes)​

"On a trending prefix: edge-cache the query, serve a precomputed top-K, shed personalization, and pre-warm for known events so p99 stays within typeahead SLO."

24. Weak Answer Example​

Weak: "Store all searches in MySQL and SELECT WHERE term LIKE prefix%."

Red flags: No indexing structure, no cache, no ranking, no scale math, no degradation.

25. Hands-On Exercise​

  1. Build compressed trie with top-K retrieval for 100K terms.
  2. Add Redis cache layer; measure hit ratio on Zipf prefix distribution.
  3. Simulate personalization timeout; verify fallback ranking.
  4. Implement singleflight for identical concurrent prefixes.
  5. Extension: Stream aggregator updating popularity every 60s.
  6. Extension: A/B test two ranking weights; compare CTR.

26. Knowledge Check (extended)​

  1. Why FST over naive trie?
  2. What three signals blend in ranking?
  3. Why cap candidate set before top-K?
  4. How blocklist propagates in < 60s?
  5. CDN hit ratio target and why not 99%?
  6. Debounce value tradeoff?

27. Flashcards​

FrontBack
FSTFinite state transducer; compressed prefix index
Typeahead debounceClient delay reducing request spam
Popularity ZSETRedis sorted set for ranked terms
Min prefix lengthLimits fan-out for ambiguous short queries
SingleflightCoalesce duplicate in-flight suggest requests
Degrade pathpopularity-only when personalization times out
CTRClicks / impressions for suggestion quality
Hot reloadDelta index update without full rebuild
Blocklist pub/subFast offensive term removal
Edge n-gramAlternative indexing for fuzzy prefix
Request coalescingSame as singleflight at API layer
Static fallbackCurated JSON when index unavailable

28. Cheat Sheet​

REQUIREMENTS: top-K prefix, <50ms p99, locale, analytics
SCALE: CDN + Redis + sharded FST; 100K+ QPS peak
APIs: GET /suggest; POST /events; blocklist admin
DATA: FST/trie shards; popularity ZSET; query log Kafka
ARCH: debounce → CDN → API → index + rank parallel
DEEP: ranking blend; personalization timeout; trending boost
RELIABILITY: replica index; static fallback; circuit breaker
SECURITY: rate limit; blocklist; PII hashing in logs
OPS: cache hit ratio; index version; shadow ranking tests
TRADEOFFS: trie vs ES; personalization vs latency

28A. Principal Interview Deep Dive​

Latency budget table​

StageBudget
Network client→CDN5–15 ms
CDN hit5 ms total
API gateway2 ms
Index lookup5 ms
Popularity fetch3 ms
Rank + serialize5 ms
Personalization (optional)10 ms max

When NOT to personalize​

  • Guest users without identity.
  • Latency SLO already at risk.
  • Regulated contexts prohibiting behavioral profiling.
  • Cold-start users with no signal.

Prefix length policy​

LengthBehavior
0–1Empty or curated defaults only
2Top global suggestions; heavy cache
3+Full prefix index query

28B. Extended BOE Walkthrough (Interview Script)​

Interviewer: "Design autocomplete for Amazon search—500M suggestions per day."

Strong candidate:

"500M/day ≈ 6K QPS average; peak 10× → 60K QPS. Each request returns top-8 from prefix index—assume 200 byte response → ~10 MB/sec egress, modest.

I'll shard prefix index by locale and first character. Hot prefixes ('lap', 'iph') cached at CDN with 60–120s TTL; origin Redis for miss. Ranking blends popularity ZSET updated every 2 min from Kafka query logs, optional personalization with 10 ms timeout.

Personalization failure → popularity-only—never block suggest path. Min prefix length 2 chars to limit fan-out. Blocklist pub/sub for offensive terms under 60s.

Scale math: if 50M unique prefixes active, FST compressed ~5–10 bytes per term edge—fits RAM on modest cluster; QPS not memory-bound at this scale. Stampede on cache expiry handled by singleflight per prefix key at API.

Failure: index down → static curated JSON per locale; metrics on degradation rate. Link to Distributed Cache Design for cache-aside patterns on popularity store."

30. References​

  • Bast and Hirst — query completion surveys (academic).
  • Elasticsearch completion suggester — official documentation (implementation).
  • Kleppmann, DDIA — stream processing for analytics.

Distinction: FST theory is well-established; production ranking blends are implementation-specific and require A/B validation.

30A. Further Reading Paths​

Deepen with Distributed Caching for popularity store design. Compare prefix search requirements to News Feed ranking—both blend popularity with personalization but feed tolerates 100ms+ while autocomplete requires <50ms. Lab: measure cache hit ratio on Zipf prefix distribution; plot tail latency with and without CDN.

30B. Interview Scoring Rubric (Principal)​

DimensionStrong (4–5)Weak (1–2)
Scale estimationQPS, cache tiers, index RAMHand-wavy "use cache"
RankingMulti-signal blend + degradeAlphabetical sort
Failure modesBlocklist, static fallback"Index always up"
TradeoffsFST vs ES with criteriaSingle solution
OperationsCache purge, trending pipelineNo ops mention