System design · Queues, provider rate limits, retries
How to design a notification system
Sending one notification is trivial. The hard part is that you don't control the last hop: Apple, Google and your email and SMS providers all cap how fast you can send. Breaking news can spike to about 10,000 notifications a second against a 2,000 a second limit.
Updated · 5 min read
The short version
- Keep provider calls off the request path. The API acknowledges onto a durable queue and returns 202.
- The provider's rate limit is the real capacity. Size workers just under it and let the queue absorb spikes.
- Dedup at both ends: an idempotency key from callers, and a conditional write in workers before sending.
- Retry with backoff and jitter, send exhausted messages to a dead-letter queue, and never retry permanent errors.
- Urgent notifications get their own queue. Check preferences and quiet hours at send time.
Clarifying questions to ask first
- Which channels? (Assume push through APNs and FCM, email and SMS.)
- What triggers a notification? (Assume both events, like a new reply, and schedules, like a daily digest.)
- What volume, and how spiky? (Assume about 500 a second on average, with a 20× spike when breaking news hits.)
- How fast must a notification arrive? (Assume within about a minute, and accepting the event must take well under a second.)
- Are some notifications more urgent than others? (Yes: security alerts and one-time codes must never wait behind marketing.)
Numbers that shape the design
These are assumptions, stated out loud. One number dominates: your provider's rate limit.
| Quantity | Assumption | Result |
|---|---|---|
| Event notifications | about 43M a day | ≈ 500/s average |
| Breaking news | a 20× spike for a few minutes | ≈ 10,000/s at peak |
| Scheduled digests | about 8.6M a day, spread evenly | ≈ 100/s, all day |
| Provider limit | your provider's rate limit, 2,000/s in this example | a hard ceiling on sends |
| Delivery log | ≈ 300 bytes per notification, kept 90 days | ≈ 15 GB a day ≈ 1.4 TB |
Normal traffic of about 600 a second is well under the limit. The spike is five times over it, so no amount of your own hardware sends it all immediately. The delivery log fits a single key-value table.
API
POST /api/notifications
{ "idempotency_key": "reply-88231-u_7", "user_id": "u_7",
"category": "replies", "template": "new_reply",
"data": { "author": "Sam", "thread_id": "t_42" } }
→ 202 Accepted { "notification_id": "n_5f1c", "status": "queued" }
GET /api/notifications/{id}
→ 200 OK { "status": "delivered", "channel": "push", "attempts": 1 }
PUT /api/users/{id}/preferences
{ "push": true, "email": false, "quiet_hours": { "start": "22:00", "end": "07:00", "tz": "Asia/Kolkata" } }202 means queued, not sent. A retry with the same idempotency_key returns the first notification instead of creating another.
Data model
Every access is by a known key, so a key-value store such as DynamoDB fits.
notifications
notification_id string partition key
idempotency_key string unique, for dedup
user_id string
channel string push | email | sms
status string queued | sent | delivered | failed
attempts int
created_at timestamp
preferences
user_id string partition key
channels map per channel and category opt-outs
quiet_hours map start, end, time zone
devices
user_id string partition key
device_token string sort key, APNs or FCM token
templates
template_id string partition key
locale string sort key
subject, body textWhy sending from the API fails
Each provider call takes a few hundred milliseconds, so calling it before replying ties every API thread to a server you don't own. At 20× load the API runs out of threads, and it sends 10,000 pushes a second to a provider that accepts 2,000. The rest are rejected and lost. More API servers just get rejected faster.
With a queue, the API acknowledges in milliseconds and workers drain at a fixed rate under the limit, say 1,600 a second against 2,000. A five-second burst at 10,000 a second leaves about 42,000 messages waiting; with 1,600 going out and 600 still arriving, the backlog clears in about 40 seconds.
High-level design
- The API validates, checks the idempotency key, queues the message (SQS, say) and returns 202.
- A scheduler such as EventBridge Scheduler puts digests on the same queue, so both kinds share one path.
- Workers check preferences and quiet hours, render the template, pick devices and call APNs or FCM, SES, or an SMS provider.
- Workers record the outcome in the delivery log; provider callbacks (delivered, bounced, invalid token) update it later.
Workers, retries and dedup
Measure one worker's send rate and run as many as fit in 70 to 80% of the provider's limit. The headroom covers retries and other services on the same account. Alarm on the age of the oldest message against your delivery target, not just on depth.
Retry throttles and failures with exponential backoff and jitter so retries don't arrive as a second spike. After a fixed number of attempts, move the message to a dead-letter queue. SQS delivers at least once, so record the notification ID with a conditional write before sending and skip it if it's already there.
Preferences, quiet hours and priority
Check preferences in the worker at send time, because a user may opt out after the event. A notification landing in quiet hours is held until the window ends (EventBridge Scheduler or an SQS delay), not dropped. Urgent notifications get their own queue so a marketing backlog never delays a one-time code.
Common traps
- Retrying permanent errors such as an invalid token or a hard bounce. Mark them failed and clean up the token or address.
- Keeping invalid device tokens. They burn the same provider quota as real sends.
- Calling a provider-accepted notification delivered. Acceptance is only the handoff; update status from the channel's later reports.
- Deduplicating on template ID alone. Many recipients get the same template; key on the logical notification and recipient.
- Caching one rendered variant per template. Templates have a locale too; include it when selecting and caching.
Follow-up questions interviewers ask
- "Would more workers clear a spike faster?" Not past the provider's limit. Extra workers only get rejected faster.
- "What if a provider is down for ten minutes?" Backoff and jitter, the queue holds the backlog, and exhausted messages go to the dead-letter queue instead of being lost.
- "How do you know delivery happened?" Providers report back later: SES through SNS notifications for bounces and deliveries, push providers through responses that flag invalid tokens.
Frequently asked questions
Why not send notifications directly from the API?
+
Provider calls are slow and rate-limited. Calling them from the request path ties up API threads and, during a spike, sends more than the provider accepts, so notifications get rejected. A queue lets the API respond in milliseconds and lets workers send at a safe, steady rate.
How many workers should a notification system run?
+
Enough to stay just under your provider's rate limit, with headroom for retries. Measure how many sends one worker makes a second and divide the limit by that.
How do you avoid sending the same notification twice?
+
Callers send an idempotency key so retried requests don't create new notifications. Workers record each notification ID with a conditional write before sending, because queues like SQS can deliver a message more than once.