Notification system
Not climbed yetModel answer
Services publish an event ("order 123 shipped") to a notification service rather than calling SMS or email providers themselves.
The notification service looks up the user's preferences (which channels, quiet hours, language), renders a template per channel, and puts one job per channel on its own queue. Channel workers send through the providers (FCM/APNs for push, an SMS gateway, an email service) and record each attempt's status.
Separate queues per channel mean a slow email provider cannot delay SMS, and each channel scales and retries on its own.
Model answer
They wait in the SMS queue and are retried with backoff; nothing is lost because the queue holds them.
Better: configure a fallback provider and switch automatically when the primary's error rate crosses a threshold (a circuit breaker per provider).
Give messages an expiry. An OTP or "your driver has arrived" is worthless after a few minutes and must be dropped, not delivered an hour late. A shipping update can still go out.
Optionally fall back to another channel: if SMS fails for a delivery alert, send a push.
Model answer
Deduplicate in the notification service, not just by hoping producers behave. Give every event an id (or derive a key such as
order123:shipped), and before sending, insert(dedup_key, channel)into a table with a unique constraint, orSET NXin Redis with a TTL of a day. Only the first insert sends.Do the same at the send step with the provider's idempotency key where supported, because the worker itself can retry after a crash.
Keep a cap per user too ("no more than 5 marketing messages a day"), which also limits the damage of bugs nobody thought of.
Model answer
- Build the audience ahead of time: compute the list of user ids before 9am, not with 10 million database queries at 9:00.
- Fan out through the queue in batches: a job splits the list into chunks of, say, 1,000 users, and workers process chunks.
- Respect provider rate limits with a shared token bucket per provider, and keep marketing traffic on a separate, lower-priority queue, so OTPs and order updates are not stuck behind it.
- Spread it out: sending over 30 to 60 minutes is usually fine for marketing and avoids the app spike when 10 million people tap at once.
Also check what happens next: 10 million push notifications can mean a million app opens in minutes. Warn the teams behind the app's home screen.
