Designing a Notification Service

Difficulty: Intermediate Patterns: Adapter, Chain of Responsibility, Observer Asked at: Amazon, Flipkart, Swiggy, Razorpay


Every service in the company wants to tell a user something. Your order shipped, your OTP is 441908, your rider is two minutes away, here is 40% off pizza. They all call one method – notify(user, template, params) – and behind it sit three vendors who agree on nothing: an SMS gateway returning a status string, an email provider throwing checked exceptions, a push service reporting through callbacks.

Wrapping those three is the part that looks like the problem, and it is already solved. One NotificationChannel interface, three adapters, and every vendor’s failure collapses into one SendResult – that derivation lives in the Adapter pattern lesson, using these exact classes, and this page does not repeat it. This page is the service around those adapters, which is where the interview actually happens.

Three things are hard there. The first is that retry and fallback are different decisions and need different inputs. A send fails; do you call the same channel again or move to the next one? Retrying a throttled SMS is right and retrying a dead device token is pointless, because the second call fails for the same reason as the first while burning quota and a few hundred milliseconds of the user’s patience. The adapter is the only class that can tell those apart, and it must not be the class that acts on it.

The second is that preferences are a hard gate, not a filter you can forget. A user who unsubscribed from marketing must not receive marketing over any channel, including the one you add next quarter, while an OTP must still arrive at 3am through the quiet hours that silence everything else. That means notifications carry a category, preferences are per category per channel, and the check runs in exactly one place. Put it in each adapter and the fourth adapter someone writes in a hurry is a compliance incident.

The third is that the same notification must not be sent twice. Retries, at-least-once queue delivery and a duplicate upstream event all produce double sends, and a user who gets the same OTP text four times stops trusting the product. The fix is a deduplication key with a time window, claimed before the first vendor call – not after it.

💡 Transient failure = one that may succeed if you make the same call again, such as a throttle, a timeout or a 5xx. Permanent and bad-recipient failures must not be retried, because the retry is a guaranteed second failure. Learn more →

💡 Fallback, here called channel escalation = giving up on one channel and trying the next one down an ordered list. Push, then SMS, then email, stopping at the first that delivers. Learn more →

💡 Quiet hours = a per-user window, in the user’s own time zone, during which non-urgent notifications are held back. The window is local to the recipient, which is why a time zone travels with them rather than with the server.

💡 Idempotent = safe to apply twice. “Send this notification” has to be an operation whose second application sends nothing, because the thing calling you will retry. Learn more →


Functional Requirements

  1. Multi-channel send – push, SMS and email, each behind one interface, with no vendor type visible above the adapter.
  2. Channel fallback – an ordered chain tried until one channel delivers, with the whole attempt trail retained.
  3. Per-channel retry – a transient failure is retried on the same channel with backoff; a permanent or bad-recipient failure escalates immediately.
  4. Preferences – per category per channel opt-out, plus per-user quiet hours, enforced at one gate before dispatch.
  5. Deduplication – a caller-supplied event id makes repeated submissions of the same notification produce exactly one send.
  6. Templating – a templateId plus params renders a channel-appropriate body, with SMS truncated to one segment.
  7. Delivery status – subscribers are notified of every outcome, and a notification that exhausts every channel lands in a dead-letter store.

Non-Functional Requirements

  1. No duplicate sends under at-least-once delivery – the dedupe key is claimed atomically before the first vendor call, so two workers handed the same queue message produce one send and one DUPLICATE. We choose at-least-once plus dedupe over exactly-once, because exactly-once across a vendor we do not control does not exist.
  2. Bounded work per notification – at most maxAttemptsPerChannel calls per channel and at most one pass down the chain, so the worst case is a fixed number of vendor calls rather than something that grows with how badly the vendors are behaving.
  3. A sick vendor cannot stall the service – every attempt is timeout-bounded, backoff is exponential with jitter, and a per-channel circuit breaker skips a channel that is failing rather than waiting on it.
  4. The gate is unbypassable and O(1) – one evaluate call per notification, reading an EnumMap of EnumSets, and no code path to a vendor exists that does not pass through it.