Sub topic 15
Tracking delivery status and feedback
This chapter assumes you know how queues and workers hand events to providers, and that you have seen retries with backoff. Here we cover what happens after a worker calls a provider: how you learn whether the message arrived, and what you do with that information.
Why "sent" is not "delivered"
A successful API response from a provider usually means only that the provider accepted the message. The message can still bounce, be rejected by a carrier, or never reach a device. Tracking means recording each step separately, so you can tell the difference.
A common set of statuses:
queued: the event passed routing and is waiting for a worker.sent: the provider accepted the request.delivered: the provider or carrier confirmed arrival.failed: delivery was rejected or retries ran out.openedorclicked: the user interacted (email, push, in-app).
Not every channel reports every status. SMS often gives delivery receipts. Push gives limited feedback. Email opens are unreliable because mail clients can block tracking pixels or load them automatically.
Receiving provider feedback
Providers report outcomes through webhooks, which are HTTP calls they make to an endpoint you expose. Your service should verify the signature, store the raw event, and return quickly. Do the heavy work asynchronously.
Webhooks arrive late, repeat, and arrive out of order. A delivered callback can show up before your worker has written sent. Treat the status as a state machine that only moves forward.
ORDER = {"queued": 0, "sent": 1, "delivered": 2, "opened": 3, "failed": 3}
def apply_status(current, incoming):
"""Return the new status, ignoring stale or duplicate updates."""
if ORDER[incoming] > ORDER[current]:
return incoming
return current
print(apply_status("sent", "delivered")) # delivered
print(apply_status("delivered", "sent")) # delivered (late update ignored)In production, make the write atomic, for example a conditional update in your database, so two webhooks racing each other cannot move the status backward.
Acting on feedback
Feedback is only useful if it changes behavior:
| Signal | Action |
|---|---|
| Invalid or unregistered device token | Remove the token so you stop sending to it |
| Hard email bounce | Suppress that address |
| Spam complaint | Suppress the address and treat it as an opt-out |
| SMS "STOP" reply | Record an opt-out for that channel |
| Soft bounce or timeout | Retry according to your backoff policy |
Sending repeatedly to bad addresses damages your sender reputation, which can push all your email into spam folders.
Storing the data
Keep one row per notification and channel, plus an append-only log of status changes with timestamps and the provider's message id. The log lets you answer support questions such as "why did this user not get the SMS?" It also feeds the metrics covered in the observability chapter, such as delivery rate and time to deliver.
Exercise
A webhook says delivered for a notification your database still shows as queued. Then a late sent update arrives. What should the final status be, and what rule produces it?
Show answer
The final status should be delivered. Status only moves forward, so an update with a lower rank (sent) than the current one (delivered) is ignored. The queued state was just a delay in writing earlier steps.