Notification Service
One request, many channels — email, SMS, push — worked through the TOGAF ADM phases.
Notification service, the ADM way, in 30 seconds
- A (Vision): reliably deliver messages across channels; principles — decouple senders from channels, never double-send, degrade per channel.
- B (Business): capabilities — accept a notification request, route to the right channel(s), respect user preferences, report delivery.
- C (Data/App): a notification event; per-channel adapters; user preference + device-token store.
- D (Technology): a queue-fronted service fanning out to Email/SMS/Push adapters, each with retries, rate limits, and a circuit breaker.
- E–H: ship one channel first, add others; govern with delivery SLOs and per-channel budgets.
- Lesson: 'decouple senders from channels' in Phase A is exactly why Phase D puts a queue and adapters in the middle.
A company mailroom
Think of a company mailroom. Any team can drop off a message — 'tell this customer their order shipped' — and walk away. They don't need to know whether it'll go by post, by courier, or by fax; whether the courier is on strike today; or whether this customer asked to only ever be emailed. The mailroom figures all that out: it picks the right carrier, retries if one is down, honors 'do not call after 9pm,' and never sends the same letter twice. A notification service is that mailroom for your product.
'Just send an email' sounds trivial, which is the trap. The moment you have email and SMS and push, three flaky outside providers, user preferences and quiet hours, retries for transient failures, and a hard promise never to double-send — you have a sprawl of concerns. The whole art is hiding that sprawl behind one dead-simple door ('notify this user') so the teams dropping off messages never see it.
What makes it hard — the problem before the solution
- Many unreliable dependencies. Every channel is a third-party provider that will be slow or down sometimes — and one dead channel must not sink the others.
- At-least-once delivery means duplicates. The queue that makes it reliable can replay an event, so you need a guard or users get the same alert twice.
- Senders shouldn't know the details. A product team should call one API, not learn SMTP, an SMS gateway, and two push protocols.
- Not every message is equal. A password-reset is urgent; a marketing blast is not. They can't share one undifferentiated line.
Why the ADM fits
A notification service is deceptively simple ('just send an email'), but it quickly sprawls: multiple channels, flaky third-party providers, user preferences, retries, and no-double-send guarantees. Walking the ADM keeps the sprawl ordered — the decoupling principle set in Phase A makes the queue-and-adapters shape in Phase D inevitable.
Phase A — Architecture Vision
- Vision: let any part of the business send a notification to a user, reliably, over whatever channel fits.
- Stakeholders: product teams (want a simple 'notify this user' API), users (want relevant, non-duplicate messages), the business (wants reliable delivery and controlled cost).
- Principles: decouple senders from channels (teams shouldn't know about SMTP or SMS gateways); never double-send (idempotent); degrade per channel (if SMS is down, email still works).
Phase B — Business Architecture
- Value stream: an event occurs → decide who to notify and how → deliver → record the outcome.
- Accept request — a simple internal API: 'notify user U about event E.'
- Route to channels — pick email, SMS, push, or several, per the message type and user preferences.
- Respect preferences & quiet hours — don't spam; honor opt-outs.
- Report delivery — track sent/delivered/failed per channel.
Phase C — Information Systems Architecture (Data & Application)
Data architecture
- Notification event: id, user, type, payload, requested channels.
- User preferences: which channels a user allows, per notification type, plus quiet hours.
- Device/contact store: push tokens, verified email, phone number.
- Delivery log: per-channel status for reporting and debugging.
Application architecture
- A single notification service behind one internal API.
- A queue between accept and deliver, so senders get an instant ack.
- Per-channel adapters (Email, SMS, Push) that hide each provider's quirks behind a common interface.
Phase D — Technology Architecture (the core)
D1 — The fan-out shape
- Senders call one API; the service accepts and enqueues immediately, returning a fast ack.
- Workers pull events, apply preferences, and route to the needed channel adapters.
- Each adapter talks to its provider (SMTP/SendGrid, an SMS gateway, APNs/FCM) behind a common interface.
def handle_event(event):
prefs = preferences.get(event.user_id)
for channel in event.requested_channels: # e.g. ['email', 'sms', 'push']
if not prefs.allows(channel, event.type): # respect opt-outs + quiet hours
continue
adapter = ADAPTERS[channel] # common interface hides the provider
try:
adapter.send(event, prefs.contact(channel))
delivery_log.record(event.id, channel, "delivered")
except ProviderError:
# One channel failing must not stop the others (degrade per channel).
delivery_log.record(event.id, channel, "failed")
raise Retry(channel) # queue retries with backoffWhy a queue and adapters, and why not call providers directly?
The shortcut is obvious: when something happens, just call SendGrid (or Twilio) right there in the request that triggered it. No queue, no adapters, fewer moving parts. Two gaps make that shortcut collapse.
Without the queue, the sender's request now blocks on a third party. If the email provider takes three seconds — or times out — your checkout request takes three seconds or fails, because it's waiting on an unrelated email. Worse, if the provider is briefly down, the notification is simply lost; there's nothing holding it to retry. The queue turns 'send now, inline' into 'accept now, deliver soon,' so the sender gets an instant ack and the delivery can retry on its own schedule.
Without the adapters, every team that sends anything has to learn and re-implement each provider's API, and when you add WhatsApp next year you touch every one of those call sites. The adapter layer is the single place that knows SMTP, the SMS gateway, APNs/FCM — so senders stay blissfully ignorant and a new channel is one new file, not a company-wide migration. That's the decouple senders from channels principle from Phase A made concrete.
| Approach | What it buys | The gap it can't fill |
|---|---|---|
| Call provider inline | Fewest moving parts to build. | Sender blocks on a flaky third party; a provider blip loses the notification. |
| Queue, providers inline in senders | Async, non-blocking. | Every team still re-implements each provider and breaks together on changes. |
| Queue + per-channel adapters (chosen) | Async, decoupled, one place per provider, add channels freely. | More components to run — justified by reliability and clean growth. |
D2 — Reliability per channel
- Retries with backoff: providers fail transiently; retry, then dead-letter after N attempts (see Message queues).
- Circuit breaker per provider: if SMS is down, trip its breaker and fail fast — don't let it back up the whole pipeline (see Circuit breaker).
- Independent channels: email delivering has nothing to do with SMS failing — realizing the Phase A degrade per channel principle.
def deliver_with_retry(event, channel, adapter, contact, attempt=1):
if breaker[channel].is_open(): # provider already known-bad
raise ChannelDown(channel) # fail fast, free the worker
try:
adapter.send(event, contact)
breaker[channel].record_success()
except TransientError:
breaker[channel].record_failure()
if attempt >= MAX_ATTEMPTS:
dead_letter.put(event, channel) # give up -> inspect later, don't spin forever
return
delay = min(BASE * 2 ** attempt, MAX_DELAY) # 1s, 2s, 4s, 8s ... capped
requeue(event, channel, after=delay, attempt=attempt + 1)D3 — No double-sends (idempotency)
- Queues deliver at-least-once, so the same event can be processed twice.
- Give each notification a stable idempotency key; record what's been sent so a retry is a no-op (see Idempotency).
- Without this, a retry means a user gets the same alert twice — a real, visible bug.
def send_once(event, channel, adapter, contact):
key = f"{event.id}:{channel}" # stable key: same event+channel = same send
# SETNX = set-if-not-exists; returns False if the key already existed.
first_time = dedup_store.set_if_absent(key, "sent", ttl="7d")
if not first_time:
return # already sent on a prior at-least-once delivery
adapter.send(event, contact) # safe: runs exactly once per event+channelD4 — Rate, priority & preferences
- Rate limit per provider so you stay within their quotas and don't get throttled (see Rate limiting).
- Priority lanes: a password-reset (urgent) shouldn't sit behind a million marketing emails — separate queues or priorities.
- Apply preferences and quiet hours before sending; drop or defer what the user opted out of.
QUEUES = { # separate queues, drained in order
"urgent": Queue("notif.urgent"), # password resets, security alerts
"default": Queue("notif.default"), # order updates, mentions
"bulk": Queue("notif.bulk"), # marketing blasts
}
def enqueue(event):
lane = "urgent" if event.type in URGENT_TYPES else \
"bulk" if event.type in BULK_TYPES else "default"
QUEUES[lane].put(event)
# Workers always drain 'urgent' before 'default' before 'bulk',
# so a million-email campaign can't delay a single password reset.Scaling & bottlenecks — what breaks first
Because the service leans on outside providers, most of its failures originate outside your walls — the design's job is to keep those failures contained. Here's the order of trouble.
| Breaks first as you scale | The symptom | The fix |
|---|---|---|
| A provider slows or fails | Its adapter's calls pile up and consume all the workers. | Circuit breaker trips the bad provider; workers stay free for healthy channels. |
| A marketing blast floods the queue | Urgent messages (password resets) wait behind millions of promos. | Priority lanes / separate queues so urgent traffic jumps the backlog. |
| Provider rate limits hit | The provider throttles or bans you for sending too fast. | Rate-limit each adapter to stay under the provider's quota; smooth with the queue. |
| Dedup store unavailable | Can't check 'already sent?', so retries risk duplicates. | Fail safe toward not-resending for low-urgency; alert and degrade gracefully. |
The key insight: the circuit breaker and priority lanes exist to stop one bad input from becoming a total failure. A dead SMS provider should cost you SMS, not email and push too — and a flood of marketing mail should never delay a password reset. Containment, not perfection, is the goal.
Phases E & F — Migration Planning
- Ship one channel first (say push) end to end, with retries and idempotency.
- Add email and SMS as new adapters — no change to senders, thanks to the decoupling.
- Add preferences and priority lanes once the core delivery loop is solid.
Phases G & H — Governance & Change
- Governance (G): SLOs on delivery latency per channel and on delivery success rate; per-provider cost budgets.
- Change (H): adding a new channel (e.g. WhatsApp) is a new adapter behind the same API — a bounded change, not a redesign.
A notification service is a masterclass in decoupling and resilience — many flaky dependencies hidden behind one clean, reliable interface.
Why it matters
- It's the canonical fan-out-with-adapters pattern reused across countless systems.
- It forces per-dependency resilience — retries, breakers, rate limits — in one place.
- It shows how decoupling lets you add capabilities (channels) without touching callers.
If ignored
- Senders coupled to providers means every team re-implements SMTP/SMS and breaks together.
- No idempotency means retries spam users with duplicate alerts.
- No per-provider breaker means one dead channel backs up and sinks all notifications.
Where it fits
- Phase D holds the queue, workers, channel adapters, and per-provider resilience.
- Phase A's decoupling and no-double-send principles drive those D choices.
- Sits beside the interview framework as a second way to structure the answer.
Related topics
- Message queues: the async backbone and retry/DLQ mechanism
- Idempotency: prevents duplicate notifications on retry
- Circuit breaker: isolates a failing channel provider
- Rate limiting: keeps each provider within quota
Interview lens
Questions it unlocks
- Design a notification/alerting service.
- How do you avoid sending the same notification twice?
- How do you keep one flaky channel from breaking the rest?
60-second answer
I frame it as an ADM walk. Phase A's principles — decouple senders from channels, never double-send, degrade per channel — pre-decide the shape. Phase B names accept, route, respect preferences, and report. Phase C models a notification event, preferences, device tokens, and a delivery log. Phase D is the core: one API that enqueues instantly, workers fanning out to per-channel adapters, each with retries and a dead-letter queue, a circuit breaker per provider so a dead channel fails fast, idempotency keys to prevent duplicate sends, per-provider rate limits, and priority lanes so urgent messages skip the marketing backlog. E–H ship one channel first and govern with delivery SLOs. Each Phase D choice traces back to a Phase A principle.
Common traps
- Coupling senders directly to channel providers instead of hiding them behind adapters.
- Forgetting idempotency and double-sending on queue retries.
- Letting one failing provider back up and sink the whole pipeline.