Skip to content
CodeBrewerz logoCodeBrewerz
System Design

Notifications: Every Message You Send Spends a Budget You Cannot Refill

The delivery pipeline is a queue and a few provider SDKs. The engineering is in not sending — deduplication, coalescing, and a cascade that can tell a phone in a lake from a user who simply did not open the app.

Published 8 min readBy Piyush Jain
  • System Design
  • Notifications
  • Messaging
  • Reliability

Most systems in this series fail loudly. A notification system fails by working exactly as specified: it delivers every message it was asked to deliver, on time, and the user turns notifications off.

That is a permanent, unrecoverable loss of a channel — not for the message that annoyed them, but for every message you will ever want to send them again, including the one about the fraudulent charge. So the design constraint here is not throughput. It is that attention is a budget you spend and cannot refill, and almost every technical decision in the system is really about not spending it.

The pipeline, briefly

The architecture is not the hard part, so here it is in one paragraph. A producer emits a domain event. A rules layer decides who should hear about it and on which channels, consulting user preferences, quiet hours and per-category opt-outs. A rendering step turns the event into localised copy per channel. Then a per-channel dispatcher talks to the provider — APNs, FCM, an email vendor, an SMS gateway — with retries, and a status collector records what happened.

Two structural notes. Preferences must be checked at send time, not at enqueue time, because the queue may hold a message for minutes and the user may have opted out thirty seconds ago. And each channel needs its own queue and its own rate control, because SMS throughput is a contractual number, email has reputation consequences if you burst, and push has neither — one shared queue means the slowest provider sets everyone’s latency.

Who decides, and when

One structural choice is worth making explicitly because it determines how much of this you can change later: does the producing service decide that a notification should be sent, or does it merely announce that something happened?

If the producing service decides, then the rules live in the order service, the comment service and the billing service, in three different shapes, and adding a quiet-hours policy means three pull requests and three deploys. If the producing service announces — “order 1042 shipped” — and a single notification service owns who hears about it, on which channel, and subject to which caps, then policy is one system with one owner. Every notification platform that survives contact with a growing product converges on the second shape, usually after doing the first.

The consequence worth planning for is that the notification service now needs to read state it does not own. “Notify the order’s watchers” requires knowing who the watchers are, which lives elsewhere. Resolving that at send time means a synchronous dependency on another service in your delivery path; carrying the recipient list in the event means the list is a snapshot that may already be wrong. Neither is clearly right, and picking deliberately is better than discovering the choice was made by whoever wrote the first consumer.

Not sending is the feature

Figure 1

Eleven events in ninety seconds

Timeline · one column per five seconds—
events in
pushes out
Delay before the first notification—

—

Policy
Coalescing window
A busy thread produces eleven events in a minute and a half. Sending each one is eleven buzzes for a conversation the user is probably already reading. Deduplicating identical events removes retries and double-publishes but not the volume. Coalescing by a window collapses the burst into a single summary at the cost of delaying the first one — which is why the window is the knob and not the policy.

Eleven events in ninety seconds, which is an ordinary busy thread. Send every one and the user gets eleven buzzes for a single conversation, including the duplicates your own at-least-once delivery produced.

The first policy is deduplication, and it is mandatory. Every notification needs a stable key derived from the event — not a random id — so a retried or double-published event collapses into one send. This is the same idempotency requirement as the message queue, and here the consequence of missing it is visible to the user rather than to your ledger.

But deduplication fixes correctness, not volume. Switch to coalescing and the burst becomes one notification: hold the first event for a window, fold in everything that arrives during it, and send a summary. “Ana and 4 others replied” is one interruption instead of eleven, and it is more useful.

The whole cost is the delay on the first message, which is why the window is a property of the message type and not a global constant. A chat message can wait ten seconds. A delivery update can wait a minute. A fraud alert cannot wait at all, and coalescing it would be a defect.

Two related controls belong next to it. A per-user rate cap — a hard ceiling on notifications per hour regardless of what the rules layer wants — is the backstop for the bug you have not written yet, and every mass-notification incident is a story about not having one. And quiet hours in the user’s timezone, with an explicit override list for categories that genuinely justify waking someone, which should be a very short list.

