Notification System

Multi-Channel Alert Platform

Design a service that sends each notification on the channels a user chose, with templates, quiet hours, priorities, a rate limit, duplicate suppression, and retries with backoff.

Overview

A notification service takes a request such as 'tell Ana her order shipped' and delivers it on the right channels: email, push, SMS or an in-app inbox. It decides which channels from the user's preferences, renders the text from a template for each channel, and hands each message to a provider. Providers fail, users sleep, and the same event can be sent twice, so the service also retries, waits for quiet hours to end, and drops duplicates.

The problem tests three things. First, whether channels are pluggable: a new channel is a new class, and the dispatch code does not change. Second, whether you separate accepting a request from delivering it: send validates and queues, and workers deliver, so a slow provider never blocks the caller. Third, whether the delivery rules are precise: which types a user cannot opt out of, what urgent means, how many retries and how far apart.

A strong answer draws the pipeline, states the rules in a table, and writes a program whose tests show a retry succeeding, a retry giving up, a notification held for quiet hours and a duplicate ignored.

Scoping the problem

Ask these questions before you draw anything, because each answer becomes part of the design.

  • Which channels? Each one is a class. The usual answer is email, push and SMS, with in-app as a follow-up.
  • How do users choose? This is the preference model. Users pick channels per notification kind, and can opt out of a kind entirely.
  • Can a user opt out of everything? No, because some messages must arrive. Security messages always go to every channel the user has.
  • What is urgent? Urgent messages skip quiet hours and the rate limit. Usually that means security alerts.
  • What happens when a provider fails? This sets the retries and the final state. Retry with growing delays, then mark the message failed and keep it.
  • Can the caller send the same event twice? Yes, and duplicates annoy users, so an idempotency key makes the second send a no-op.
  • Is delivery synchronous? No, because that would add to the caller's latency. send queues, and workers deliver.

Functional requirements

  • send(user, kind, priority, data, now, key) picks channels from the user's preferences, renders a template per channel and queues one job per channel
  • Security notifications go to every channel the user has an address for, even if they opted out
  • Non-urgent notifications during quiet hours are held until quiet hours end
  • At most N non-urgent notifications per user per hour (5 in the demo)
  • A repeated idempotency key returns the first notification id and queues nothing
  • process(now) delivers due jobs by priority; a failed delivery is retried after 60 s and 120 s, then marked FAILED and dead-lettered
  • A status per notification and channel: QUEUED, RETRYING, SENT or FAILED

Non-functional requirements

  • A new channel is a new Channel subclass; NotificationService does not change
  • The caller never waits on a provider: send only validates and queues
  • Several workers may call process at once; each job is taken by exactly one of them
  • The service lock is never held during a provider call
  • Nothing is lost silently: every job ends SENT or FAILED with a dead-letter entry
  • Deterministic in tests: time is passed in, and a FlakyChannel fails on demand

Interview questions

  • A provider times out. Walk me through the retries.
  • Four workers call process() at once. How do you make sure a message is not sent twice?
  • How do you add a new channel, such as Slack?
  • How do you stop a user from getting 30 notifications in a minute?
  • How would you persist this and run it on many machines?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio