Part 7 of 8 · 2 min read
Trade-offs
Priorities, ordering and a follow-up.
Trade-offs and what to say
Priority. Not all notifications are equal. A password reset code must go before a marketing campaign. If both share a queue, the campaign delays the code. Give urgent messages their own queue or a priority lane, and let workers take from the urgent one first.
Ordering. Queues are not strictly in order across workers, and retries reorder messages. For notifications, order rarely matters, as the book allows. If it does, such as "payment started" then "payment done", order by key in a single partition (Level 6).
Cost. Providers charge per message, and SMS is the most costly. A rule such as "try push first, and fall back to SMS only if the user has no app" saves money. Batching emails to the provider lowers call counts.
Provider failure. Use more than one provider for a channel, and fail over when the main one is down (a circuit breaker, Level 8). This is the extensibility the book asks for.
Scheduled sends. Services may schedule a notification for later. Keep scheduled events in a store ordered by time, and have a job move due ones into the right queue.
Interview angle
Answer in your head first, then open it.
What are the pieces?
Services, notification servers, cache and database, a queue per channel, workers, third-party providers, a notification log.
Why queues?
They decouple parts, absorb bursts, and keep a fault in one provider from affecting others.
How do you avoid losing messages?
Persist events in a log, retry from the queue, and alert on repeated failure.
Can you deliver exactly once?
No. Aim for at-least-once and dedupe by event id.
How do you avoid spamming users?
Opt-in settings and per-user rate limits.
One follow-up to try. "A password reset code must arrive in under 5 seconds even during a marketing blast." A good reply: put reset codes on a separate high-priority queue and worker pool, with its own provider quota, so a campaign cannot delay it. Set an alarm on that queue's age, not just its depth, and send reset codes by the fastest channel first.
Check yourself: 1. Why give urgent messages their own queue? 2. How can SMS cost be cut? 3. What alarm fits the reset-code queue better than depth?
Words you will meet
| Word | Meaning |
|---|---|
| Notification | A message that alerts a user to something |
| Device token | A unique id of one phone for push |
| APNS and FCM | Apple's and Google's push services |
| Payload | The data carried in a message |
| Queue depth | How many messages are waiting |
| Dedupe | Dropping a repeat of a message already handled |
| Opt-in and opt-out | A user's choice to receive or not receive messages |
| Template | A reusable message layout with parameters |