NOTIFICATION PIPELINE
At-least-once delivery without spamming the user
Worker W1 takes job n1 from the queue on a 30-second lease: the queue hides n1 until W1 acks it or the lease runs out. The provider delivers n1 to the phone, and W1 crashes before acking. What happens next with a naive worker?
A notification pipeline is a queue, a pool of workers and a push provider. A worker leases a job, sends it, then acks it. If the worker dies before the ack, the lease expires and the job runs again. That is at-least-once delivery, and it is the right choice: a lost notification is worse than a late one.
The cost lands on the user. A crash between send and ack sends twice. So the send carries an idempotency key, and the last hop before the phone delivers each key once. That is the idealised provider in the sim. APNs and FCM do not dedup on a key, so in practice you keep your own dedup store, and it narrows the window without closing it: record the key before the send and a crash loses the notification, record it after and a crash still sends twice. A collapse id (apns-collapse-id, FCM collapse_key) makes the phone show a repeat as one notification. Failures need sorting too: a 429 is retried with backoff, while a 400 never succeeds and goes to a dead-letter queue.
async function handle(job) {
for (;;) {
const r = await provider.send(job.to, job.body); // no key
if (r.status === 200) return queue.ack(job);
await sleep(1000); // forever, 1 s apart
}
}async function handle(job) {
const r = await provider.send(job.to, job.body, { idempotencyKey: job.id });
if (r.status === 200) return queue.ack(job); // also for a duplicate key
if (r.status === 429 && job.attempts + 1 < MAX)
return queue.retryLater(job, Math.min(CAP, BASE * 2 ** job.attempts));
return queue.deadLetter(job, r.status); // 4xx or out of attempts
} Watch the user’s phone and the three invariants. NAIVE: CRASH + THROTTLE sends n1 twice, hammers the throttling provider once a second for a minute, and retries the dead token forever. FIX: KEY + BACKOFF + DLQ runs the same crash and the same throttle window: the second n1 send is answered as a duplicate, n2 gets through in 5 attempts, and n3 goes to the DLQ at once.
The naive pipeline with no crash: each notification reaches the phone once (the throttling is a separate mess). Crash W1 at the step where it makes the user get the same notification twice.
GOAL: the user receives the same notification twice
Write handle(job, provider, sent). It is called for every delivery of a job, including redeliveries after a crash, and must make exactly one provider call (or none) and return a decision: { action: 'ack' }, { action: 'retry', delay } or { action: 'dlq' }. The user must get each notification at most once; 429 retries back off exponentially (4 s × 2^attempts); a 400, or a 429 on the 6th attempt, goes to the DLQ.
SQS (visibility timeout, redrive policy to a DLQ), Pub/Sub and RabbitMQ (ack deadlines, dead-letter topics), Sidekiq and Celery retries with exponential backoff; APNs and FCM in front of the phone, with their own throttling. APNs and FCM do not dedup by key, so teams keep a sent-keys table in front of them; APNs' apns-collapse-id at least makes a repeat replace the earlier banner instead of stacking.
“Design a notification service.” On the whiteboard: producers write to a queue; workers lease jobs, look up preferences and device tokens, call APNs/FCM/email, then ack. Then name the failure: a worker that dies between send and ack causes a redelivery, so the user gets duplicates. The fix: an idempotency key per notification, checked in a dedup store (with a TTL) and honoured by the provider where it can be. Say that the store only shrinks the window (a crash between send and record still duplicates) and that collapse ids hide what is left. Separate retryable errors (429, 5xx: exponential backoff with jitter, a max attempt count) from permanent ones (invalid token: DLQ, and delete the token), add per-provider rate limits so a throttle does not turn into a retry storm, and finish with priority queues so a marketing blast cannot delay a password-reset code.