← Whiteboard questions

Design a notification system

Send email, SMS and push without loss or repeats

System design, Hard

Design a system that sends email, SMS and push notifications to users. There are 10 million users. A normal day sends 2 million notifications. A marketing campaign can send 5 million pushes within 10 minutes. One-time passcodes (OTP) must arrive within 10 seconds. Messages must not be lost, and users must not get the same message twice. Cover the flow from request to delivery, retries, duplicates, user preferences and how you protect urgent messages from a large campaign. Out of scope: building the email, SMS or push providers.

Examples

Input POST /notifications { "userId": "u1", "channel": "push", "type": "campaign", "idempotencyKey": "c42-u1" } is sent twice within one minute.
Output The API accepts both calls, and the user gets one push.
Why Callers retry. The design must say how a repeat is recognised.
Input A one-time passcode is requested while a 5-million-push campaign is still sending.
Output The passcode reaches the user within 10 seconds.
Why This is the requirement a large campaign puts at risk. Say what keeps it safe.
Input The push provider answers HTTP 503 for 2 minutes. 40,000 notifications are waiting.
Output None is lost. After the provider recovers they are sent, and no user gets the same message twice.
Why A provider failure is normal. Say what happens to messages in flight and to those that keep failing.

Constraints

Your answer must contain

A strong answer covers A strong answer covers the numbers, a queue per priority, at-least-once delivery with idempotency keys, retries with a dead-letter queue, and preferences checked at send time.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Start with the traffic: the normal rate and the campaign rate. Why must an OTP never wait behind a campaign of 5 million messages?

Hint 2 the approach

Do not call providers inside the API request. Accept the request, store it durably, and let queues and workers send it. Give urgent messages their own queue and workers.

Hint 3 the key step

Exactly once across a network cannot be promised. Use at-least-once delivery plus an idempotency key for each notification, and say plainly what window of repeats is left.

Walkthrough

Full walkthrough brute force to best

Naive design

The API calls the email, SMS or push provider directly inside the request. It is simple. But a slow or failing provider slows or fails the API, a crash loses the message, a retry by the caller sends it twice, and a campaign can delay an OTP.

Improved design

Put one durable queue between the API and the workers. The API writes the row and publishes to the queue, and workers send with retries. This absorbs bursts and survives provider failures, but one shared queue still lets a campaign of 5 million pushes delay an OTP.

Final design

Numbers: a normal day is about 25 per second and a campaign is about 8,300 pushes per second. Providers cap their own rates, so queue depth absorbs the burst. A 5-million campaign is fanned out by a job that reads the audience in batches of 1,000 and publishes 5,000 queue messages. At a provider cap of 10,000 pushes per second the campaign takes 500 seconds, inside the 10 minutes. At a cap of 5,000 it takes 1,000 seconds, so ask for a higher cap or add a second provider. If one worker sends about 200 pushes per second (an assumption to measure), 50 workers are needed, so the marketing queue needs at least 50 partitions. The OTP queue carries about 25 messages per second at most, so 2 workers on their own partitions keep it far below 10 seconds. Flow: the API validates the request, checks the idempotency key, writes the notification row and an outbox record in one transaction, and answers 202. A relay publishes to a durable queue per channel and per priority (otp, transactional, marketing), each with its own workers. A worker checks the user preferences and quiet hours at send time, renders the template, calls the provider and records the status. Not lost: the outbox, plus acknowledging a queue message only after the provider call returns. Not twice: at-least-once delivery plus an idempotency key that the provider also gets if it supports one. A very small window is left, so call it effectively once, not exactly once. Failures: exponential backoff with jitter, no retry for permanent errors such as a bad number, a dead-letter queue with an alert and a replay tool, a circuit breaker per provider and a second provider as failover. Preferences: a cached table of user, channel, type, opt-in and quiet hours, where legal opt-outs always win.

Follow one request

Follow the 503 case. 40,000 push notifications wait while the provider answers 503. Workers get errors and retry with growing, random waits, and the circuit breaker opens so workers stop hitting the provider. The messages stay in the queue, because each one is acknowledged only after a successful send. After 2 minutes the provider recovers, the breaker closes and the queue drains. Each message carries its idempotency key, so one that was sent just before a crash is not sent twice.

API and data model

POST /notifications  { userId, channel, type, payload, idempotencyKey }  ->  202 { id }
Table notifications: id, user_id, channel, type, status, idempotency_key (unique), created_at
Table outbox:        id, notification_id, published_at
Queues:              otp, transactional, marketing, one set per channel
Table preferences:   user_id, channel, type, opted_in, quiet_hours

Numbers and capacity

About 25 notifications per second on a normal day and about 8,300 pushes per second at the campaign peak. The OTP target is under 10 seconds from request to provider.

Common mistakes

  • Calling the provider inside the API request.
  • Using one shared queue. A campaign then delays OTPs.
  • Promising exactly-once delivery. Say at-least-once plus idempotency keys.
  • Checking preferences only when the campaign is created. A user who opts out later still gets a message.

Related problems

Design a notification system: walkthrough and solution