System Design Case Study: Notification System
A notification system can alert user with important information like breaking news offers.
step1: understand the problem and establish design scope
From abstraction level, it only has 3 components: sender, system core, receiver. While in real world notification system can be a complicated system, to figure out which part should be focused is very important.
Say, we want to focus on the system core.
step2: propose high-level design
- senders send the request of sending notification to system core
- load balancer will be the entry point of system, it will balance the load of API server where we handle the real logic
- API server send the query to DB to find the users who are qualified to receive the notification
- API server get receivers ID
- Send the notification to those receivers.
We figured out the basic flow of system, while some part need more clarification
for example,
what’s the API design for sender,
what’s the schema design of user table
{
"to":[
{
"user_id":123456
}
],
"from":{
"email":"from_address@example.com"
},
"subject":"Hello World!",
"content":[
{
"type":"text/plain",
"value":"Hello, World!"
}
]
}
step3: deep dive design
So far we complete the high level design of system, interviewer will ask us some problem in depth to test our mastery of knowledge.
Here are some examples
Reliability
How can we make sure the notification has sent to every qualified receiver?
Notifications can be delayed or re-ordered, while it cannot be lost.
When it comes to “preventing data loss”, Database should be the first thing come into our mind.
So the solution is, before we send the notification to receiver, first we should store them into DB. Some message queue system also has DB, so we can also introduce message queue to send the notification.
duplicated notification
it’s hard to de-dup the event in distributed system, while sending multiple similar notification to user is a very bad user experience.
One solution is we attach event ID for each notification, then on client side we check if this device receive the event ID before, if it’s received, then we don’t trigger any notification.
optimize performance
there are several potential bottlenecks of system,
- fetch the qualified receivers
- send the notification to receiver
second point is restricted by third party, while for 1st point, we can introduce cache level to increase the efficiency.