Notification System: Scope
Sixty million notifications a day sits inside every limit you are about to hear, which is why the daily number is the wrong place to stop. The first ten minutes find the two numbers that do bite: how large one campaign is, and how long the slowest company in the chain takes to answer. The API comes last, and its main call changes its answer before the round is over.
Two words first. A provider is the outside company that delivers on one channel: a push service for phones, an email sender, a text-message gateway. A campaign is one marketing send to a chosen audience, as against the steady messages other services ask for one at a time.
Questions That Move the Board
Five questions change what you draw. Each comes with what its answer moves and what this lesson agrees.
- What kinds of message go through this, and does any of them have to arrive within seconds? The answer sets the priority classes and, later, who gets shed. Agreed: sign-in codes and security alerts within seconds, the critical class; order and account messages within a minute, the transactional class; marketing within hours.
- How many a day on each channel, and how large is one campaign? The answer separates the steady rate from the burst, and the burst is what shapes the queues. Agreed: 30 million push, 8 million email and 2 million text a day from the steady services, and about three campaigns a week of up to 50 million each.
- Can a notification ever arrive twice? The answer gets written on the arrow in First Design, and Chapter 6's Delivery Semantics at Scale asks it in these words. Agreed: yes, at-least-once, because a lost sign-in code locks someone out. A marketing text is the exception, and it stays parked until the deep dive.
- Does the caller need the outcome, or only to know the request was taken? A need for the outcome keeps the caller on the line, and "taken" allows a queue. Agreed for now: the caller waits. Callers name only a user ID, and this system holds the tokens and addresses.
- Do users choose what they receive, and how soon must a mute hold? The answer places the preference check and bounds how stale it may be. Agreed: choices per category and channel, and a mute holds within a minute. Critical messages cannot be muted.
The second one is really two, so ask it as two, because a daily figure alone will not get you the campaign. Try: "How many a day, and separately, how large is the biggest single campaign? Those give me two different designs."
The third is where a strong answer goes further than it was asked, and it sounds like this: "Yes, it can arrive twice, because losing a sign-in code is worse. But a second marketing text is a paid message and a complaint, so I'd like to come back to that channel on its own."
Agreed, and Ruled Out
Four things are functional. Accept a notification for a user on a named channel. Send it through that channel's provider. Accept a campaign for an audience. Let a user set preferences. Three are non-functional. A critical message reaches the provider within seconds, whatever else is going on. An accepted notification is never silently lost. A duplicate is rare, and its cost is chosen per channel.
Four things you rule out aloud: writing templates, choosing a campaign's audience, an in-app inbox, and whatever a provider does after it accepts a message. That last one draws the edge of your promise. You can promise that a message reached the provider, and nothing beyond it.
The Rate That Decides Nothing, and Two That Do
Start with volume, because you have to, and then let it go. The steady services send 40 million a day. Three campaigns of 50 million in a week add about 21 million a day, if you spread them over the week. That is about 60 million a day, about 600 a second, and 1,200 at a peak of twice the average. The steady rate decides nothing, and the campaigns are not spread over anything.
The burst decides. Take a campaign as push, and assume that provider's contract lets you send 5,000 a second. Fifty million at that rate is 10,000 seconds, which is nearly three hours. Whatever stands behind a campaign stands there that long, and the interviewer will come back to this with a sign-in code in hand.
The slowest provider decides too. Text is 2 million a day, over 100,000 seconds, so 20 a second, and 40 at peak. Assume a text call takes three seconds and times out at ten. At three seconds, 40 a second is 120 calls in flight at any moment. At the timeout it is 400. Every call in flight holds something of yours while it waits, and First Design will have to pay for it.
History is the last sum worth doing. Assume you keep a 500-byte record of each send for a year. Sixty million a day is 30 GB a day, about 11 TB a year, and about 33 TB with three copies. Assume 2 TB a node, and that is about 17 nodes, with every disk full, so it is a floor. It decides how the send record is split, once a crash gives you a reason to have one.
Bandwidth can go quickly: 1,200 a second at 500 bytes is 600 KB a second, a sliver of what one 10-gigabit link carries, about 1.25 GB a second, so you say that and move on.
Set on these numbers, the estimator below has one decision line, the 17 storage nodes. Double the notifications to 120 million and the line goes to 33. The three hours do not move, because they come from the campaign and the contract, not from the day. Choose Spike and press Play, and the day carries one hour at about 5,000 a second, which shows the rate and nothing of who stands behind it. The second figure holds what the estimator cannot.
The API, Before Any Queue
Three calls cover the requirements. A POST to notifications carries a request ID from the caller, the user ID, a template and its values, the class and the channel, and for now it returns the outcome. A POST to campaigns names an audience, a template and a channel. A PUT on a user's preferences sets their choices. The request ID looks like a detail, and it is there because a caller may retry and a crash will later need something stable to find.
POST /notifications
{"request_id": "r-7f3a", "user_id": "u-42",
"template": "sign-in-code", "values": {"code": "491 208"},
"class": "critical", "channel": "text"}
200 {"notification_id": "n-9c1e", "status": "sent"}
POST /campaigns
{"audience": "a-spring", "template": "spring-sale", "channel": "push"}
201 {"campaign_id": "c-31"}
PUT /users/u-42/preferences
{"category": "marketing", "channel": "text", "muted": true}
Five entities sit behind them. A destination ties a user and a channel to a token or an address, one for each device, and a provider's answer that a token is dead deletes it. A preference holds a user's choice for a category on a channel, muted or not, with their time zone. A template belongs to one category, so each message knows its category without the caller naming it. A notification has an ID, the request ID, the user, the class, the channel, a status and its times. A campaign has an audience, a template and a channel.
- Quoting only the daily average, which sits inside every limit. The campaign is what breaks the design, and you find that out in front of the interviewer.
- Answering "exactly once" to the duplicate question. The next question is "exactly once where?", and the provider is outside every transaction you hold.
- Treating every message as one class, so that priority has to be invented under pressure at minute twenty-five.
- The campaign's size asked about as a separate question from the daily rate, which shows the candidate expects a burst.
- Fifty million turned into nearly three hours aloud, with what that wait means for a sign-in code.
- The duplicate question answered per channel before anyone asks for that, the first sign of a senior answer.
Knowledge Check
The interviewer gives you room for one more question before the board. Which answer reshapes this design most?
- How many templates the marketing team has in use today
- How large one campaign is, apart from the daily rate
- Which push provider the company holds a contract with
- Whether the email sender reports opens for each message
A campaign of 50 million meets a push contract of 5,000 a second. What does that turn into?
- About 15 minutes, once enough workers run side by side
- About eight hours, once a copy goes out on each channel
- Nearly three hours, for whatever stands behind it in line
- About a minute at the average, since the day absorbs it
Text runs at 40 messages a second at peak, and each call takes three seconds. How many calls are in flight at any moment?
- About 120, and 400 if the calls hit a ten-second timeout
- About 13, since each call only lasts for a few seconds
- About 40, one for each of the messages sent in a second
- About 240, since the peak doubles the rate one more time
Which of these is safest to rule out of scope out loud in the first ten minutes?
- Keeping a sign-in code clear of a marketing campaign
- Holding a user's mute within the minute they expect
- Deciding when a text message may go out a second time
- Choosing which users make up a campaign's audience
You got correct