Every product grows a notification system: order updates by push, receipts by email, one-time passwords by SMS. Dozens of services want to notify users, so it pays to build one shared service that handles preferences, templates, retries and provider quirks in one place.
The pipeline
- API: producers send an event, not a finished message: "order 812 shipped for user 42", with an idempotency key. The API stores the key and rejects repeats.
- Durable log: accepted events go to Kafka, so a slow provider never blocks the services calling the API.
- Router: loads the user's preferences and contact details, chooses channels, renders templates, and applies quiet hours and frequency caps.
- Channel queues and workers: one queue per channel, so a slow SMS provider can't delay push notifications. Workers call providers within each provider's rate limits.
- Status store: one record per notification and channel attempt: queued, sent, delivered, opened, failed.
Give one-time passwords and security alerts their own high-priority queue. Marketing can wait; a login code can't.
Deduplicating across channels
Duplicates come from two places: producers sending the same event twice, and the router choosing several channels for one event.
- Producer duplicates: dedupe on the idempotency key (or on
user_id + event_type + entity_id) with a unique constraint or a RedisSET NXwith a TTL. - Channel policy: decide per event type, not per channel. For example: send push first; if it isn't opened within 10 minutes, send email; never send SMS for the same event. One notification record per event, with its channel attempts underneath, makes that easy to enforce.
- Per-channel key:
(notification_id, channel)is unique in the status store, so a worker that picks up the same job twice finds it already sent.
Retries without double notifying
- Record the attempt as sending before calling the provider and sent afterwards. A retry checks that state first.
- Retry only errors that can succeed later: timeouts, HTTP 5xx and 429. Don't retry 4xx errors like an invalid phone number or an unregistered device token. Use exponential backoff with jitter, cap the attempts, then send the job to a dead-letter queue.
- The hard case is a timeout after the provider has already accepted the message. You can't know whether it was sent. Where providers support it, a collapse key (APNs
apns-collapse-id, FCMcollapse_key) makes the device replace duplicates instead of showing two. For the rest, accept a rare duplicate rather than risk never sending. - Producers should publish through a transactional outbox so "order shipped" is only announced if the order update committed.
The 10-million-follower broadcast
"Notify everyone who follows this account" must not become one request with 10 million recipients. A broadcast job pages through followers in batches of a few thousand and enqueues those batches at a rate the providers allow, often spread over minutes. Low-priority queues absorb it without delaying transactional messages. Collapse repeated broadcasts ("12 people liked your post") rather than sending one notification each.
Preferences and the law
Every channel needs per-category opt-outs, an unsubscribe link in marketing email, quiet hours in the user's time zone, and a cap on messages per day. These belong in the router, so no producing service can bypass them.
Interview checklist
- One API taking events plus idempotency keys, then a durable log.
- Separate queues per channel and per priority.
- A channel policy and unique keys to prevent duplicates.
- Retry rules: what's retryable, backoff, dead-letter queue, collapse keys.
- Batch big broadcasts, and enforce preferences centrally.
Level 9: Retry Storm
Put this into practice against live traffic. Your progress in the game is saved in your browser.
Play level 9Further reading
- Alex Xu, System Design Interview: An Insider's Guide, volumes 1 and 2. Worked case studies in interview format.
- Martin Kleppmann, Designing Data-Intensive Applications (O'Reilly). The standard book on replication, partitioning and consistency.
- Site Reliability Engineering, Google's free book on running reliable systems.
- The Amazon Builders' Library: practical articles on timeouts, retries, caching and load shedding.
- The System Design Primer: a broad, community-maintained overview.