Case Study: Notification System

Push, SMS, email: designing a notification system that ships billions of messages without spamming.

Intermediate8 min read
Language
Theme
Notification APIOTP queuehigh priorityMarketing queuelow priorityWorkersWorkersFCM / APNs (push)Twilio (SMS)SES (email)Preferences + quiet hours + per-user rate limits + idempotent retries + delivery webhooks
Key idea at a glance

The problem, stated properly

Design a notification platform that any product team can call with one API ("notify user U about event E") and that handles everything after: choosing the channel (push, SMS, email, in-app), personalizing and localizing the message, respecting user preferences and quiet hours, rate-limiting so users do not uninstall, retrying through flaky third-party providers, and tracking delivery. The trap in this question is treating it as plumbing: the hard parts are fan-out scale, third-party dependency management, and not annoying the user, which is a correctness problem of its own.

Scale first

  • Volume: a large consumer product sends ~10-100 million notifications/day (order updates, likes, reminders, promos). That is ~1,000/sec average, 50-100x that when a marketing blast or a breaking-news event lands: design for ~100K/sec bursts.
  • Fan-out: one trigger can mean millions of notifications (a celebrity posts; every follower gets one). A single API call can turn into hours of queued work, so the ingest path must decouple from delivery instantly.
  • Latency classes: OTPs and security alerts need delivery in seconds; a promo can wait an hour. One size of queue fits nothing: priorities are a first-class design element.

The pipeline, stage by stage

  1. Ingest API: receives the request, validates it, stamps an idempotency key, writes it durably, and returns. Nothing slow happens here.
  2. Decision/orchestration service: for each notification, resolve the user, check preferences (does this user want marketing pushes at all?), quiet hours (batch it for morning), template + locale (render the final text), and channel selection (push first, SMS fallback if push is undeliverable and the message is critical, email for long content).
  3. Priority queues: one queue per (priority class, channel). A Kafka topic or SQS queue per lane keeps OTPs from queueing behind a 10-million-message promo blast.
  4. Channel workers: pools per channel that render final payloads and call providers: FCM/APNs for push, Twilio-class aggregators for SMS, SES-class services for email. Workers respect per-provider rate limits with token buckets.
  5. Delivery tracking: providers webhook back accepted/delivered/failed/bounced. These events update a delivery store that powers retries, fallbacks, and analytics.

The parts that are actually hard

  • Idempotency and dedup: producers retry, queues deliver at-least-once, providers accept duplicates. You need dedup at every layer: idempotency key at ingest, message-ID dedup in workers (a Redis SETNX with a 24h TTL is enough), and provider-side idempotency keys where supported. A user who gets the same "your order shipped" push 3 times files it under spam, permanently.
  • Provider failure and fallback: FCM is down, APNs is throttling, an SMS route to a country degrades. Circuit breakers per provider, automatic failover to a secondary provider for critical lanes, and dead-letter queues for messages that exhaust retries. SMS in particular has per-country routes with wildly different reliability and cost; routing tables are their own subsystem at scale.
  • Token lifecycle (push-specific): device tokens rot constantly (app uninstalls, OS refreshes, device changes). Providers return invalid-token errors that must feed a cleanup loop, or you keep paying to send into the void. iOS delivery is especially opaque: APNs confirms acceptance, not display.
  • Rate limiting the user, not the server: per-user caps (max N notifications/day by category) and fatigue models (if the last 5 pushes were never opened, demote this user). This is the difference between a notification system and an uninstall generator.

Preferences and quiet hours: the correctness problem nobody diagrams

Preference checks sound trivial until you enumerate them: global opt-outs, per-category toggles, per-channel toggles, quiet hours in the user's timezone (with DST), and legal compliance (marketing consent is revocable and must propagate fast; GDPR/Indian DPDP-style rules make this a hard requirement, not a nicety). The standard shape is a preference service with a read-optimized cache, because the decision service queries it for every single notification. Quiet hours convert "send now" into "send at 8 AM user-local," which means the scheduler (a delayed-delivery queue) is part of the core design, not a bolt-on.

Tradeoffs worth naming

  • Push vs SMS vs email: push is free-ish and instant but silent if permissions are off (~50% of Android users globally deny them); SMS works everywhere but costs real money per message (~$0.05-0.10 in India, more internationally) and has carrier-level filtering; email is cheapest and richest but slowest and lands in tabs nobody opens. Critical alerts justify SMS cost; promos never do.
  • Build vs buy: nobody builds their own APNs gateway, but owning the decision/orchestration layer in-house is what lets you do fatigue modeling and cross-channel logic that off-the-shelf tools do badly.
  • Ordering: per-user ordering matters ("ride arrived" before "ride completed" is nonsense) but global ordering is impossible across channels. Sequence per user per channel, accept cross-channel races.

Production failure modes

  • The retry storm: a provider outage makes every message retry, queues balloon, and when the provider recovers your backlog hits it like a DDoS and trips its rate limits again. Defenses: exponential backoff with jitter, max-age expiry (a 3-hour-old OTP is worthless, drop it), and admission control on retries.
  • Duplicate delivery: at-least-once everything means duplicates are guaranteed eventually. Dedup windows and client-side suppression (same message ID within N minutes) are mandatory.
  • Blast-induced self-DoS: a promo to 50M users at once also wakes 50M apps that then call your APIs. Stagger sends (bucket users over 15-30 minutes) or your notification system takes down your own product.
  • Template regressions: a broken template variable ships "{user_name} your order {order_id}..." to a million users. Template validation and canary sends exist because this has happened at every company.

Common interview traps

  • Designing one queue for everything. Priority lanes are the first thing the interviewer probes.
  • Calling providers synchronously from the API. Third-party latency belongs behind a queue, always.
  • Forgetting preferences and quiet hours entirely. It reads as never having used a phone.
  • Treating "sent to provider" as "delivered." Delivery tracking is what separates a system from a hope.

Interview Q&A

Q: How do you guarantee a user is not spammed even if five different product teams all decide to notify them today?
A: Centralize the decision: every notification, from every team, flows through one orchestration layer that enforces per-user budget (say, max 3 marketing + unlimited transactional per day), fatigue scoring (demote users who never open), and dedup by content hash within a window. Individual teams can request anything; the platform owns the user's attention. This is why notification platforms get built as internal products with real authority, not as libraries.

Q: An OTP must arrive in under 10 seconds, 99.9% of the time. Walk me through the design.
A: OTP lane gets its own top-priority queue with a dedicated worker pool that no blast can starve. Two SMS providers behind a circuit breaker: on slow ack or failure from the primary, the worker fails over within ~2 seconds. Delivery SLA is measured from ingest to provider-ack via the tracking webhooks, and anything breaching it pages. Voice-call fallback for the residual failures. And OTPs expire from the queue itself: a message older than 60 seconds is dropped, because a stale OTP only confuses.

Key takeaways

Interview pointer

'Design a notification system' sounds simple, but the depth is in preferences, priorities, retries and tracking. Say these four with the diagram and the answer is complete.