Part 7 of Building a Notification Service
Campaigns and bulk sends
Module 7 · 6 min read
Campaigns and bulk sends
TL;DR
- A campaign is a batch job with brakes. Freeze and dedupe the audience, chunk it, and throttle below the provider’s limit.
- A separate lane protects OTPs from the queue, but only headroom at the provider protects them from its rate limit.
- Estimate the cost, test on 1% first, keep pause and kill switches, send per time zone, and honour unsubscribes fast.
A million messages is a different job
So far one event meant one notification. A campaign is one message (“Weekend offer: 10% cashback today”) for a million phones through the same pipes. It can flood the SMS lane ahead of every OTP (module 3), or go over the provider’s per-second rate, which gateways throttle or reject. A mistake (wrong audience, a typo, a broken link) goes out a million times before anyone notices. And duplicates, 3 am deliveries and messages after unsubscribing annoy people. So I treat it as a small batch job with brakes.
Step 1: pick the audience, then freeze it
The audience is a query, e.g. “active users in one city who paid a bill last month and allow marketing”. Run it once and save a snapshot of user IDs with the campaign ID. Re-running it mid-send, as users join, leave or opt out, could reach some twice and skip others.
While building it, drop users who opted out of marketing, lack a valid phone number or device token, or already hit today’s frequency cap. Then dedupe, so a user in two segments gets it once, and the idempotency key (campaign_id, user_id) from module 4 holds even when a chunk is retried.
Step 2: chunk it and fan it out
Split the snapshot into chunks of, say, 1,000 users, one job each on a dedicated campaign topic. A campaign worker takes a chunk, renders the template in each user’s language, checks the latest preferences, and writes individual sends to the bulk lane (sms.bulk, push, email). Now progress is just “chunks done / chunks total”, a crash repeats only one chunk, and pausing means “stop taking new chunks”.
Chunks should keep per-job overhead small, retries cheap and progress smooth. Hundreds to a few thousand users is common, but measure. A chunk should take seconds. If it takes minutes, it’s too big.
Step 3: throttle to what the provider can take
Say the gateway allows 500 messages per second on your account (illustrative, the real number is in your contract). Go over it and you get “too many requests”, retries that make it worse, and rejected OTPs on the same account.
So throttle the bulk lane below the limit, with headroom for OTPs. A token bucket gains tokens at a fixed rate (say 400 per second) up to a maximum. Each send takes one, and with none left the worker waits. Small bursts pass, but the average never goes over the rate. Several worker machines share one bucket in a store such as Redis.
every second: tokens = min(capacity, tokens + rate)
before a send: if tokens >= 1: tokens -= 1; send()
else: wait a little and try again
In the demo, OTP traffic is a steady 20 per second.
This surprised me. A separate lane protects OTPs from the queue, but only headroom protects them from the provider limit. Also, a million messages at a few hundred per second takes the better part of an hour, so “send now” is never instant.
Step 4: schedule in the user’s time
“Send at 7 pm” means 7 pm for each user. Group the snapshot by time zone and send each group as its own wave, still applying quiet hours (module 6), because a long send can drift past 10 pm. And if every campaign is set for 7:00:00 sharp, add a few minutes of spread.
Step 5: brakes, progress and cost
- Estimate first. Show “812,400 recipients, about 1.1 million SMS segments, about $X, about 45 minutes at 400 segments a second” and require confirmation. This catches most expensive mistakes.
- Start small. A test send to staff, then 1% of the audience, catches broken links and bad rendering cheaply.
- Pause, cancel, kill switch. Every worker checks the campaign state (
running,paused,cancelled) before each chunk, and a global kill switch stops all bulk sends. - Track progress per chunk. Chunks done, messages sent, failed, skipped (opted out, capped), and delivery receipts as they arrive.
- A/B variants. Split the snapshot randomly (for example by a hash of the user ID), record each user’s variant, and compare clicks or conversions afterwards.
Step 6: unsubscribe has to work
Every marketing message needs a clear way out. “Reply STOP” for SMS, an unsubscribe link for email, an in-app setting for push. Workers re-check preferences right before sending, so an opt-out applies even to queued messages and the user is counted as “skipped: opted out”.
For email, the big mailbox providers enforce this. Since 2024, Gmail and Yahoo require bulk senders to support one-click unsubscribe (the List-Unsubscribe header plus List-Unsubscribe-Post, RFC 8058) and to honour it within days. SMS rules vary by country, so check yours.
| Risk | Guard |
|---|---|
| OTPs stuck behind the campaign | Separate topic and workers for OTP (module 3) |
| Provider rejects traffic | Token-bucket throttle below the limit, with headroom |
| Wrong audience or text | Snapshot, cost estimate, test send, 1% canary |
| Duplicates | Dedupe the snapshot; idempotency key (campaign, user) |
| Messages at 3 am | Per-time-zone waves and quiet hours |
| Something goes wrong mid-send | Pause, cancel and a global kill switch |
Quick check
OTPs have their own Kafka topic, but during a big campaign they are still slow. Campaign workers send at 100% of the gateway’s limit on the same account. What’s the fix?
The bottleneck is now the provider. If the campaign uses the whole rate limit, the gateway throttles OTPs no matter how many workers or partitions you add. Reserve headroom, or send OTPs through a different account or route.
Q&A
Why not let the provider do the bulk send?
For simple cases the bulk APIs many SMS and email providers offer are fine. You still need the snapshot, preferences, dedup and cost check on your side, and you lose the single view of every message in your notification log unless you sync their reports back.
Push is free, so why throttle it?
APNs and FCM have their own limits, but mostly it’s to protect your own system. A million taps on a deep link in five minutes can overload the app’s backend, so spreading the send spreads the traffic it causes.
References
- Token bucket (Wikipedia): the rate-limiting algorithm used above.
- RFC 8058: signalling one-click functionality for list email headers.
- Alex Xu, System Design Interview, chapter “Design a notification system”.