Notification System: Deep Dives and Curveballs
A text message cannot be unsent, and when a worker dies mid-call, nothing you hold says whether one went. Minutes thirty to forty-five start there, with the marketing text you parked in Scope. Then quiet hours and the cap get their place, the provider's limit becomes a budget, and four curveballs change what you were asked to build.
What a Duplicate Costs, Channel by Channel
There is one question: when the outcome is unknown, resend or drop? Scaling It left about six of these per crash on text alone, and the key answers only where a provider offers one.
A sign-in code resends. A lost code locks someone out, and a second one is noise. A push resends under a collapse ID, a value that merges the copies into one notification, as Apple's service does. Android needs a tag for that, since its collapse key only replaces a copy not yet delivered. Either way the user normally sees one. Email resends, and the reader deletes one. A transactional text resends, and a paid message is accepted as the price.
A marketing text drops. A loss costs nothing anyone will notice, and a duplicate is money and a complaint. That is the exception you parked, and it swaps at-least-once for at-most-once on that one pair of class and channel. Say what it risks: a few marketing texts never go out, and their records close as dropped, so the loss is not silent.
Choose it out loud: "When the outcome is unknown, I resend a sign-in code and drop a marketing text. The code's loss locks someone out, and the text's duplicate is a paid message and a complaint."
The choice is one grid of class by channel, held as configuration that the workers load, so changing a rule is not a deploy. Chapter 24's Payment System has the same unknown outcome at a far higher price, and it gets its own answer.
Resend an unknown outcome when a loss hurts more than a second copy: a sign-in code, a security alert, an order confirmation.
Drop it when a second copy costs more than the loss: a marketing text, where a repeat is a paid message and a complaint.
Where the Checks Live
Preferences, the cap and quiet hours all sit at send time, because a message can wait for hours, and a campaign accepted at noon is still going out at three. Critical skips all three. Quiet hours and the cap stop only marketing, since an order confirmation held until morning is wrong too, while transactional messages still obey a mute.
The cap is a counter per user per day, on the cache node beside the preference entries. It is read before the send and incremented after, so a crash between the two may let one extra through. Parallel sends slip through the same way, one extra for each send in flight past the last slot. A strict count would have to reserve the slot before a call that may take three seconds and give it back when the call fails, and a few extra marketing messages are not worth that, since one user rarely has more than a couple in flight. The increment is a third operation, so the node's load is at most 15,000 a second, still far under its 100,000.
Quiet hours are the other half of what you parked. They come from the time zone on the user's preference. A message that falls inside them goes to a schedule, a table keyed by send time beside the send records, and its worker acknowledges it, so it never blocks a queue. The planner reads what has fallen due and feeds it back at its paced rate. The rejected option is all checks at the API, which is simple and wrong for anything that waits.
The Provider's Limit as a Budget
A hard split wastes the reserved share at night, when 1,000 push a second sit idle for messages that are not coming. Borrowing recovers it, if a critical message can take that share back within a second. That means each send asks a shared rate limiter, one more call per send, which Chapter 11's Rate Limiter designs and you use here as a box. If the limiter is unreachable, workers fall back to the hard split, which needs no limiter.
Start with the hard split, and name how small the saving is. Borrowing the whole reserve turns three and a half hours into nearly three, so about forty minutes on a campaign, and less while steady traffic is flowing. Borrow once a campaign's duration is the complaint. The shares themselves are configuration, beside the resend rules.
Watch each class's oldest message, as Chapter 6's Backpressure and Load Shedding says, and never depth alone. A marketing queue holding five minutes of work is healthy. A critical message older than a few seconds is the alarm, because Scope promised seconds. Running the monitoring stack that raises it is Observability Deep Dive's subject.
Curveballs
"The text provider is down for an hour. Add a second one." The workers hold a breaker per provider and fail over by class: critical moves at once, transactional follows when its minute is at risk, and marketing waits, with the planner paused behind its full queue. Keep about 5 percent of text on the second provider, 2 a second at peak, so you know it works before you need it. The idempotency key does not cross providers, so up to the 120 calls in flight may go twice. For critical, you accept that.
"Product wants delivery receipts and open rates." Providers call back late, out of order, and not on every channel. Receipts go to a log, and a consumer of that log moves a record's status forward only, so a late receipt never undoes a newer one. Analytics read the log, off the send path. No receipt leaves the record at sent: delivery is not known, which is not the same as failed. If each send gets one, that is a third write per send: 15,000 a second at the same peak, so 30 partitions at half load, unless the consumer is allowed to fall behind the peak.
"Marketing must arrive at 9 am local time, everywhere." The planner schedules per time zone, stored as a zone and never as an offset, because daylight saving moves the offset. Take a campaign of 50 million with 30 percent of its audience in one zone. Fifteen million at 4,000 a second takes 3,750 seconds. So you tell the interviewer what the promise really is: "At our marketing rate, 9 am means 9 to about 10 in the biggest zone, unless we borrow or the contract grows."
"A bug sent the same campaign twice. Now stop it." An ID from campaign and user makes a second run a no-op at the send record. A new campaign ID gets past that, and the per-user cap is the last guard: a user already at the day's limit gets no repeat, and one under it still does. A per-campaign flag, set by a stop call on the API, is the kill switch. It lives in the database, the planner checks it before each batch, and workers hold a copy refreshed every 10 seconds, an assumed interval. The planner stops first, so even if the workers were slow to see it, only five minutes of queue are left to send, and the damage has a size.
How Each Level Answers
At mid level, the candidate reaches queues and workers once the slow provider is named, sizes the campaign's drain, and separates critical from marketing at the sign-in code probe. They say at-least-once and add an ID when asked about a crash, and they move the preference check when the interviewer points at the waiting message.
At senior, the candidate raises the campaign first and puts three hours on it, then splits the provider's limit as well as the queues. They price a duplicate per channel unprompted, choose to drop a marketing text and say what that risks. They give every check a place and the mute a bound.
At staff, the candidate asks who owns the send budget when two teams' campaigns collide, and designs the kill switch and the cap before anything goes wrong. They plan the second provider and what that does to duplicates. They ask what a text costs against a lost code for this business, and they treat the resend rules as something the product owns.
- Applying one duplicate rule to all channels, which is what this problem exists to catch.
- Answering the second provider with "fail over", and no word on the calls still in flight.
- Answering local-time delivery with a cron job per zone and no sum for how long a zone takes.
- Resend or drop stated with the condition that decides it, per class and channel.
- The cap and the flag brought out as designed guards, not as incident response.
- Duplicates chosen first when the floor is left open, which shows where the candidate sees the risk.
Knowledge Check
"The text provider is down for an hour, and you have a second one." What is the best move?
- Move every class across at once, so nothing waits on the outage
- Move critical at once, hold marketing, accept up to 120 repeats
- Move nothing, and let the durable queue hold it all for the hour
- Move critical, with the same idempotency key so nothing goes twice
A delivery receipt arrives hours late, after a newer status is already on the record. What may it change?
- The status, whatever it says, since it is the latest news
- The send decision, by causing that message to go out again
- Nothing at all, since receipts are never put on the record
- The status, and only if that moves the status forward
Thirty percent of a 50-million audience is in one time zone, and marketing runs at 4,000 a second. What does "9 am local" cost?
- Nothing, since the planner starts the whole zone at 9:00
- About 20 minutes, once more workers are added for that zone
- About an hour, so 9 am means 9 to about 10 in that zone
- About three and a half hours, the length of the campaign
A bug runs the same campaign again, this time under a new campaign ID. Which guard is left?
- The send record, keyed by the campaign and the user
- The provider's idempotency key sent on each message
- The five minutes of work the planner holds in the queue
- The per-user cap, which is read before every send
You got correct