List non-functional requirements for the system...
Estimate the scale of the system you are going to design...
assume:
QPS: (2 ^ 20 * 10 ) * (10 * 20) / (60*60*24) = 24000
PQPS: 2 * QPS = 48000
Define what APIs are expected from the system...
GET /api/search
request:
{
query: 'do' // search query
}
response:
{
data: ['do', 'dog', 'dollar', 'doll', 'dong' ] // 5 top most relevant words
error: '', // error message
status: 0 // API status code
}
Defining the system data model early on will clarify how data will flow among different components of the system. Also you could draw an ER diagram using the diagramming tool to enhance your design...
see diagram
2 main services:
data aggregate service
steps:
query service
given search query, get the most relevant terms from Trie cache , and form the result and return results in JSON
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
Trie data structure
(root)
/ \
d
/ \
do da
/ \
dog don
/ \
doge dont
for each node, store below info, e.g. do
{
freq: 100,
top_freq_words: [[dog: 1000], [dont: 2000], ...]
}
each Trie node maps to K:V pair in Trie DB
k: 'do', v: [[dog: 1000], [dont: 2000]...]
Trie operations
create
got data from logs, and created by workers
update
update prefix frequency
2 options.
option 1: update trie weekly
option 2: directly update Trie data structure. if Tire is small, it is okay, but if it is large, will be slow, because all ancestor nodes need to be updated
so we choose option 1.
delete
we dont want to display hateful suggestions, add a filter service between query service and Trie cache. we can flexibly filter words based different filter rules
Explain any trade offs you have made and why you made certain tech choices...
current design doesn't support treading search queries, like breaking news. because workers are offline jobs that update Trie weekly. to support treading search queries, need to do more frequent trie update, like hourly
Try to discuss as many failure scenarios/bottlenecks as possible.
if internet is not available, client side store cache as well
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?