architect-handbook

Software Architect Handbook

View on GitHub

Design a Notification System

Overview

A notification alerts a user with important information like breaking news, product updates, events, offering, etc.

A notification is more than just mobile push notification. Three types of notification formats are: mobile push notification, SMS message, and Email.

Step 1: Understand the problem and establish design scope

The interview question is purposely designed to be open-ended and ambiguous, and it is your responsability to ask questions to clarify the requirements.

Candidate: What types of notification does the system support? Interviewer: Push notification, SMS message, and email.

Candidate: Is it a real-time system? Interviewer: Let us say it is a soft real-time system. We want a user to receive notifications as soon as possible. However, if the system is under a high workload, a slight delay is acceptable.

Candidate: What are the supported devices? Interviewer: iOS devices, android devices, and laptop/desktop.

Candidate: What triggers notifications? Interviewer: Notifications can be triggered by client applications. They can also be scheduled on the server-side.

Candidate: will users be able to opt-out? Interivewer: Yes, users who choose to opt-out will no longer receive notifications.

Candidate: How many notificatiuons are sent out each day? Interviewer: 10 million mobile push notifications, 1 million SMS messages, and 5 million emails.

Step 2: Propose high-level design and get buy-in

Different types of notifications

iOS Push Notification

We primary need three components to send an iOS push notification:

Android Push Notification

Android adopts a similar notification flow. Instead of using APNs, Firebase Cloud Messaging (FCM) is commonly used to send push notifications to android devices.

SMS Message

For SMS messages, third party SMS services like Twilio, Nexmo, and many others are commonly used.

Email

Although companies can set up their own email servers, many of them opt for commercial email services such as Sendgrid and Mailchimp.

Contact info gathering flow

To send notifications, we need to gather mobile device tokens, phone numbers, or email addresses. When a ser installs our app or signs up for the first time, API servers collect user contact info and store it in the database.

These are implified database tables to store contact info. Email addresses and phone numbers are stored in the user table, whereas device tokens are stored in the device table. A user can have multiple devices, indicating that a push notification can be sent to all the user devices.

Notification sending/receiving flow

High-level design

Three problems are identified in this design:

High-level design (improved)

Sending/receiving flow

Example POST https://api.example.com/v/sms/send

  1. A service calls APIs provided by notification servers to send notifications.
  2. Notification servers fetch metadata such as user info, device token, and notification setting from the cache or database.
  3. A notification event is sent to the corresponding queue for processing. For instance, an iOS push notification event is sent to the iOS PN queue.
  4. Workers pull notification events from message queues.
  5. Workers send notifications to third party services.
  6. Third-party services send notifications to user devices.

Step 3: Design deep dive

We will explore the following in deep dive:

Reliability

A simple dedupe logic: When a notification event first arrives, we check if it is seen before by checking the event ID. If it is seen before, it is discarded. Otherwise, we will send out the notification.

Additional components and considerations

Notification template

A large notification system sends out millions of notifications per day, and many of these notifications folow a similar format. Notification templates are introduced to avioid building every notification from scratch by using a preformatted notification to create unique notification by customizing parameters, styling, tracking limits, etc.

The benefits include:

Notification setting

You may want to provide users fine-grained control over notification settings. This information can be stored for example in the notification setting table with the following fields:

Before any notification is sent to a user, we first check if a user is opted-in to receive this type of notification.

Rate limiting

To avoid overwhelming users with too many notifications, we can limit the number of notifications a user can receive. This can help to avoid users turning off notifications completely.

Retry mechanism

When a third-party service fails to send a notification, the notification will be added to the mssage queue for retrying. If the proble mpersists, an alert will be sent out to developers.

Security in push notifications

For iOS or Android apps, appKey and appSecret are used to secure push notification APIs. Only authenticated or verified clients are allowed to send push notifications using our APIs.

Monitor queued notifications

A key metric to monitor is the total number of queued notifications. If the number is large, the notification events are not processed fast enough by workers. To avoid delay in the notification delivery, more workers are needed.

Events tracking

Notification metrics, such as open rate, click rate, and engagement are important in understanding customer behaviors. Analytics service implements events tracking. Integration between the notification system and the analytics service is usually required.

Updated design

Many new components are added:

Step 4: Wrap up

We described the design of a scalable notification system that supports multiple notification formats: push notification, SMS, and email. We adopted message queues to decouple system components.

Besides the high-level design, we dug deep into more components and optimizations: