Your OTP arrives in seconds, your food-delivery app pings when the rider is near, and a shopping app emails you about a sale. Behind all of them is a notification system. “Design a notification system” is a frequently reported question at companies like Amazon, Flipkart and Swiggy because it's the purest test of queues, retries and idempotency, ideas you'll reuse in the news feed and WhatsApp designs next.
Step 1 · Requirements
- Functional: send push, SMS and email (plus in-app); templates in many languages; respect user preferences and opt-outs; scheduled and bulk sends; delivery status.
- Non-functional: OTPs in seconds; never lose an important message; no duplicates; handle huge spikes (a sale to millions); don't spam users.
- Out of scope (say it): building our own SMS network or email servers. We use third-party providers.
Step 2 · Estimates
- Volume: 50 million users; 10 million push, 1 million SMS and 5 million emails a day ≈ 16 million/day ≈ 200 per second on average.
- Spikes: one marketing campaign can add 5 million messages in an hour (~1,400/s). The queue absorbs this; workers drain it at the providers' allowed rate.
- Storage: a status log row per message (~200 bytes) ≈ 3 GB per day. Keep 90 days, then archive.
Step 3 · API
POST /notify
{
"idempotencyKey": "order-981-shipped", // same key twice = sent once
"userId": 42,
"template": "order_shipped",
"params": { "orderId": "981", "eta": "Tuesday" },
"channels": ["push", "email"], // optional: else use the user's preferences
"priority": "normal", // high (OTP) | normal | low (marketing)
"sendAt": null // or a time, for scheduled messages
}
→ 202 Accepted { "notificationId": "n_7f3a" }202 Accepted means “received, will deliver”. The caller never waits for an SMS provider.
Step 4 · Design
🎮 Send a notification through the pipeline
Try an OTP during quiet hours with the SMS provider down, then a sale offer for a user who opted out. The pipeline's job is as much about what NOT to send as what to send.
The key pieces
- Preferences service: which channels a user allows, opt-outs per category (marketing, social), quiet hours and language. Cache it; it's read on every message.
- Template service: “Your order {orderId} arrives {eta}” in every language, so product teams can change wording without code.
- Device tokens: a table of each user's push tokens per device. Remove tokens that providers report as invalid.
- Queues: one per channel (and per priority) so a slow email provider never delays SMS, and OTPs skip ahead of marketing.
- Workers: stateless consumers that call providers. Scale each channel separately.
| Channel | Provider | Speed | Cost | Best for |
|---|---|---|---|---|
| Push | APNs (iPhone), FCM (Android) | Seconds | Free | Order updates, chats, reminders |
| SMS | SMS gateways | Seconds | Paid per message | OTPs, critical alerts |
| Email delivery services | Seconds to minutes | Very cheap | Receipts, digests, newsletters | |
| In-app | Your own database + WebSocket | Instant while open | Free | Notification bell / inbox |
Step 5 · Scale and reliability
- At-least-once + idempotency: queues may deliver a message twice; the idempotency key makes the second one a no-op.
- Retries: exponential backoff with jitter (1 s, 2 s, 4 s…), a maximum number of tries, then a dead-letter queue for a human or a later job to inspect.
- Fallbacks: a second SMS provider, or fall back from push to SMS for critical messages if the push isn't delivered.
- Rate limits: per user (no more than a few marketing pushes a day) and per provider (respect their throughput limits). That's the rate limiter again.
- Bulk campaigns: don't loop over 5 million users in one request. A campaign job pages through the audience and enqueues in batches.
- Tracking: store status per message (queued, sent, delivered, opened, failed) for support and analytics; providers report back through webhooks.
🧠 PATH memory card
Remember the notification system design for good
Picture it · Anchor it · Trace it · Hit-recall it. Four quick steps that turn “I read it once” into “I can explain it in an exam”.
- P
Picture it
Your brain remembers scenes far better than definitions.
← swipe to see the whole picture →
The college post room. The clerk first checks the do-not-send list, sorts letters into trays (one per delivery type, urgent tray first), hands them to couriers, and if nobody's home the courier tries again later. Every letter has a reference number, so nobody gets the same letter twice.
- A
Anchor it
A silly sentence you can't forget, where every word maps to the concept.
“Check, Queue, Send, Retry”
- Check→Preferences, opt-outs, quiet hours, idempotency key
- Queue→One queue per channel and priority; absorbs spikes
- Send→Channel workers → APNs/FCM, SMS gateway, email service
- Retry→Exponential backoff, fallback provider, dead-letter queue
- Reference number→Idempotency key: at-least-once becomes exactly-once in effect
- T
Trace it
Run it by hand on a tiny example once. Doing beats reading.
Order 981 ships. Check: keyorder-981-shippedunseen; user allows push + email, not in quiet hours. Queue: one message on the push queue, one on the email queue. Send: push worker → FCM ✓; email worker → provider times out. Retry: wait 1 s, 2 s → accepted ✓. The worker crashes before acking, the queue redelivers, the key is already marked sent, so it's skipped. No duplicate email. - H
Hit-recall it
Test yourself now, then again on the day each card comes back.
Card 1 / 8 · new0 mastered · 0 practisedSay the answer out loud first, then flip and be honest.
Follow-up questions interviewers ask
- “Exactly-once delivery?” True exactly-once over a network isn't practical; use at-least-once delivery plus idempotency so the user sees it once.
- “Message ordering?” Usually not needed across notifications. When it is (order placed → shipped), partition the queue by user ID so one user's messages are processed in order.
- “How do you measure success?” Delivery rate, time to deliver (especially OTPs), open and click rates, and opt-out rate after campaigns.
Where to go next
Notifications push one message to one person. A news feed pushes every post to all of an author's followers, and some authors have millions: design a news feed.
Key takeaways
- ✓Accept fast (202), then deliver asynchronously through queues.
- ✓Check preferences, opt-outs, quiet hours and an idempotency key before anything is sent.
- ✓Use separate queues per channel and priority so OTPs never wait behind marketing.
- ✓Retry with exponential backoff, fall back to another provider, and dead-letter what still fails.
- ✓Rate-limit per user and per provider; track every message's status.
Frequently asked questions
How does a notification system work?
Other services (orders, payments, social) send an event to a notification service. It checks the user's preferences and quiet hours, renders a template, and puts one message per channel (push, SMS, email) onto a message queue. Workers for each channel take messages from the queue and hand them to third-party providers such as Apple's and Google's push services, an SMS gateway or an email service, retrying failures and recording the delivery status.
Why use a message queue in a notification system?
A queue separates accepting a notification from delivering it. The calling service gets a fast response, sudden spikes (a sale announcement to millions) are absorbed and processed at a steady rate, failed sends can be retried without blocking anything, and each channel can scale its workers independently. Separate queues per priority keep urgent OTPs from waiting behind marketing emails.
How do you avoid sending duplicate notifications?
Give every notification an idempotency key (for example, the event ID). Before sending, the service checks a store of recently processed keys; if the key was already handled, it skips the send. This matters because queues usually deliver at-least-once, so the same message can be processed twice after a retry or a worker crash.
What is exponential backoff?
Exponential backoff means waiting longer after each failed attempt before retrying: for example 1 second, then 2, then 4, then 8, usually with a little random jitter and a maximum number of tries. It gives a struggling provider time to recover and avoids every worker hammering it at the same moment. Messages that still fail go to a dead-letter queue for investigation.
How do push notifications reach a phone?
The app registers with the phone's push service (Apple Push Notification service on iPhone, Firebase Cloud Messaging on Android) and receives a device token, which it sends to your backend. To notify the user, your server sends the message and the token to APNs or FCM, which delivers it to the device over a connection the operating system keeps open.
🧠 Quick check: notification system
1. A flash-sale email to 5 million users is queued at the same time as login OTPs. How do you keep OTPs fast?
2. A worker crashed after sending but before marking the message done, so the queue redelivers it. What prevents a duplicate SMS?
3. The SMS provider times out. What's the best first response?
4. Which check must happen before a marketing push is sent?
Checking your access…