Design Typeahead Suggestion

Problem Statement:


Step-1: What is Typeahead Suggestion ?


Step-2: Requirements and Goals of the System

Step-3: Basic System Design and Algorithm


Note:- We can merge nodes that have only one branch to save storage space. The above trie can be stored like this:

Should we’ve case insensitive trie ?

For simplicity and search use case let’s assume our data is case insensitive.

How to find top suggestion ?
Given a prefix, how much time it can take to traverse its sub-tree ?
Can we store top suggestions with each node ?
How would we build this trie ?
How to update the trie ?
How can we update the frequencies of typeahead suggestions ?
How can we remove a term from the trie ?
What could be different ranking criteria for suggestions ?


Step-4: Permanent Storage of the Trie

How to store trie in a file so that we can rebuild our trie easily - this will be needed when a machine restarts ?

Step-5: Scale Estimation

Traffic Estimation:
Storage Estimation:


Step-6: Data Partition

How can we efficiently partition our data to distribute it onto multiple servers ?

a) Range Based Partitioning:


b) Partition Based on the Maximum Capacity of the Server:


c) Partition based on the hash of the term:


Step-7: Cache


Step-8: Replication and Load Balancer


Step-9: Fault Tolerance

What will happen when a trie server goes down ?


Step-10: Typeahead Client

We can perform the following optimizations on the client to improve user’s experience:

  1. The client should only try hitting the server if the user has not pressed any key for 50ms.
  2. If the user is constantly typing, the client can cancel the in-progress requests.
  3. Initially, the client can wait until the user enters a couple of characters.
  4. Clients can pre-fetch some data from the server to save future requests.
  5. Clients can store the recent history of suggestions locally as recent history has a very high rate of being reused.
  6. Establishing an early connection with server turns out to be one of the most important factors. As soon as the user opens the search engine website, the client can open a connection with the server. So when user types in the first character, client doesn’t waste time in establishing the connection.
  7. The server can push some part of their cache to CDNs and Internet Service Providers (ISPs) for efficiency.


Step-11: Personalization




← Previous: Design Youtube / Netflix

Next: Design API Rate Limiter →