AlgoViz

Design a notification service

medium 45 min

One service that every team calls to reach a user by push, email, SMS or inside the app.

Video lesson · Chapter 1 of 5

An order ships and the phone should know

0:00 / 3:40

Theory

Read or listen first. About 2 min.

What it is

A notification service takes a request such as "tell this user that the order has shipped" and gets the message to the user: as a push message on a phone, an email, an SMS, or a message inside the app.

It sits between many producers (orders, sign-in, payments, marketing) and several outside providers. It does not own the last step. Apple, Google, email hosts and phone carriers do, so the design is mostly about waiting lines, rules and failures.

Why it exists

Without it, every team writes its own provider code, its own retry loop and its own opt-out list. One team forgets the opt-out list, and the company breaks a promise or a law.

With it, a producer says what happened and who should know. One place decides the channel, the words, the time of day and what to do when a provider is down.

A short history

  1. 1992

    The first SMS is sent on 3 December and reads "Merry Christmas".

  2. 2003

    The United States passes the CAN-SPAM Act. Opt-out requests for commercial email must be honoured within 10 business days.

  3. 2009

    Apple launches the Apple Push Notification service (APNs) with iPhone OS 3 on 17 June.

  4. 2016

    Google replaces Google Cloud Messaging with Firebase Cloud Messaging (FCM). RFC 8030 standardises web push in December.

  5. 2017

    RFC 8058 defines one-click unsubscribe for email with an HTTPS POST.

Words to know

Channel
A way to reach a person: push, email, SMS, in-app.
Provider
The outside company that delivers on a channel, such as APNs or an email service.
Device token
The address of one app on one device. It can stop working at any time.
Priority lane
A separate waiting line, with its own workers, for one level of urgency.
Quiet hours
The hours in which a user does not want to be disturbed, in the user's own time zone.
Idempotency key
A name for a request, so that a repeat is recognised.
Dead letter queue
Where messages go after the last attempt, for a person to look at.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 4

Accept. A producer posts a request and gets 202 with an id

Understand it

The village bell ringer
One bell for everyone

The ringer knows the families, the hour and how serious the news is. That is the whole service.

Scope

What are we building?

A notification service carries timely messages to users over several channels. You meet one when a ride is on its way, a parcel has left, or a bank sends a one-time code.

  • Critical

    "Your code is 000000, valid for 5 minutes." Security alerts and password resets.

  • High

    "Your order 12345 has been shipped." Receipts and confirmations.

  • Normal

    "Sam liked your photo." Likes, comments, follows.

  • Low

    "50% off this weekend." Promotions, newsletters, digests.

Channels

ChannelReachesThroughWatch out for
PushPhones, browsersAPNs, FCM, Web PushTokens die; 4 KB payload limit at APNs
EmailInboxAn email serviceReputation, bounces, unsubscribe
SMSAny phoneA carrier gatewayPrice per message, slow rate per number
In-appOpen appOur WebSocket serversNothing arrives if the app is closed
WebhookAnother systemHTTPS callThe receiver may be down

Similar products

ProductKnown for
Firebase Cloud MessagingGoogle's push service
Amazon SNSPublish and subscribe, push and SMS
OneSignalPush for many platforms
TwilioSMS, voice and messaging APIs
SendGridEmail delivery
BrazeCustomer engagement campaigns
We design a service like the ones companies build on top of these, not any company's real system.

The hard part

Urgent and bulk share the same road

Averages are easy here. The test is the moment when ten million adverts are waiting and one person is looking at a sign-in screen. That code must overtake all of them, in the API, in the queue, in the workers and in the provider's quota.

The finished system

Where the design ends up

This is the last version. The Design tab starts with one service and one database.

The whole system

Producers call the notification API, which reads preferences and templates (Redis in front of PostgreSQL) and publishes to Kafka topics split by channel and priority. Reserved workers read the critical topics, shared workers read the rest, and both call the outside providers: APNs, FCM, email and SMS. Failed sends go to a retry topic with dead letters. Provider webhooks come back through a receiver into Cassandra and ClickHouse. A campaign service adds paced batches to the low topics.
Blue is a request, pink is delivery, orange is failure and feedback. Dashed means asynchronous or only sometimes. Each worker also claims a send in Redis first, which is not drawn.

Where to read more

Ideas this problem borrows

IdeaIts homeUsed here for
Waiting linesQueues, log-based queuesLanes by channel and priority
One event, many readersPublish and subscribe, fan-outCampaigns, status events
Safe repeatsIdempotency, retriesClaims before sends
Outside failuresCircuit breaker, fallbacksBackup providers
Speed limitsRate limiterProvider quotas, user caps
Live messagesReal-time APIs, webhooksIn-app and feedback
Later, at 9 in the morningTask schedulerQuiet hours, scheduled sends

Summary

The whole topic in one minute.

A notification service accepts quickly, then delivers slowly. The API checks the request, writes it to a durable queue and answers 202. Workers do the rest, one pool for each channel, so a slow email provider cannot hold up push.

Urgency gets its own lane all the way to the provider. Preferences are checked twice. Each send is claimed before it is made, so retries rarely become duplicates. What the providers report comes back as events, and it is reported honestly: accepted is not the same as seen.

  • Answer 202 only after the request is safely in a replicated log.
  • A lane for each channel and priority, with workers reserved for the critical ones.
  • Check preferences when the message is accepted and again just before it is sent.
  • Claim a send before making it. Duplicates become rare, never impossible.
  • A message has a deadline. Retrying a five-minute code for hours is worse than failing.
  • Accepted by the provider is not delivered, and delivered is not read.

Sources