Design a Notification System
A multi-channel notification platform (push, SMS, email, in-app) used by every product team — event ingestion, user preferences and quiet hours, templating and localisation, priority queues per channel, provider integrations with retries and failover, deduplication, rate limiting and digests, scheduled and bulk campaigns, delivery tracking and analytics.
1. Requirements
Functional
- Channels: mobile push (iOS/Android), SMS, email, in-app inbox (and web push).
- Triggered by services (transactional) and by campaigns (bulk marketing), immediate or scheduled.
- Per-user preferences, quiet hours, unsubscribes; templates with localisation.
- Delivery status and analytics (sent, delivered, opened, clicked).
Non-functional
- 1B notifications/day; spikes of 10× during campaigns or incidents.
- Transactional p99 latency < 5 s end-to-end (OTP!).
- At-least-once with dedup → effectively once; no lost transactional notifications.
- Extensible: adding a channel or provider shouldn't touch producers.
2. Estimation
| Quantity | Value |
|---|---|
| Average rate | 1B/day ≈ 11.6k/s |
| Peak | ~100k/s |
| Status events | ~3 per notification → ~35k/s avg writes |
| Storage | 1 KB per notification + status → ~1 TB/day (keep 30–90 days hot) |
3. API
POST /v1/notifications
Idempotency-Key: order_8812:shipped
{
"recipients": [{"user_id": "u_17"}],
"type": "order_shipped",
"priority": "high",
"data": {"order_id": "8812", "eta": "2026-10-09"},
"channels": ["push", "email"], // optional override; default from type config
"send_at": null
}
202 Accepted {"notification_id": "n_01J…", "status": "queued"}
4. High-level design
5. Data model
CREATE TABLE notification (
id TEXT PRIMARY KEY, -- ULID
idempotency_key TEXT UNIQUE,
user_id BIGINT NOT NULL,
type TEXT NOT NULL,
payload JSONB NOT NULL,
priority TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
CREATE TABLE delivery (
notification_id TEXT REFERENCES notification(id),
channel TEXT, -- push | sms | email | inapp
status TEXT, -- queued | sent | delivered | failed | opened
provider TEXT,
attempts INT DEFAULT 0,
updated_at TIMESTAMPTZ,
PRIMARY KEY (notification_id, channel)
);
-- preferences: (user_id, category, channel) -> enabled; quiet_hours; timezone
-- devices: (user_id, device_id) -> platform, push_token, last_seen
At very high volume the delivery log moves to Cassandra/DynamoDB (write-heavy, TTL) and analytics to a columnar store.
6. Deep dives
Routing logic
def route(n: Notification):
if dedupe.seen(n.idempotency_key):
return
cfg = notification_types[n.type] # default channels, category, priority
prefs = preferences.get(n.user_id) # cached
channels = [c for c in (n.channels or cfg.channels) if prefs.allows(cfg.category, c)]
if cfg.category == "marketing" and caps.exceeded(n.user_id, "marketing", per_day=3):
return
for ch in channels:
when = quiet_hours.next_allowed(prefs, now()) if cfg.respect_quiet_hours else now()
msg = templates.render(cfg.template[ch], locale=prefs.locale, data=n.payload)
queues[ch][n.priority].enqueue(msg, deliver_at=when)
Provider integration
| Channel | Provider facts |
|---|---|
| iOS push | APNs over HTTP/2; device tokens change; invalid token → 410 → delete |
| Android push | FCM HTTP v1; collapse keys; topic messaging |
| SMS | Twilio/Vonage; per-country regulations, sender IDs, cost per message; OTP via dedicated routes |
| SES/SendGrid; SPF/DKIM/DMARC, IP warm-up, bounce and complaint handling, unsubscribe headers | |
| In-app | write to an inbox table + real-time push over WebSocket if online |
Use a circuit breaker per provider and fail over (e.g., SMS via a second vendor) when error rates spike.
Scheduling and digests
- Scheduled sends: store with
send_at; a scheduler (time-bucketed table or delay queue) releases them — see job scheduler. - Digests: accumulate events per user/type in a window (Redis list with TTL); a timer flushes one aggregated notification.
Rate limiting
Per-user (fatigue), per-provider (contractual limits), per-tenant (fairness among internal producers). Bulk queues drain at a configured rate.
7. Trade-offs
| Concern | Decision |
|---|---|
| Latency vs throughput | separate priority lanes; transactional never queues behind marketing |
| Exactly-once | idempotency keys + per-channel send state (effectively once) |
| Preferences consistency | cached with short TTL + invalidation on change; legal unsubscribes applied immediately |
| Provider dependence | multi-provider with failover; abstraction layer per channel |
| Observability | per-type funnel: requested → sent → delivered → opened |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.