Notes of the book System Desgin Interview - An Insider's Guide
Add Search Autocomplete Chapter
Gaurav Kumar6dcec0cf157a
diff --git a/search-autocomplete/Readme.md b/search-autocomplete/Readme.mdnew file mode 100644index 0000000..2cc83c6--- /dev/null+++ b/search-autocomplete/Readme.md@@ -0,0 +1,165 @@+# Chapter 13: Design a Search Autocomplete System++## Introduction+Autocomplete, also known as typeahead or incremental search, provides real-time suggestions to users as they type in search boxes. The system must efficiently deliver top-k relevant and popular suggestions based on historical query data.++### Key Features+- Suggest up to **5 autocomplete results**.+- Based on **query popularity** (frequency).+- Support only **lowercase English characters**.+- Fast response time (<100 ms) and scalable.++---++## Step 1: Understanding the Problem++### Requirements+1. **Real-Time Suggestions:** Display relevant matches as the user types.+2. **Top-k Results:** Return up to 5 results sorted by popularity.+3. **Scalability:** Handle **10 million DAU** with a peak QPS of **48,000**.+4. **High Availability:** Handle failures without system downtime.+5. **Data Growth:** Support daily storage growth of **0.4 GB** for new query data.++---++## Step 2: High-Level Design+At the high-level, the system is broken down into two services:+1. **Data Gathering Service:**+ - Collects user queries and aggregates them for frequency analysis in real-time.+ - Real-time processing is not practical for large data sets; however, it is a good starting point+++2. **Query Service:** Provides the top-k suggestions based on the user’s input.++---++### Data Gathering Service+- Aggregates query data from analytics logs and updates the frequency table.+- Processes historical data weekly to build a **trie** (prefix tree).++ <img src="./images/data-gathering.png" alt="Data Gathering" width="400">++++### Query Service+- Uses the frequency table from data gathering service.+- Processes user input and retrieves top-k suggestions from the frequency table using a Trie.+- Optimized for fast lookups using caching and efficient data structures.+- For example when a user types “tw” in the search box, the following top 5 searched queries are displayed.++ <img src="./images/frequency-table.png" alt="Frequency Table" width="300">+ <img src="./images/basic-search-suggestions.png" alt="Search Suggestions" width="273">++---++## Step 3: Design Deep Dive++### Trie Data Structure+The **trie** is a tree-like data structure used to store and retrieve query strings efficiently.++#### Key Features+1. **Compact Storage:** Represents prefixes hierarchically to minimize redundancy.+2. **Frequency Information:** Stores the popularity of queries at each node.++4. **Steps to get top k most searched queries**+ - Find the prefix+ - Traverse the subtree from prefix node to get all valid children+ - Sort the children and get top k++ <img src="./images/trie-structure.png" alt="Trie Structure" width="400">++3. **Optimizations:**+ - Cache top-k queries at each node to speed up retrieval and avoid traversing the whole trie.++ <img src="./images/cached-trie.png" alt="Cached Trie" width="400">++ - Limit prefix length to reduce search space as users rarely type a loong search query (say 50).++#### Trie Operations+1. **Create:**+ - Built weekly using aggregated query data.+ - The source of data is from Analytics Log/DB.+2. **Update:** Rarely updated in real-time; weekly updates replace old data.+3. **Delete:**+ - Filters remove unwanted or harmful suggestions (e.g., hate speech).+ - Having a filter layer gives us the flexibility of removing results based on different filter rules.+ - Unwanted suggestions are removed physically from the database asynchronically.++ <img src="./images/delete-kv.png" alt="Delete KV" width="300">++---++### Query Processing Flow+1. **Prefix Search:**+ - Identify the prefix node corresponding to the user’s input.+ - Traverse the subtree to collect valid suggestions.+2. **Top-k Sorting:**+ - Cache top-k suggestions at each node to minimize sorting overhead.+3. **Response Construction:**+ - Construct results using cached data for fast response times.++---++### Optimizations+1. **Cache at Each Node:**+ - Store the top-k queries to avoid redundant traversals.+2. **Limit Prefix Length:**+ - Cap prefix length to a small value (e.g., 50 characters) for faster lookups.+3. **AJAX Requests:**+ - Use lightweight asynchronous requests for real-time responses.+4. **Browser Caching:**+ - Save autocomplete results in the browser cache for frequently searched terms.++---++### Data Gathering Pipeline+In the high-level design, whenever a user types a search query, data is updated in real-time. This appraoch is not practical.+- Users may enter billions of queries per day. Updating the trie on every query is not feasible.+- Top suggestions may not change much one the trie is built.+++#### Updated Design++<img src="./images/data-gathering-flow.png" alt="Updated Data Gathering Flow" width="500">++1. **Analytics Logs:**+ - Stores raw query data as logs for weekly aggregation.+ - Logs are append-only and are not indexed+2. **Aggregators:**+ - Process logs into frequency tables, suitable for trie construction.+ - For real-time applications such as Twitter, aggregate data in a shorter time interval.+ - For other cases, aggregating data less frequently, say once per week is good enough.+3. **Workers:**+ - Asynchronous servers rebuild the trie and store it in persistent storage.+4. **Storage Options:**+ - **Trie Cache**: Trie Cache is a distributed cache system that keeps trie in memory for fast read.+ - **Trie DB**+ 1. **Document Store (e.g., MongoDB)**: Since a new trie is built weekly, we can periodically take a snapshot of it, serialize it, and store the serialized data in the database like MongoDB+ 2. **Key-Value Store:**+ - Maps prefixes to node data for fast access.+ - Every prefix in the trie is mapped to a key in a hash table.+ - Data on each trie node is mapped to a value in a hash table.++ <img src="./images/trie-db.png" alt="Trie DB" width="400">+---++### Scalability+1. **Sharding:**+ - Distribute trie nodes across servers based on prefix ranges (e.g., `a-m`, `n-z`).+ - Further shard within prefixes to balance uneven distributions (e.g., `aa-ag`, `ah-an`).+2. **Load Balancing:**+ - Use a shard map manager to route requests to the appropriate server.++ <img src="./images/sharding.png" alt="Sharding" width="300">++---++## Step 4: Advanced Features++### Multi-Language Support+1. **Unicode Characters:** Use Unicode to support non-English languages.+2. **Country-Specific Tries:** Build separate tries for different countries or regions.++### Trending Queries+- Handle real-time events by dynamically updating trie nodes or weighting recent queries more heavily.+diff --git a/search-autocomplete/images/basic-search-suggestions.png b/search-autocomplete/images/basic-search-suggestions.pngnew file mode 100644index 0000000..d352542Binary files /dev/null and b/search-autocomplete/images/basic-search-suggestions.png differdiff --git a/search-autocomplete/images/cached-trie.png b/search-autocomplete/images/cached-trie.pngnew file mode 100644index 0000000..02727d4Binary files /dev/null and b/search-autocomplete/images/cached-trie.png differdiff --git a/search-autocomplete/images/data-gathering-flow.png b/search-autocomplete/images/data-gathering-flow.pngnew file mode 100644index 0000000..cdd9791Binary files /dev/null and b/search-autocomplete/images/data-gathering-flow.png differdiff --git a/search-autocomplete/images/data-gathering.png b/search-autocomplete/images/data-gathering.pngnew file mode 100644index 0000000..f0353baBinary files /dev/null and b/search-autocomplete/images/data-gathering.png differdiff --git a/search-autocomplete/images/delete-kv.png b/search-autocomplete/images/delete-kv.pngnew file mode 100644index 0000000..7bf93a9Binary files /dev/null and b/search-autocomplete/images/delete-kv.png differdiff --git a/search-autocomplete/images/frequency-table.png b/search-autocomplete/images/frequency-table.pngnew file mode 100644index 0000000..8acb595Binary files /dev/null and b/search-autocomplete/images/frequency-table.png differdiff --git a/search-autocomplete/images/sharding.png b/search-autocomplete/images/sharding.pngnew file mode 100644index 0000000..ffa0cf7Binary files /dev/null and b/search-autocomplete/images/sharding.png differdiff --git a/search-autocomplete/images/trie-db.png b/search-autocomplete/images/trie-db.pngnew file mode 100644index 0000000..445d72fBinary files /dev/null and b/search-autocomplete/images/trie-db.png differdiff --git a/search-autocomplete/images/trie-structure.png b/search-autocomplete/images/trie-structure.pngnew file mode 100644index 0000000..7f39124Binary files /dev/null and b/search-autocomplete/images/trie-structure.png differ