Case Study 14 โ Notification System (Push / Email / SMS)
Design a multi-channel notification platform that delivers push, email, and SMS messages reliably at scale โ used by many internal product teams (orders, social, marketing).
1. Problem
When something happens in the product ("your ticket is confirmed", "friend liked your photo"), the system must notify the right user on the right channel, respect preferences and quiet hours, retry failures, and provide delivery status โ without slowing down the main application.
2. Requirements
Functional (MVP)
- Send notifications via push (mobile), email, and SMS
- Template-based messages with variables (
{{userName}},{{orderId}}) - User preferences: opt-in/out per channel and category (marketing vs transactional)
- Priority levels: transactional (high) vs marketing (low)
- Delivery status: queued, sent, delivered, failed, bounced
- Retry transient failures with backoff
- Idempotent send โ same event must not spam duplicate notifications
- Admin API to register templates and trigger test sends
Out of scope (initially)
- In-app notification center UI (can consume same events later)
- A/B testing subject lines at scale
- Two-way SMS conversations
- Advanced ML send-time optimization
- Full campaign builder with segmentation UI
Non-functional
- Decouple from core apps โ async via queue
- High throughput: millions of notifications/hour
- At-least-once delivery with dedup (exactly-once illusion to user)
- Provider failover (SendGrid down โ backup)
- Compliance: CAN-SPAM, GDPR unsubscribe, SMS opt-in
- Observability: per-channel success rates, latency percentiles
3. Back-of-the-envelope estimates
These numbers are rough order-of-magnitude math โ not a capacity plan. In interviews, the goal is to show you know what to count and which resource breaks first. A useful shortcut: 1 QPS sustained โ 86,400 operations/day. Notification systems often burst (marketing campaigns, breaking news) โ peak 2โ5ร average is conservative; campaigns can spike higher.
Why we estimate
Notifications must never block checkout or social APIs. Estimates tell us:
- Why the API enqueues and returns
202 Acceptedinstead of sending synchronously - Why separate queues per channel matter (provider limits differ wildly)
- How delivery logs drive storage at 90-day retention
Assumptions
| Assumption | Value | Why it matters |
|---|---|---|
| Registered users | 100M | User base |
| Notifications per user per day | 5 | Mix of transactional + marketing |
| Channel split | 70% push, 20% email, 10% SMS | Different payloads and provider caps |
| Email HTML size | ~50 KB | Template/blob storage |
| Push payload | ~4 KB | FCM/APNs |
| SMS length | ~160 chars | Small but expensive |
| Delivery log retention | 90 days | Audit and analytics |
Step A โ Traffic (QPS) with labeled arithmetic
Total notifications per day:
Users = 100,000,000
Notifications/user/day = 5
Daily notifications = 100M ร 5 = 500,000,000/day
Average enqueue QPS (API accepts and queues):
Average QPS = 500,000,000 รท 86,400
โ 5,787 notifications/second
โ 5,800/s (round)
Peak QPS (3ร average โ conservative for campaigns):
Peak QPS โ 5,800 ร 3 โ 17,400/s
โ 17,000/s
Per-channel peak QPS (same total, split by channel):
Push peak = 17,000 ร 70% โ 11,900/s
Email peak = 17,000 ร 20% โ 3,400/s
SMS peak = 17,000 ร 10% โ 1,700/s
Worker send rate vs provider limits:
Email: typical ESP limit 100โ1,000/s per account
โ need multiple accounts / IP pools for 3,400/s peak
Push: FCM/APNs can handle 100k+/s with batching โ not the bottleneck
SMS: ~$0.01/msg ร 500M/day ร 10% = $500k/day at scale โ budget caps matter more than QPS
Step B โ Storage
Delivery log metadata (90 days):
Notifications/day = 500M
Retention = 90 days
Total log rows = 500M ร 90 = 45 billion rows
Bytes per row โ 300 B (IDs, status, timestamps, channel, provider ref)
Raw metadata = 45B ร 300 B โ 13.5 TB
With indexes โ 15โ20 TB
Email templates and rendered bodies:
Templates in DB/S3 โ small catalog (thousands), not 500M copies
Rendered HTML stored briefly or not at all โ provider holds copy
User preferences + device tokens:
100M users ร ~200 B โ 20 GB โ trivial compared to delivery logs
Step C โ Bandwidth
Outbound payload at peak (rough):
Push: 11,900/s ร 4 KB โ 48 MB/s
Email: 3,400/s ร 50 KB โ 170 MB/s โ largest channel by bytes
SMS: 1,700/s ร 160 B โ 0.27 MB/s
Total egress โ 220 MB/s peak (dominated by email HTML)
Email bandwidth is why templates live in blob store and workers stream from cache.
Step D โ Read:write ratio table
| Operation | Type | QPS | Notes |
|---|---|---|---|
POST /notifications (enqueue) | Write | ~5,800 avg; ~17k peak | Must return in ms |
| Preference lookup | Read | ~5,800/s | Filter channels |
| Dedup / idempotency check | Read | ~5,800/s | Prevent duplicate sends |
| Worker โ provider send | Write (external) | Per channel | Rate-limited |
| Delivery log update | Write | ~5,800/s | Status tracking |
| Webhook status update | Write | Lower | Bounces, delivery receipts |
GET /notifications/:id | Read | Low | Status polling |
Ratio: enqueue path is write-heavy; product APIs should only write once to queue, not call SendGrid synchronously.
What the numbers tell us
- Core API enqueues only โ 5,800/s is easy for a stateless API; workers absorb provider latency
- Separate queues for push, email, SMS โ slow email must not block fast push
- Email provider limits (100โ1k/s per account) force multiple ESP accounts or bulk queue throttling
- 13+ TB of delivery logs โ partition by date, tier to cold storage after 90 days
- Idempotency keys essential โ upstream retries at 5,800/s would duplicate without dedup
- SMS cost often drives product policy (opt-in, caps) more than architecture
Common mistake for this problem
Sending synchronously in the API (POST /notifications waits for SendGrid). At 17k/s peak, that couples product latency to provider SLA and causes timeouts. Correct pattern: validate โ dedup โ enqueue โ 202 Accepted. Another mistake: one queue for all channels โ a slow SMS provider stalls push delivery.
4. High-Level Design (HLD)
Components
| Component | Role |
|---|---|
| Notification API | Validate, dedup, route to queues |
| Template Store | HTML/text templates per channel + locale |
| Preferences Service | User channel/category settings, quiet hours |
| Priority Queues | Separate queues: critical, normal, bulk marketing |
| Channel Workers | Pull jobs, render template, call provider |
| Delivery Log | Audit trail, status, provider message IDs |
| Webhook Handler | Bounces, complaints, delivery receipts |
| Scheduler | Delayed sends, digest batching, quiet hours resume |
| Rate Limiter | Per-provider token buckets |
Flows
Send notification (async)
- Order Service:
POST /notificationswith{ userId, templateId, data, channels } - API checks idempotency key / event ID โ skip if already processed
- Load user preferences โ filter channels (no marketing email if opted out)
- Render preview hash; enqueue one job per channel
- Return
202 AcceptedwithnotificationIdimmediately
Worker delivery
- Worker pulls job from priority queue
- Rate limiter permits call to provider
- Fetch template โ substitute variables โ send
- Update log:
SENT+ provider message ID - On failure: retry with backoff; dead-letter after N tries
Webhook update
- SendGrid POST bounce event โ Webhook Handler
- Match provider message ID โ update log
BOUNCED - Disable bad email addresses; alert if spike
Trade-offs
| Choice | Pros | Cons |
|---|---|---|
| Single queue vs per-channel queues | One queue simpler | Slow SMS blocks fast push โ separate queues |
| Sync send in API | Immediate error to caller | Timeouts, coupling, poor scale |
| Template in DB vs object storage | DB easy for small HTML | Large emails โ S3 + cache |
| Fan-out on enqueue vs worker | Faster worker | Heavier API โ usually OK at enqueue |
| At-least-once + dedup | Reliable | Must design idempotency keys |
5. Low-Level Design (LLD)
APIs
POST /api/v1/notifications
Headers: Idempotency-Key: order-123-confirmed
Body: {
"userId": "u-456",
"templateId": "ORDER_CONFIRMED",
"category": "TRANSACTIONAL",
"channels": ["push", "email"],
"data": { "orderId": "ord-789", "total": "$49.99" },
"priority": "HIGH"
}
โ 202 { "notificationId": "n-abc", "status": "QUEUED" }
GET /api/v1/notifications/:notificationId
โ { status, channelStatuses: [{ channel, state, sentAt, error }] }
GET /api/v1/users/:userId/preferences
โ { email: { marketing: false, transactional: true }, push: {...}, sms: {...} }
PUT /api/v1/users/:userId/preferences
Body: { email: { marketing: false } }
POST /internal/webhooks/sendgrid
โ 200 # bounce, open, click events
POST /api/v1/templates
Body: { templateId, channel, locale, subject, bodyHtml, bodyText }
Schema
notifications (
notification_id UUID PRIMARY KEY,
idempotency_key VARCHAR(128) UNIQUE NOT NULL,
user_id UUID NOT NULL,
template_id VARCHAR(64) NOT NULL,
category VARCHAR(30), -- TRANSACTIONAL, MARKETING, ...
priority VARCHAR(20),
payload JSONB,
status VARCHAR(20), -- QUEUED, PROCESSING, COMPLETED, FAILED
created_at TIMESTAMPTZ NOT NULL
)
notification_deliveries (
delivery_id UUID PRIMARY KEY,
notification_id UUID REFERENCES notifications,
channel VARCHAR(20), -- PUSH, EMAIL, SMS
status VARCHAR(20), -- QUEUED, SENT, DELIVERED, FAILED, BOUNCED
provider VARCHAR(50),
provider_msg_id TEXT,
attempts INT DEFAULT 0,
last_error TEXT,
sent_at TIMESTAMPTZ,
delivered_at TIMESTAMPTZ,
UNIQUE (notification_id, channel)
)
user_preferences (
user_id UUID PRIMARY KEY,
email_marketing BOOLEAN DEFAULT FALSE,
email_transactional BOOLEAN DEFAULT TRUE,
push_enabled BOOLEAN DEFAULT TRUE,
sms_enabled BOOLEAN DEFAULT FALSE,
quiet_hours_start TIME NULL,
quiet_hours_end TIME NULL,
timezone VARCHAR(50)
)
templates (
template_id VARCHAR(64),
channel VARCHAR(20),
locale VARCHAR(10),
subject TEXT,
body_html TEXT,
body_text TEXT,
PRIMARY KEY (template_id, channel, locale)
)
device_tokens (
user_id UUID,
platform VARCHAR(10), -- IOS, ANDROID
token TEXT,
updated_at TIMESTAMPTZ,
PRIMARY KEY (user_id, platform, token)
)
Modules
NotificationController / NotificationService
PreferenceService / TemplateRenderer
DedupStore / IdempotencyFilter
QueueProducer / PushWorker / EmailWorker / SmsWorker
ProviderClients (FcmClient, SendGridClient, TwilioClient)
RateLimiter / RetryPolicy / DeadLetterHandler
WebhookController / DeliveryStatusUpdater
QuietHoursScheduler
Key algorithm โ enqueue with idempotency
function sendNotification(request, idempotencyKey):
existing = repo.findByIdempotencyKey(idempotencyKey)
if existing: return existing # safe retry from Order Service
prefs = preferenceService.get(request.userId)
channels = filterChannels(request.channels, prefs, request.category)
if channels empty: return SKIPPED
if inQuietHours(prefs) and request.priority != HIGH:
scheduler.schedule(request, afterQuietHours)
return SCHEDULED
notification = repo.create(request, idempotencyKey, QUEUED)
for channel in channels:
delivery = repo.createDelivery(notification.id, channel, QUEUED)
queueFor(channel, request.priority).push({
deliveryId: delivery.id,
notificationId: notification.id,
userId: request.userId,
templateId: request.templateId,
data: request.data
})
return notification
Key algorithm โ worker with retry
function processDeliveryJob(job):
delivery = repo.get(job.deliveryId)
if delivery.status in [SENT, DELIVERED]: return # already done
if not rateLimiter.acquire(job.channel, job.provider):
queue.requeueWithDelay(job, delayMs=500)
return
try:
rendered = templateRenderer.render(job.templateId, job.channel, job.data, userLocale)
address = resolveRecipient(job.userId, job.channel) # email, phone, device tokens
if job.channel == PUSH:
result = fcm.send(address.tokens, rendered)
elif job.channel == EMAIL:
result = sendgrid.send(address.email, rendered)
elif job.channel == SMS:
result = twilio.send(address.phone, rendered.text)
repo.updateDelivery(job.deliveryId, SENT, result.providerMsgId)
catch RetryableError as e:
delivery.attempts += 1
if delivery.attempts >= MAX_ATTEMPTS:
repo.updateDelivery(FAILED, e)
deadLetter.push(job)
else:
delay = exponentialBackoff(delivery.attempts)
queue.requeueWithDelay(job, delay)
catch PermanentError as e:
repo.updateDelivery(FAILED, e) # invalid token, unsubscribed
Key algorithm โ template rendering
function render(templateId, channel, data, locale):
tmpl = templateStore.get(templateId, channel, locale)
subject = substitute(tmpl.subject, data) # {{orderId}} โ ord-789
body = substitute(tmpl.body_html, data)
validateNoMissingVars(body)
return { subject, body }
Use a sandbox: no arbitrary code execution in templates (avoid eval).
Concurrency & correctness
| Concern | Approach |
|---|---|
| Duplicate notifications | idempotency_key UNIQUE on notifications |
| Double send same delivery | Worker checks status before send; UPDATE ... WHERE status=QUEUED claim |
| Provider timeout unknown | Mark SENT; reconcile via webhook; don't auto-retry SENT blindly |
| Push invalid token | Remove from device_tokens; don't retry |
| Marketing blast overload | Bulk queue with lower priority + provider rate caps |
| Quiet hours | Scheduler delays non-critical; transactional bypasses |
Digest mode (bonus): Instead of 50 push events/day, aggregate into one daily email โ separate aggregation window keyed by (userId, category, date).
6. Scale evolution
| Stage | Change |
|---|---|
| MVP | Monolith + single Redis queue + one SendGrid account |
| 10k/s | Per-channel Kafka topics, worker pools, multiple ESP accounts |
| Global | Regional queues; locale-specific templates; GDPR data residency |
| Marketing | Separate bulk infrastructure; stricter rate limits |
| Observability | Real-time dashboards; auto-pause on bounce rate spike |
7. Recap
- Notifications belong on a queue, not in the critical path of checkout/social APIs
- Idempotency keys prevent duplicate emails when upstream retries
- Separate queues per channel and priority so SMS slowness doesn't block push
- Track delivery state + webhooks for bounces; respect preferences and quiet hours
Practice: Design the payload for POST /notifications. Explain how you'd handle SendGrid outage without losing messages.