download

A notification job is retried. How do you avoid a double send?

System design~15 minReviewed 29 Oct 2026Free · No sign-in

Question

When an order ships, a shop puts a job on a queue. A worker then sends a "Your order has shipped" push with the code below. The queue delivers each job at least once: a job not acknowledged within 30 seconds goes to a worker again.

Some users get the notification twice.

  1. Name two ways the same job can run twice.
  2. Change the design so a repeated job doesn't send again. Which double send can you still not rule out?
"CODE · PYTHON"
def handle(job):
    push.send(job.device_token, "Your order has shipped")
    queue.ack(job)  # tells the queue the job is done
Show hints (3)
  1. "COUNT THE RUNS"

    The worker crashes after send but before ack. What does the queue do? And what if send takes 40 seconds?

  2. "REMEMBER THE SEND"

    Give each notification a key that is the same on every run, like order-1001-shipped. Where do you store it, and when?

  3. "THE LAST GAP"

    You call the provider, then save "sent". What if the worker dies between those two steps?

Show checked explanation

A job runs twice after a crash before ack, or when a send passes 30 seconds and a second worker gets it. Save one notification row under a stable key like order-1001-shipped. Skip it once it is sent; claim it atomically if workers can overlap. A crash after the push, before sent is saved, can still repeat it.

"SOLUTION · PYTHON"
notes = {}  # key -> status; in production a table with a unique key


def decide(key):  # the order shipped; a repeat adds nothing
    notes.setdefault(key, "new")


def deliver(key, push):  # the queue job; it may run more than once
    if notes[key] == "sent":
        return "skip"
    push()  # may raise: then the job runs again later
    notes[key] = "sent"
    return "pushed"


pushes = []
for run in range(2):  # the event and the job both arrive twice
    decide("order-1001-shipped")
    print(deliver("order-1001-shipped", lambda: pushes.append(run)))
print(len(notes), "notification,", len(pushes), "push")
"OUTPUT"
pushed
skip
1 notification, 1 push

Code run on CPython 3.12, with every crash point and two overlapping workers simulated. Checked by an editor other than the author.

Walkthrough

At-least-once delivery means the queue would rather repeat a job than lose it. Two common ways a job runs twice:

  • The worker sends, then crashes before ack. After 30 seconds the job comes back.
  • The send takes 40 seconds. At 30, the queue gives the job to a second worker, so both send.

No queue can make a push happen exactly once, so make a repeated run do nothing. Build a key from the event, the same on every run: order-1001-shipped, not a new id per attempt.

Store the key with the decision. When the order ships, write a notification row under this key in the same transaction as the order update. The key is unique, so a repeated event adds no second row. The row starts as new.

Check it on every run. Skip a row that is already sent. After the provider accepts the message, mark the row sent, then ack. What each crash point costs:

crash pointpushes, no keypushes, with key
before the push11
after the push, before sent is saved22
after sent is saved, before ack21

The middle row is the gap that remains. No transaction covers both the provider and your database, so a retry can't tell whether the push went out. You choose between a rare duplicate and a lost message. For a shipping notice, the duplicate is the better failure.

Retry with backoff. On a timeout, 429 or 5xx, put the job back with a delay that doubles each time, say 1, 2, 4, 8 and 16 minutes, then alert. Don't sleep in the worker: past 30 seconds the queue hands the job out again. Provider rules come first: obey Retry-After, and APNs asks for 15 minutes before retrying a 5xx. Add random jitter so failed jobs don't all retry at once.

Common mistakes

  • Saving sent before calling the provider. A crash in between marks a message sent that never went out. That is at-most-once delivery; choose it on purpose.
  • Retrying every error. A timeout or 503 may pass, but a dead device token never will. Stop on permanent errors and remove the token.
  • Raising the queue's 30-second limit to stop repeats. Slow sends then repeat less often, but a crash still brings the job back.

Follow-up questions

  • "A send takes 40 seconds, and a second worker gets the job. Does the key help?" Not alone: both workers read new and both push. Claim the row first, in one atomic update: set it to sending with a deadline, only if it is new or its old deadline has passed. Only the worker whose update changed the row pushes. Make the deadline longer than a slow send.
  • "Does the push provider guarantee delivery?" No. An FCM message id means accepted for delivery, not delivered. Apple calls APNs best-effort: it may store, throttle or drop a notification. To hide a repeat, reuse the same apns-collapse-id; Apple merges the two into one notification. On Android, the same tag replaces the first one only while it is still shown.

What a strong answer covers

Explains why at-least-once repeats a job. Stores a stable key with the send decision, skips sent rows and claims rows atomically. Retries through the queue within the provider's rules. Names the gap between the push and the database write.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from System Design Interview: An Insider's Guide (Alex Xu, 2020); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.