The cascade lies to you

Then the pattern everyone reaches for: if push does not work, try email; if email does not work, try SMS.

Figure 2

Escalate on silence, and see what silence meant

    Messages sent—
    Cost—
    User experience—

    —

    What really happened
    Escalate when
    Each channel reports something different: push tells you the platform accepted it, email tells you a server accepted it, SMS tells you a carrier accepted it. None of them tells you a human saw anything. A cascade that escalates on no-response therefore escalates on messages that were delivered and read, and the user gets the same alert three times — on the channels that cost you the most.

    Set the truth to “the push arrived and was read” and the rule to “escalate on silence”. Two more messages go out — on the two channels that cost money — to tell the user something they already read. At a million recipients that is a five-figure bill for annoying people.

    The reason is that no channel can report what you actually want to know. Push tells you the platform accepted the message. Email tells you a server accepted it. SMS tells you a carrier accepted it. None of them tells you a human saw anything. So “no response” is not evidence of non-delivery; it is the absence of a signal you were never going to get.

    The only escalation trigger that means something is a hard failure the provider reports — an unregistered device token, a hard bounce, a rejected number. Those are facts. Escalating on silence is inference, and it is wrong in the most common case.

    Which leaves a genuine dilemma for messages that matter: a muted push is a message nobody sees, and escalating on failure alone will not catch it. The honest resolutions are to require an in-app acknowledgement for the small set of notifications that truly need one, or to accept that a nudge is a nudge and let it go unread. What does not work is treating every notification as an alert.

    The parts nobody mentions

    Device tokens rot constantly. Users reinstall, switch phones, revoke permission. The provider tells you a token is dead in the response, and if you do not act on that you accumulate a growing set of addresses that can never be delivered to — inflating your “sent” numbers, wasting your quota and quietly ruining every metric that has delivery in the denominator. Prune on the failure response, every time, as part of the send path.

    Fan-out size is unbounded and your rules layer is not. “Notify everyone following this” is a query that returns 40 followers in testing and 4 million in production. That job must be chunked and rate-limited from the start, because the version that loads all recipients into memory works perfectly until the day it matters.

    The template is user-visible code with no deploy gate. A missing interpolation ships Hello {{name}} to a hundred thousand inboxes, and unlike a web page you cannot fix it after the fact. Render a preview in CI, treat template changes as code changes, and require that every variable has a fallback.

    Email deliverability is reputation, not code. SPF, DKIM and DMARC are table stakes; the part that bites is that a sudden volume increase from a cold sending domain routes you to spam, and recovering a burned domain takes weeks. Warm up gradually, keep transactional and marketing on separate domains, and watch complaint rates as a first-class metric.

    Store what you sent. An in-app notification centre, backed by the same records, gives the user a place to find the thing they dismissed — and gives you a support answer better than “it should have arrived”. It also turns push into what it should always have been: a hint that something is waiting, not the only copy of it.

    The short version

    The scarce resource is attention, and spending it badly costs you the channel permanently. Design for not sending.

    Deduplicate on a stable event key so your own retries do not become interruptions, then coalesce bursts into a window sized per message type, then put a hard per-user hourly cap underneath everything as a backstop for the bug you have not written yet. Check preferences at send time rather than enqueue time, and give every channel its own queue and rate limit so the slowest provider does not set everyone’s latency.

    Escalate between channels only on a hard failure a provider actually reported. “No response” is not evidence of non-delivery — it is the absence of a signal no channel ever promised — and escalating on it sends the same alert three times to someone who read it on the first. Prune dead tokens on the failure response, chunk every fan-out before you need to, and keep a durable record of what was sent, because the notification centre is the only place a user can find what they swiped away.

    Next step

    Want This Built, Not Just Explained?

    CodeBrewerz builds the systems these posts take apart: web, mobile, cloud and the infrastructure underneath. Tell us what you are building.

    Start a conversation