Notification fanout
Other services request notifications. The system respects user preferences and quiet hours, deduplicates, and delivers by push, email or SMS with retries.
Envelope10M notifications/hour · per-user preferences · multiple channels
What the design must do
- Producers enqueue and return immediately
- Preferences and quiet hours checked before send
- Duplicate requests collapse into one delivery
- Failed sends retry, then dead-letter
The worked envelope
Assumed. 10M notifications/hour steady state, 100M registered devices, a broadcast campaign can target 10M users at once.
| Steady rate | 10M/hr ≈ 2.8k/s, peak ~8k/s |
| Broadcast burst | 10M in 5 minutes = 33k/s — 12× steady |
| Preferences | 100M users × 200 B = 20 GB |
| Push tokens | 100M × 100 B = 10 GB |
| Dedup keys | 240M/day × 50 B = 12 GB at 24h TTL |
| Provider ceiling | APNs/FCM throughput is theirs, not yours |
What the numbers decide. The gap between 2.8k/s steady and 33k/s during a campaign is the whole design problem. You cannot provision for the burst — that is twelve times the fleet, idle almost always — and you cannot drop it either.
So the queue is not an optimisation here, it is the load-shaping mechanism: it absorbs a burst your workers drain at whatever rate the downstream providers will actually accept. And note the last row. Your peak is not your constraint; APNs and FCM have their own limits, so the system's real job is to be a well-behaved, rate-limited, retrying client of somebody else's service.
The decisions, and what each one costs
A queue between event and delivery, workers draining at provider rate
- Because
- A campaign is 12× steady load and the downstream provider has a fixed ceiling; the queue converts a burst you cannot serve into a backlog you can
- The cost
- Delivery is no longer immediate, and queue depth becomes a metric someone must watch and alert on
- Rejected
- Synchronous send, which fails the campaign and takes the publishing service down with it
Preferences and quiet hours evaluated at send time, not at enqueue time
- Because
- A campaign queued at 10:00 and drained until 10:20 must respect a user who opted out at 10:05, and a user’s timezone decides whether this is the middle of the night
- The cost
- A preference lookup per notification rather than per campaign — 33k/s of reads against the 20 GB store
- Rejected
- Filtering at enqueue, which sends to people who unsubscribed while the queue drained
Idempotency key per (user, event), stored with a 24-hour TTL
- Because
- At-least-once delivery from the queue means a worker restart re-sends, and a duplicate push is the most visible bug this system can ship
- The cost
- 12 GB of keys to hold and expire, and a late redelivery past the TTL slips through
- Rejected
- Exactly-once delivery, which does not exist across a boundary you do not control
Per-channel adapters behind one interface, with per-channel retry policy
- Because
- Push, SMS and email fail differently — a bounced email is permanent, a 429 from APNs is not — and a shared retry policy is wrong for all three
- The cost
- More code paths, and a channel-specific bug affects only that channel’s users, which makes it easy to miss
- Rejected
- One generic sender, which retries permanent failures forever and gives up on transient ones
Components in the reference design
- Client
- Browser or mobile app
- Load Balancer
- L7 routing, TLS termination, health checks
- App Service
- Stateless business logic
- Queue
- Work distribution, one consumer per message
- Worker
- Async consumer, background jobs
- Push Service
- APNs / FCM delivery
- SQL DB
- Relational, ACID, joins
The reference design
Why this is queue-shaped. The requesting service must not wait for a third-party push provider. It writes an intent and returns. Everything after that is asynchronous, retryable, and independently scalable.
Preference evaluation belongs in the worker, not the producer. If every calling service had to know about quiet hours and channel preferences, that logic would be reimplemented five times and diverge. One consumer owns the rules.
Deduplication. Delivery is at-least-once, so the same message will arrive twice. Give every notification a deterministic key — something like user + event type + entity ID + time bucket — and record delivered keys with a TTL. Second arrival, no send. This is also what collapses "37 people liked your post" into one notification rather than 37.
Rate limiting per user is a product requirement disguised as an infrastructure one. Cap notifications per user per hour, and batch overflow into a digest. Without this, one noisy event turns into an uninstall.
Retries and dead letters. Provider failures are transient often enough to retry with exponential backoff and jitter, and permanent often enough — invalid device token, unsubscribed address — that you must distinguish the two. Permanent failures should remove the token, not retry it forever. After N attempts a message goes to a dead letter queue where a human can look at it, rather than blocking the partition.
Priority. A one-time password and a marketing digest must not share a queue. Separate queues with separate worker pools, or the digest campaign delays every login in the country.