Courses System design · Part 6 · Case study 2
Case Study: A Notification System (SMS, Email, Push)
An online shop wants to tell customers about important moments: “your order is placed”, “your parcel has shipped”, “your login code is 482917”. Some messages go by SMS, some by email, some as phone notifications (push). The actual sending is done by outside services (an SMS gateway, an email service, the Android and iOS push services). Our job is to get each message to them correctly and on time.
1. Requirements
Functional:
- Other services (orders, shipping, login) say “send this message to this user”.
- Each message goes through one or more channels: SMS, email, push.
- A user can turn some messages off (for example marketing).
Non-functional:
- A login code must arrive within seconds. A marketing message can be minutes late.
- Important messages must not be lost.
- Duplicates are bad (two SMS cost twice and annoy the customer).
- One outside service failing must not stop the others.
2. Numbers
- 500,000 orders a day, about 3 messages each (placed, shipped, delivered): 1.5 million, plus 500,000 login codes: about 2 million messages a day, about 23 a second.
- One marketing campaign: 3 million emails that marketing wants to send “right now”.
- An outside limit: say the SMS gateway accepts at most 100 messages a second from us.
The result: the normal load is low, but the waves (campaigns, sales) are very big and larger than the outside services can take. So we need queues and backpressure.
3. API
POST /notify {userId, template: "order_shipped", data: {...}, priority: "high"|"normal"|"bulk", key: "order-881-shipped"}
key is an idempotency key: if the order service sends the same request twice, only one message goes out.
4. Data model
- User settings: which channels are on for which kind of message, phone number, email, push tokens of the user’s phones.
- Templates: the message text for each kind and each language.
- Delivery log: each message, channel, status (queued, sent, delivered, failed) and its idempotency key.
5. High-level picture
- The notification service receives the request, checks the idempotency key, reads the user’s settings and builds the text from the template.
- For each channel it puts a message on that channel’s queue: an SMS queue, an email queue, a push queue. Each queue has two priorities: “urgent” and “bulk”.
- Workers for each channel take from their own queue and call the outside service (message queue).
- The result is written to the delivery log.
Why a queue per channel? If the email service is slow or down, only the email queue fills up. SMS and login codes keep going without delay.
Why a queue per priority? If 3 million marketing emails are at the front of the queue, one user’s login code must not wait behind them. Workers always empty the urgent queue first.
6. Deep dive: when an outside service fails
Outside services sometimes return errors or do not answer. Three tools from this course:
- Retries with jitter: a worker resends a failed message with a doubling wait plus a random part, so that when the service comes back, the workers do not all rush at it together. After a few attempts (say 5), the message goes to a dead-letter queue for someone to look at.
- Circuit breaker: if the SMS gateway fails again and again, the workers stop calling it for a while. Messages wait in the queue, and workers do not wait in vain. For login codes, we can switch to a second SMS gateway.
- A rate limiter on our side: together, the SMS workers send no more than 100 messages a second, because the gateway refuses more.
Duplicates
With retries, duplicates cannot be avoided: the gateway sent the SMS, but its answer never reached us. The fix is the same at least once + idempotency key: every message has an ID, and if the outside service supports an idempotency key, we send it. If it does not, we at least ask the service for the message’s status before we retry.
The numbers of one campaign
3 million emails at an allowed 500 emails a second: 3,000,000 ÷ 500 = 6,000 seconds, about 1 hour 40 minutes. Tell the marketing team this number from the start: “right now” means within two hours. The queue holds the wave and empties it step by step.
7. Trade-offs
- Speed versus cost: SMS is expensive. Push is almost free but does not always arrive. For an important message, send push first and, if it is not read within a minute, send an SMS.
- Exactly once in practice: idempotency keys and the delivery log bring us close, but with outside services a full guarantee is not possible.
- Respect for the user: quiet hours (no marketing messages after 10 pm, for example) and an unsubscribe link in every marketing email. These are core requirements, not extras.
Labs used
Message queue · Backpressure · Retries · Circuit breaker · Rate limiter