Mahir Shahriar Tamim

AI Researcher · Explainable & Responsible AI · Vision, Language & Agentic Systems

Part 4 of Building a Notification Service

Retries, duplicates and dead letters

Module 4 · 5 min read

Retries, duplicates and dead letters

TL;DR

  • After a timeout you can’t know if the SMS went out. So go at-least-once, and make retries harmless with a stable ID and idempotency keys.
  • Retry with backoff and jitter (retry topics in Kafka), then dead-letter, and keep an eye on the dead letters.
  • Circuit breakers, provider caps and per-user limits contain failures. The notification log holds it all together.

The outside world fails all the time

Most of the work happens in someone else’s system, so even a well-built service fails every few minutes. A 503 from the SMS gateway, a slow push provider, a connection dropped mid-request. At high volume “rare” happens daily. The bad outcomes are no message, or the same one twice, and two “You sent $50” alerts look like a double charge.

Three outcomes of one request

  1. Success. The gateway says “accepted”.
  2. A clear failure. An error or refused connection. The message did not go out, so retry later.
  3. A timeout. Maybe the gateway never got the request, or maybe it sent the SMS and the reply got lost. We cannot know.

Retry after a timeout and we may send twice. Don’t, and we may send nothing. Systems pick at-most-once (never retry, sometimes lose) or at-least-once (always retry, sometimes duplicate). For notifications that matter I pick at-least-once and remove the duplicates.

Making the retry harmless

An operation is idempotent if doing it twice has the same effect as once. So every notification gets a stable ID from the event and channel, like payment-81234:sms.

  • The worker skips the job if the notification log already shows that ID as accepted.
  • If the provider supports it, the ID goes along as an idempotency key (or client reference) and the provider ignores a second request with the same key. That covers timeouts too.
  • Kafka consumers re-read events after a crash or rebalance, and the same check makes those repeats harmless.

Without provider keys some timeout duplicates remain, but you can shrink them. Check the log before every retry, wait for the delivery receipt before retrying a timed-out send when time allows, and keep timeouts generous so fewer calls end in “don’t know”. In the demo, turn up the trouble, then turn on keys.

Retries
delivered once delivered twice or more dead-lettered

Press Send.

“Trouble” is the chance an attempt goes wrong. Two thirds are clear errors, one third are timeouts where the SMS actually went out. Up to 4 attempts, with growing waits, before dead-lettering.

Don’t retry all at once

If every worker retries immediately, a recovering gateway gets a wall of traffic and falls over again. Two things help.

  • Exponential backoff. Wait 1 second, then 2, 4, 8. A short glitch is retried quickly and a long outage isn’t hammered.
  • Jitter. Randomise each wait (say, between 0 and the backoff time), so ten thousand messages that failed together don’t retry at the same instant.

A Kafka consumer can’t sleep and retry without holding up its whole partition. So a failed job goes to a retry topic like sms.retry.1m, whose consumer waits until the job’s retry time, then to sms.retry.10m on another failure, and so on, while the main topic keeps moving. Channels retry independently, so if SMS fails, the push and inbox entry still go out.

Dead letters

An invalid or blocked number, or a crashing template, never succeeds, and retrying forever burns money and hides the problem. After the last attempt the job goes to a dead-letter topic (DLT, or DLQ for queues) with the reason. Alert when it grows, make it easy to inspect, and build a replay tool for after the fix. Replaying an OTP is usually pointless, but replaying a monthly statement is exactly right.

Circuit breakers and rate limits

  • Circuit breaker. When most calls to a provider fail, stop calling it for a bit (“open the circuit”), use a backup if you have one, and test with a few calls before going back to full traffic. It protects the provider and keeps your workers from waiting on timeouts.
  • Provider limits. Go over a gateway’s maximum rate and you get errors or throttling. Cap workers at the contracted rate, with headroom for the OTP lane (module 3).
  • Per-user limits. A bug elsewhere can fire one event a hundred times. “No more than N SMS per user per hour, except OTPs” stops that becoming a hundred texts.

The notification log

All of this rests on one table. One row per notification and channel, keyed by the stable ID, with status (queued, sent, delivered, failed, expired, dead-lettered), provider, attempt count and timestamps. It’s the idempotency check, the retry state, the support tool (“did this user get the SMS?”) and the source of module 5’s metrics.

Quick check

A call to the SMS gateway times out. Without an idempotency key, why is retrying risky?

Q&A

Doesn’t Kafka’s exactly-once solve duplicates?

Only inside Kafka. It covers reading from and writing to Kafka topics, and an outside SMS gateway is beyond that boundary, so the timeout problem is back. Idempotency at the edge is still your job.

How many times should I retry?

As long as the message stays useful. A few quick retries over a minute or two for an OTP, hours for a statement email. I set the policy per notification type instead of globally.