Design a Scalable Notification System: Multi-Channel Push, SMS, Email & Priority Queues
More Than Just Calling an Email API
Scalable Notification System Architecture
1. High-Level Multi-Channel Architecture
[Triggering Services] ──► [Notification Service] ──► [Channel Kafka Topics]
│ ├── Push Topic ──► Workers ──► APNs / FCM
(Auth + Templates + ├── SMS Topic ──► Workers ──► Twilio / Vonage
User Opt-Out DB) └── Email Topic──► Workers ──► AWS SES / SendGrid
Separating Channel Queues Prevents Head-of-Line Blocking
2. Core Reliability & Safety Mechanisms
1. Preventing Duplicate Alerts (Idempotency Key)
"Order Shipped" event twice.- Attach a deterministic
event_id(e.g.,notif:order_987:shipped) to every request. - Before dispatching to Twilio/FCM, workers execute an atomic
SET notif:{event_id} 1 NX EX 86400check in Redis. Duplicates are discarded silently.
2. Retry with Exponential Backoff & Dead-Letter Queues (DLQ)
5xx or 429 transient errors:- Retry failed messages with exponential backoff + jitter (
5s -> 25s -> 125s). - After $N$ failed attempts, move the payload to a Dead-Letter Queue and alert on-call engineers for inspection.
3. User Preferences, Opt-Out & Rate Limiting
- Centralized Opt-Out Check: Store per-user channel preferences (
email_marketing: false,sms_transactional: true) cached in Redis. Validate preferences before queueing. - Anti-Fatigue Throttling: Enforce per-user frequency caps (e.g., max 3 promotional pushes/day) so users don't mute or uninstall your app.
4. Delivery Tracking & Templates
- Templates: Store versioned Handlebars/Jinja templates centrally rather than hardcoding copy in microservices.
- Analytics Callbacks: Track
Sent -> Delivered -> Opened -> Clickedvia provider webhooks piped into an analytics log to audit deliverability rates.