Notification System: Scaling It
Between minute twenty and minute thirty the outside world gets in. A text provider slows down, and every caller is stuck on the line with it. Marketing lines up fifty million messages ahead of one person trying to sign in. A worker dies just after a provider took a message, so the message returns with no memory of having gone out. And a user mutes marketing while a campaign message is already waiting for them. Each of those adds one thing to the board, and each comes with a price you say out loud.
The Slow Provider
"Three seconds a text, and your callers are stuck behind it. How do you get them out?" You stop making them wait for a company you do not run. The API writes the request to a durable queue, and once the broker has said it holds the message, it answers 202 with a notification ID. There is one queue per channel, each with its own workers, which is the pool per dependency from Chapter 6's Failure Across Many Services.
The broker is the box Chapter 14 opened, so here you use it as a box. Each message is one task for any worker, and a slow call must not hold up the rest, so this is the per-message side of that chapter's comparison of offsets and acknowledgements. A worker holds a message on a lease and acknowledges it when the send is done.
Text needs 120 calls in flight at peak, so assume 240 slots, twice that, on the usual reasoning of running at half the limit. Then the provider slides to ten-second timeouts and wants 400. Two hundred and forty slots at ten seconds finish 24 calls a second, against 40 arriving, so text backs up, and only text. A breaker in the text workers sees the timeouts, stops the calls and tests now and then, and the queue holds the work. Push and email have other pools and do not notice.
POST /notifications {"request_id": "r-7f3a", "user_id": "u-42", ...}
202 {"notification_id": "n-9c1e"}
GET /notifications/n-9c1e
200 {"status": "sent"}
The call now returns 202 and an ID, and a second call reads the status by that ID. What it costs comes first in your answer: "The price is the outcome. The caller now knows I took the request, and not whether the text went out." You also have a broker to run. If it refuses a write, the API answers with an error and the caller retries, so nothing is accepted that is not held. And between queue and worker, delivery is at-least-once, which the crash below comes back for.
The Campaign
"Fifty million marketing messages go in. Where is the sign-in code now?" That depends on its channel, and this campaign is on push. A code sent by text is untouched, because one queue per channel already separated them. A code sent by push, or a security alert, is behind all of them, nearly three hours back. So the queues split again: a queue per class per channel, with its own workers, from Chapter 6's Backpressure and Load Shedding. Three classes on three channels makes up to nine.
Separate queues still share one contract. The provider counts all your calls together, so marketing running at the full 5,000 a second leaves nothing for the code, whichever queue it stands in. So you split the limit too. Assume 1,000 of the 5,000 push a second are reserved for critical and transactional messages, against their peak of 600. On text, assume a contract of 200 a second, with 100 reserved against a peak of 40. Each class's workers pace themselves to their share.
Marketing gets the other 4,000. Fifty million at 4,000 a second is 12,500 seconds, about three and a half hours, so the split costs each campaign about forty minutes.
Fifty million messages also have no business sitting in a queue, because anything can change while they wait. So the API hands a campaign to a planner, which expands the audience no faster than sending drains it. The queue holds about five minutes of work: 4,000 a second for 300 seconds is 1.2 million messages. The rest of the campaign is still a plan. When sending stops, the planner stops. If the planner dies, a replacement resumes from the last user it expanded.
Age is the other rule. Assume a sign-in code is good for five minutes, and one older than that is dropped unread, the rule Backpressure and Load Shedding gives for a request older than its caller's timeout. Its status says expired, so the loss is not silent. That takes an outage to reach, and a curveball in the next lesson deals with the outage.
The cost is campaigns slower than the provider could carry and a planner to run. Marketing also becomes the class that gets shed, and you say that too: "When something has to give, it's marketing. I'm choosing to run campaigns slower than the contract allows, so that a code is never in line behind one."
The Crash in the Gap
"The provider took it, your worker died, and the message comes back. Does the user get two?" As drawn, yes, and nothing on the board knows. The lease lapses, the broker hands the message to another worker, and that worker calls the provider again. So you add a send record, written as sending before the call and as sent after it.
The record is keyed by an ID born at the origin: caller and request ID, or campaign and user. That is the rule from Chapter 6's Delivery Semantics at Scale, because both copies must carry the same ID. The notification ID the caller was given is this one, and the status it reads is this record's. Until a worker writes the record, the read finds nothing, and that answers queued. It also catches a caller's retry, and it finally gives the request ID a job. The row the first design wrote has become this record.
The next worker reads the record first. Redelivered and marked sent: stop. Redelivered and marked sending: the outcome is unknown, because the first worker may have died before the call, during it or after it. So send the ID as the provider's idempotency key, a value the provider uses to recognise a repeat, where one is offered. Where none is, the channel decides, and that is the first deep dive.
Size it from both ends. Scope's 33 TB is about 17 nodes, as a floor. The rate comes from the worst moment, a campaign on top of the steady peak: 4,000 marketing push, 600 other push, 160 email and 40 text. Call that 5,000 sends a second. Two writes per send is 10,000 writes a second. At 1,000 writes a second for one key-value partition, you need 10 partitions, and 20 at half load, keyed by the ID hashed. Bytes set the nodes, and writes set the partitions.
If the send store refuses a write, a critical message goes anyway, because a duplicate code beats a lost one, and everything else waits in its queue. What it costs is two writes per send, and duplicates that remain. The gap between the call and the second write stays open. Assume 20 text workers sharing 120 calls, so one crash leaves about six unknown outcomes. The record narrows the gap, and you say so before anyone asks whether it closes it.
The Late Mute
"They muted marketing five minutes after the campaign reached them. What stops the send?" Nothing yet, because the check happened before the message started waiting. So the worker reads the preference and the per-user cap just before sending, through a cache. A critical message skips the read. A message the check stops closes its record as muted or capped, because Scope promised nothing accepted is silently lost. The cap is a count of what this user has had today, and the deep dive places it.
Assume 100 million users at 200 bytes, which is 20 GB. About 5,000 sends a second at two reads each is 10,000 operations a second. One cache node holds about 100 GB and does about 100,000 simple operations a second. So one node and a replica carry it, with room on both counts.
Staleness is the question from Chapter 2's Caching at Scale. A write to the preference table in the database invalidates the entry, and a miss reads the table and fills it again. A short expiry would not help here, because at 5,000 sends a second across 100 million users, an entry would be gone long before that user's next message, and nearly every read would miss. So entries stay until they change. A new or emptied node is loaded from the table before workers use it, or a campaign of 50 million meets 50 million first-time misses. An invalidation can still be lost, so a small job runs every 30 seconds and invalidates again every row changed since its last run. That bounds what invalidation misses, inside the minute agreed in Scope. If the job stops, ordinary invalidation still runs and only the bound is gone, so you alarm on it.
The user's own settings page reads the table and not the cache, so they read their own write, the promise from Chapter 4's Consistency Models That Come Up. If the node and its replica are both out, critical is untouched, transactional reads the table, and marketing waits, because a send without a check can break a mute. What it costs is a read on every send, and a message can still leave inside that minute.
The Board at Minute Thirty
Calling services reach the API through the load balancer. The API puts each message on a queue per class per channel and answers 202. The planner feeds the marketing queues at their pace. Workers take a message, check the preference and cap, write the send record, call the provider and write the record again. The arrows to the providers now leave the workers, and a timeout on text stops at its breaker while the queue holds the work.
The one database stays, for tokens, addresses and preferences. The API reads the token or address once, the planner reads them in batches, and the message carries it, so a worker has only its two reads. The row per notification moved to the send store, and the 200 threads no longer wait on anyone.
Three questions are still open, and you can list them as they stand: "What an unknown outcome does on each channel, where the cap and quiet hours live, and who gets the provider's spare room at night. I'd take them in that order."
- One queue with a priority field. Asked what happens when every worker is busy on the campaign, the field has no answer.
- Separating the queues and forgetting that all classes share one provider contract. The next probe is what a campaign at the full rate leaves for the code.
- Claiming the send record removes duplicates. It narrows them, and the gap between the call and the write stays.
- Checking preferences only at the API, then finding that the campaign waited for hours.
- The caller's lost outcome said aloud as the price of the queue.
- The provider's limit split as well as the queues, with numbers.
- A staleness bound on the mute, where a weaker answer says "real time".
Knowledge Check
The API now answers 202 once the broker holds the message. What has the caller given up?
- Any guarantee that the request it handed over is kept
- The chance to retry when its own request fails outright
- Hearing in its answer whether the message actually went
- Any say in which channel carries the message it asked for
Marketing holds 4,000 of the 5,000 push a second after the split. How long does a campaign of 50 million take?
- Nearly three hours, because the contract has not changed
- About three and a half hours, which is 12,500 seconds
- About five minutes, the length of the planner's queue
- About ten hours, because the planner holds the work back
A worker takes a redelivered message and finds its send record marked sending. What does that tell it?
- That the provider may or may not have taken the message
- That another worker is sending that same message right now
- That the provider call failed and is safe to make again
- That the send went through and only the ack went missing
A user mutes marketing, and the invalidation for that write is lost. What does the 30-second job put a limit on?
- How long the mute takes to show on their settings page
- How many sends can reach the cache node during a minute
- How long a campaign message can wait in the marketing queue
- How long a lost invalidation can leave that entry stale
You got correct