Notification system
Design fan-out to push, email and SMS with retries, preferences and deduplication.
By Invicly Tech, , in System Design Deep Dive. About 11 minutes to read.
In short
A notification system accepts events from many services and delivers them via push, email and SMS. Queues per channel decouple sending from third-party providers, while preferences, rate limits, retries and deduplication protect users.
Explained simply
A notification system sends messages to users on behalf of the rest of the product: your order shipped, someone replied to you, here is your login code. It can send them through several channels: push notifications to a phone, email, SMS text messages, and sometimes in-app messages.
Other services do not send these messages themselves. They tell the notification system what happened and who should know. The notification system then decides which channels to use, fills in the message text from a template, and hands it to an outside provider such as Apple's or Google's push service, an email service or an SMS service.
Outside providers are slow sometimes and fail sometimes. So the system puts messages in queues and sends them in the background. If a send fails, it tries again later. The service that asked for the notification gets a quick OK and moves on.
It also protects users. It respects their settings (no marketing emails, no pushes at night), avoids sending the same message twice, and limits how many messages one person can get. A noisy notification system makes people turn off notifications entirely, which hurts the product.
Press Play on a diagram to walk through it one step at a time.
- 1Producer services
Call the notification API with an event
- 2Notification service
Validates, checks preferences, renders templates
- 3Channel queues
Separate queues for push, email, SMS
- 4Channel workers
Send with retries and backoff
- 5Providers
APNs, FCM, SendGrid, Twilio
- 6Delivery log
Status tracking and analytics
- Order svc to Notif svc: POST /notify (dedupe key)1POST /notify (dedupe key)
- 2Check prefs and dedupeNotif svc to Notif svc: Check prefs and dedupe
- Notif svc to Push queue: Enqueue push message3Enqueue push message
- Notif svc to Order svc: 202 Accepted4202 Accepted
- Push worker to Push queue: Consume message5Consume message
- 6Send via APNs, retry on failurePush worker to Push worker: Send via APNs, retry on failure
- Login codes, receipts, alerts
- Seconds matter
- High-priority queue
- Usually cannot be fully disabled
- Promotions, digests, announcements
- Minutes or hours are fine
- Low-priority, paced queue
- Opt-in, caps and quiet hours apply
In depth
Requirements and volume estimate
Clarify channels (push on iOS and Android, email, SMS, in-app), message types (transactional like receipts and login codes, versus marketing), latency (login codes within seconds, digests can wait), and whether broadcasts to all users are needed.
Then estimate. Say 50 million daily users. Push: 10 per user per day is 500 million a day, about 6,000 per second on average. Email: 20 million a day, about 230 per second. SMS: 1 million a day, about 12 per second, but SMS is the expensive one, often a few cents each, so it gets strict limits. Plan for peaks of 5 to 10 times the average, plus marketing broadcasts that can send tens of millions of messages in an hour.
These numbers say: push needs horizontally scaled workers, SMS needs cost controls more than throughput, and broadcasts need their own path so they never delay login codes.
API and data model
The main API is asynchronous: POST /v1/notifications with user ID, type, data and a dedupe key returns 202 Accepted with a notification ID. Add a bulk endpoint for many users and a scheduled send time for reminders.
Core tables: templates (type, channel, locale, subject and body with placeholders, version), user_preferences (user, type or category, channel, enabled, quiet hours and time zone), devices (user, platform, push token, last seen), contact info (verified email and phone), and a notification log (ID, user, type, channel, status, provider message ID, timestamps).
The notification log is large and append-heavy, so it fits a wide-column store or a partitioned table keyed by user and time; it powers the in-app inbox, support lookups and analytics. Preferences and devices are read on every send, so cache them, with short TTLs, in Redis.
- POST /v1/notifications returns 202 with an ID
- Templates per type, channel and language
- Preferences per category and channel, with quiet hours
- Device tokens per app install
Fanning out large broadcasts
A single event can target millions of people: a new feature announcement, a sports score, a breaking news alert. You cannot handle that in one request.
Instead, the API stores a campaign record and returns immediately. A fan-out job pages through the target segment (for example 1,000 user IDs at a time), and for each page enqueues one batch message. Workers expand each batch: load preferences and devices for those users, render templates, and enqueue per-channel sends. FCM and APNs both accept many concurrent requests over HTTP/2 connections, so push workers keep long-lived connections open and send in parallel.
Broadcasts run on separate, lower-priority queues and are paced, for example a few hundred thousand sends per minute, so they cannot starve transactional messages, overload providers, or send a wave of users to your servers all at once when they tap the notification.
- 1Create campaign
API stores it, returns 202
- 2Fan-out job
Pages the segment, 1,000 users per batch
- 3Batch queue
Low priority, paced
- 4Expand workers
Preferences, devices, templates
- 5Channel queues
Push, email, in-app
- 6Senders
Parallel HTTP/2 to FCM and APNs
Delivery, retries and provider errors
Workers must treat provider responses differently. Temporary errors (timeouts, 429, 5xx) are retried with exponential backoff and jitter, a few times, then moved to a dead-letter queue for inspection. Permanent errors are not retried. An APNs response of 410 Unregistered, or FCM's UNREGISTERED error, means the app was uninstalled: delete that device token so you stop paying to send to it. An invalid email address or a hard bounce should mark the address as undeliverable.
Delivery is at least once, so keep sends idempotent: record the notification ID before or with the send, and pass it to providers that support idempotency.
Track outcomes through provider callbacks: email services post delivered, bounced, opened and clicked events to a webhook, and SMS providers send status callbacks. For critical channels, configure a second provider and fail over when the first one's error rate jumps.
Preferences, quiet hours, limits and dedupe
Before any send, a pipeline of checks runs. Is this category allowed for this user and channel? Marketing needs explicit opt-in in many countries, while security alerts usually cannot be turned off. Is it quiet hours in the user's time zone? If so, hold non-urgent messages until morning. Has the user hit a frequency cap, such as at most 3 marketing pushes a day? Has this exact event already been sent?
Deduplication uses the dedupe key from the producer, such as order_shipped:o-77, combined with the user and channel. A Redis SET with NX and an expiry of a day or so records it atomically; a second request with the same key is dropped.
Priority separates traffic: one-time passwords and security alerts go on a high-priority queue with dedicated workers, ahead of social and marketing messages. Some systems also collapse bursts into a digest, like 5 people liked your photo.
- 1Dedupe
SET NX on dedupe key
- 2Preferences
Category and channel allowed?
- 3Quiet hours
Hold non-urgent until morning
- 4Frequency cap
Per user, per category
- 5Render and enqueue
Template, by priority
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Clarify channels and requirements~35m
- 2Estimate notification volume~35m
- 3Design the API and data model~35m
- 4Fan out through queues to providers~35m
- 5Add retries, preferences and deduplication~35m
Key points
- Separate queues per channel so a slow SMS provider does not block push.
- Return 202 immediately; delivery happens asynchronously.
- Deduplicate with an event ID so retries never double-notify.
- Respect user preferences, quiet hours and per-user rate limits.
- Failed sends retry with backoff, then land in a dead-letter queue.
Key terms
Tap a card to flip it. 0/5 known
Examples
POST /v1/notifications
{
"userId": 42,
"type": "order_shipped",
"channels": ["push", "email"],
"data": { "orderId": "o-77" },
"dedupeKey": "order_shipped:o-77"
}
-> 202 Accepted
Tables: user_prefs(user_id, channel, enabled, quiet_hours)
devices(user_id, token, platform)API and core data model.
Atomic dedupe with Redis
The order service retries its notify call after a timeout, so the notification service receives order_shipped:o-77 twice for user 42. Step 1: the first request runs SET with NX (only if not exists) and EX 86400; Redis replies OK, so the send proceeds. Step 2: the second request runs the same command; Redis replies nil because the key exists, so the service returns 202 with the original notification ID and sends nothing. Step 3: after a day the key expires on its own. Include the channel in the key so the same event can still go out by both push and email.
redis-cli SET dedupe:42:push:order_shipped:o-77 n-9001 NX EX 86400
# OK -> first time: send it
redis-cli SET dedupe:42:push:order_shipped:o-77 n-9002 NX EX 86400
# (nil) -> duplicate: skip
redis-cli GET dedupe:42:push:order_shipped:o-77
# "n-9001" -> return the original notification IDHandling a dead device token
A user uninstalled the app last month. Step 1: an event triggers a push to their iPhone token. Step 2: the push worker sends it to APNs, which answers 410 with reason Unregistered. Step 3: the worker does not retry; it deletes the token from the devices table and logs the send as failed: unregistered. Step 4: if the user has no other devices and the message is important, the system falls back to email. Over time this cleanup keeps send volume, cost and error rates honest.
Quick reference
- Return 202 Accepted; send asynchronously from per-channel queues
- Separate high-priority queues for OTPs and security alerts
- Retry 429, 5xx and timeouts with backoff, then dead-letter
- APNs 410 Unregistered or FCM UNREGISTERED: delete the device token
- Dedupe: SET key NX EX 86400 on user + channel + dedupe key
- Respect preferences, quiet hours in the user's time zone, and frequency caps
- Pace broadcasts on their own low-priority path
- Track delivered, bounced, opened via provider webhooks
Common mistakes
- Calling providers synchronously in the request path.
- One shared queue where an email backlog delays urgent pushes.
- No dedupe, so retries send the same SMS several times.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
List the checks, in order, that a notification should pass before it is sent, and say what happens when each check fails.
- Exercise 2medium
Design the notification log table so you can show a user's in-app inbox (newest first) and look up one notification by ID.
- Exercise 3hard
Your SMS provider has an outage for 20 minutes and 30,000 login codes are queued. Design how the system behaves during and after the outage.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between FCM and APNs?
- APNs is Apple's push service for iOS and macOS devices. FCM (Firebase Cloud Messaging) is Google's service for Android, and it can also forward to iOS through APNs. Most systems talk to both, or use FCM for both platforms.
- Should I build this or use a service?
- Services such as Amazon SNS, Firebase, OneSignal or Braze handle channels and providers for you. Building your own makes sense when you need custom preferences, routing and analytics at a scale where the service cost is significant.
- How do I avoid sending notifications at night?
- Store each user's time zone and quiet hours, and have the pipeline delay non-urgent messages that fall in that window until it ends. Urgent messages like security alerts usually bypass quiet hours.