Design a Search Autocomplete System
1. Problem Statement & Scope Clarification
System Mission
Design a real-time, globally distributed search autocomplete (typeahead) system (similar to Google Search and Amazon product typeahead) capable of returning the Top-5 most relevant, high-frequency query completions as a user types into a search box, achieving sub-10 millisecond latency at massive planetary scale.
Functional Requirements
- Real-Time Prefix Completion: Given a search prefix (e.g.,
"sys"), return the Top-5 highest-ranked completed search phrases within 10ms. - Dynamic Relevance & Frequency Ranking: Rank suggestions based on historical query frequency, recency, location, and user personalization.
- Trending & Breaking News Ingestion: Surface sudden viral search spikes within 5 minutes without waiting for nightly batch re-indexes.
- Content Filtering & Safety: Filter out profanity, hate speech, and sensitive personal information using a high-speed blacklist filter.
- Multi-Lingual & Unicode Support: Support accent-insensitive, case-insensitive, and multi-byte UTF-8 string prefix matching.
Non-Functional Requirements (SLAs & SLOs)
- Ultra-Low Latency: , globally. Autocomplete must render before the user types the next character ( human typing speed).
- High Availability: uptime SLA.
- Scalability: Support at peak.
- Data Freshness: Batch models refreshed daily; trending query velocity merged within 5 minutes.
2. Capacity & Scale Estimation (Back-of-the-Envelope Math)
Ingest & Query Volume
- Daily Searches: (1 Billion searches/day).
- Average Keystrokes per Search: 4 autocomplete queries per completed search.
- Total Daily Autocomplete Requests:
- Average QPS:
- Peak QPS ( peak multiplier):
Trie Memory Footprint Estimation
- Total Unique Search Queries Retained: ().
- Average Query Length: 20 characters ().
- Optimized Radix Tree / FST (Finite State Transducer) Node Layout:
- Precomputing Top-5 suggestions at every node eliminates tree traversal during runtime.
- 5 suggestions 20 bytes per node.
- Pointer and Trie structural overhead .
- Total per prefix entry .
- Raw Trie Memory Footprint:
- Multiplexing across an Amazon ElastiCache Redis Cluster with replication requires only , fitting comfortably on a modest multi-node cluster.
3. Dual-Path Architecture & AWS Component Mapping
The architecture splits into an Ultra-Low Latency Online Query Path and an Asynchronous Big Data Analytics Pipeline.
Interactive Architecture DiagramSynthesizing vector architecture diagram...
4. API Interface Design & Wire Protocol
Typeahead Query REST/HTTP API
httpGET /v1/search/autocomplete?q=sys&limit=5&country=US&lang=en Host: autocomplete.production.aws.internal Accept: application/json Response: 200 OK Cache-Control: public, max-age=60, stale-while-revalidate=30 Content-Type: application/json { "prefix": "sys", "suggestions": [ { "query": "system design interview", "score": 98500, "type": "HISTORICAL_POPULAR" }, { "query": "system design roadmap", "score": 84200, "type": "HISTORICAL_POPULAR" }, { "query": "system down outage today", "score": 79100, "type": "REALTIME_TRENDING" }, { "query": "system 32 error fix", "score": 64300, "type": "HISTORICAL_POPULAR" }, { "query": "system architecture patterns", "score": 52100, "type": "HISTORICAL_POPULAR" } ], "server_timing_ms": 1.42 }
5. Storage Engine & Trie Serialization Schema
1. Radix Trie Node Structure with Precomputed Top-
Storing the Top- (Top 5) completions directly on every intermediate prefix node converts the query time complexity from down to a pure memory lookup:
textTrie Root βββ "s" [Top-5: "spotify", "slack", "system design", "steam", "salesforce"] β βββ "y" [Top-5: "system design", "system down", "synonym", "syntax", "sync"] β βββ "s" [Top-5: "system design", "system down", "sysco", "sysadmin", "system32"]
2. Redis Key Layout & Binary Serialization Format
To optimize memory cache lines, precomputed suggestions are serialized using FlatBuffers and stored under compressed Redis keys:
- Redis Key:
trie:en:us:<prefix_sha1_or_string>(e.g.trie:en:us:sys) - Redis Value: Binary FlatBuffer byte array containing 5 compressed query ID pointers and integer relevance scores.
- TTL: 86,400 seconds (Refreshed daily via snapshot deployment).
Unlock Complete Architecture & Production Runbooks
You have explored the free architectural preview (~43%). Spend 1 Coin to unlock the remaining 6 production deep-dive sections for a full 24 hours.