NOTIFICATION PIPELINE

At-least-once delivery without spamming the user

L320 MINSYSTEM DESIGN
PREDICT

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.

▶ SIMULATION
WORKER W1
holding — · idle
idle
QUEUE · t = 0 s
—
DLQ —
WORKER W2
holding — · idle
idle
providerhealthy · 0 calls, 0 while throttling
user's phone—
✓ each notification delivered at most once to the user✓ no notification attempted more than 6 times✓ at the end, every job is delivered or dead-lettered
SCHEDULE
01[QUEUE]no idempotency key; retry every 1 s forever
02[QUEUE]QUEUE: enqueue n1, n2, n3
03[W1]W1: take the next visible job (30 s lease)
04[W1]W1: send to the provider
05[W1]W1: CRASH (after sending, before ack)
06[W1]W1: ack (delete the job)
07[QUEUE]QUEUE: clock → 31 s (expired leases make their jobs visible again)
08[W2]W2: take the next visible job
09[W2]W2: send to the provider
10[W2]W2: ack
11[PROVIDER]PROVIDER: starts throttling: 429 until 92 s
12[W1]W1: restart if crashed
13[W1]W1: take the next visible job
14[W1]W1: send to the provider
15[W2]W2: take the next visible job
16[W2]W2: send to the provider
17[QUEUE]QUEUE: clock → 100 s (workers retry by their policy)
18[QUEUE]QUEUE: report
Start of naive: crash + throttle scenario. 18 steps.
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
  }
}

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.

BREAK IT NOT YET

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

FIX IT NOT YET

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.

Mental model:

a queue gives you at-least-once; the user wants once. Close the gap by making the side effect idempotent: a key that travels with every attempt of the same job. Against a provider that ignores keys you can only shrink the gap, not close it. Then sort failures by whether time can fix them: back off on the ones it can, dead-letter the ones it cannot, and cap both, so one bad job never runs forever.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: DISTRIBUTED LOCK SERVICE →