Part 20 of 22 · System Design

How do you deliver webhooks so no event is ever lost?

Write every event to an outbox, retry with backoff and jitter for a day or more, sign each request with a timestamp, and let receivers replay what they missed. Receivers answer fast, deduplicate by event ID and never trust order. The design, and the quiet failure it prevents.

A red Victorian post box on a city street, for delivering events reliably

The short answer: treat a webhook as a promise you keep, not a request you fire once. Record the event in an outbox in the same transaction as the change, deliver at least once with exponential backoff and jitter for a day or more, sign every request with a timestamp so receivers can prove it came from you, and keep an events API so anyone who missed something can fetch it again. On the receiving side: answer 2xx quickly, deduplicate by event ID, and never assume order.

The finance team noticed it first. Every month, the number of calls the CRM said had happened was a little lower than the number on the telephony bill. Not by much: a few dozen calls out of tens of thousands. Nobody could find a pattern. The calls weren’t from one agent, one day or one customer.

Then someone lined the missing calls up against the deploy log. Every one of them had ended within the same minute as a deploy of the CRM.

Case file

The case A few dozen events a month vanish without a trace
Suspects The network, the sender’s servers, a bug in the CRM’s parser
Culprit A sender that retried twice within a minute, then gave up, while the receiver was restarting
The fix Retries that outlast any outage, deduplication on receipt, and a way to replay
Evidence Missing events line up exactly with the receiver’s deploys

How the events went missing #

Here is what happened, one deploy at a time. A call ends. The phone system sends call.ended to the CRM’s webhook URL. The CRM is mid-deploy: its load balancer returns 502 Bad Gateway. The sender waits 30 seconds and tries again: still deploying, another 502. The sender’s policy says two attempts, so it marks the event failed and moves on. Ninety seconds later the CRM is healthy again, and nobody will ever tell it about that call.

No component was broken. Each did exactly what it was built to do. The event was lost in the gap between two reasonable decisions: a sender that gave up quickly, and a receiver that was briefly unavailable. That’s what makes webhook bugs feel so mysterious: there is no error to find, only an absence.

The pieces of the puzzle #

Flowchart: Application change, Outbox: events table, Dispatcher, Queue: endpoint A, Queue: endpoint B, Delivery worker, Receiver A, Receiver B, Failed deliveries: visible, replayable, Events API: list since cursorFlowchart: Application change, Outbox: events table, Dispatcher, Queue: endpoint A, Queue: endpoint B, Delivery worker, Receiver A, Receiver B, Failed deliveries: visible, replayable, Events API: list since cursor
Diagram source
flowchart LR
  APP[Application change] -->|same transaction| OUT[(Outbox: events table)]
  OUT --> DISP[Dispatcher]
  DISP --> Q1[[Queue: endpoint A]]
  DISP --> Q2[[Queue: endpoint B]]
  Q1 --> W1[Delivery worker] -->|signed POST| RA[Receiver A]
  Q2 --> W2[Delivery worker] -->|signed POST| RB[Receiver B]
  W1 -. failure: backoff + jitter .-> Q1
  W1 -. gave up .-> DLQ[(Failed deliveries: visible, replayable)]
  RA -. missed something? .-> API[Events API: list since cursor]
  API --> OUT
Piece Job Why it matters
Outbox Store the event in the same database transaction as the change that caused it A crash between “change saved” and “webhook sent” can’t lose the event
Per-endpoint queues Each receiver gets its own queue and workers One slow or dead receiver can’t delay everyone else’s events
Retries with backoff and jitter Retry failures at growing intervals, randomised Outlasts deploys and outages without hammering a struggling receiver
Signatures HMAC over the timestamp and body, with a shared secret Receivers can prove the request is from you and reject replays
Delivery log Every attempt: status, response time, error Senders and receivers can both see what happened
Events API and replay List events after a cursor; resend a range The safety net when everything else has failed

Retries that outlast the outage #

The most important number in a webhook system is how long you keep trying. Two attempts a minute apart survive a dropped packet; they don’t survive a deploy, a database migration or a bad night. A schedule that grows to hours survives almost anything:

Attempt Wait before it Time since the event
1 immediately 0
2 ~30 seconds 30 s
3 ~2 minutes 2.5 min
4 ~10 minutes 12.5 min
5 ~1 hour ~1 h 15 min
6 ~3 hours ~4 h
7 ~8 hours ~12 h
8 ~12 hours ~24 h

Two details make this work, and they are small and quite lovely:

  • Jitter. Add a random spread to each wait (say ±20%). Otherwise, when a receiver comes back after an outage, every queued event retries at the same instant and knocks it over again. Jitter turns a thundering herd into a gentle queue.
  • Only retry what might succeed. Timeouts, 5xx and 429 Too Many Requests are worth retrying (and honour Retry-After when it’s sent). A 400 or 401 won’t change on its own; retrying it just wastes everyone’s time. Disable an endpoint after it has failed continuously for days, and email its owner.

The delayed retries post shows how a queue can schedule millions of these retries cheaply, and the queue post covers the outbox in detail.

Signing: proving it was you #

A webhook URL is just a public endpoint. Anyone who learns it can post a fake payment.succeeded to it. The defence is a signature:

webhook-id:        evt_01JD8Q3W6X9K2M4N7P
webhook-timestamp: 1794816000
webhook-signature: v1,<base64 HMAC-SHA256 of "id.timestamp.body">

The receiver recomputes the HMAC with the shared secret and compares it in constant time. Including the timestamp in the signed text is the clever bit: the receiver rejects anything older than about five minutes, so an attacker who captures a real request can’t replay it next week. This header layout follows the open Standard Webhooks specification, which is a good default if you’re designing one from scratch. For key rotation, allow two valid secrets at once for a while.

One more sender-side rule, easy to forget: don’t let customers point webhooks at your own network. Resolve the URL and refuse private and internal addresses (10.x, 127.x, 169.254.169.254 and friends), or your webhook sender becomes a way to reach your internal services.

The receiver’s half of the bargain #

At-least-once delivery means duplicates are guaranteed, and the network means order isn’t. A good receiver is built for both:

  1. Verify the signature, store the raw event, return 200 immediately. Do the real work afterwards from your own queue. A receiver that does slow work inside the request will time out and get the same event again.
  2. Deduplicate by event ID. A unique constraint on event_id in a processed-events table turns a duplicate into a harmless no-op.
  3. Don’t trust arrival order. call.ended can arrive before call.answered. Either include a version or sequence number and ignore older ones, or treat the webhook as a nudge: “something changed on call 42”, then fetch the current state from the API.
  4. Reconcile on a schedule. Once a day, list events since your last cursor and process anything you don’t have. That nightly job is what would have found our missing calls the next morning instead of at the month’s end.

Three situations, three setups #

Case 1: a startup sending its first webhooks #

Situation: a small SaaS wants to notify customers’ systems when orders change.

What I’d build: an outbox table, one worker pool, the retry schedule above, HMAC signatures, and a page in the dashboard showing recent deliveries with a “resend” button. That page will save your support team hours: most webhook tickets are answered by “here is every attempt and what your server said”.

Case 2: receiving call events into a CRM #

Situation: a phone system sends call events, and you don’t control its retry policy.

What I’d build: assume the sender might give up early, because some do. Make the receiver boringly available (return 200 from a tiny handler that only writes to a queue), deduplicate by event ID, and run a nightly reconciliation against the provider’s call-records API. Your reliability shouldn’t depend on someone else’s retry settings.

Case 3: a platform with thousands of receivers #

Situation: some customer endpoints are fast, some are slow, some have been dead for months.

What I’d build: per-endpoint queues and per-endpoint rate limits, so one customer’s slow server can’t fill your workers. Circuit-break endpoints that keep failing: pause them, retry occasionally, disable after a few days with an email to their owner. Offer replay by time range, so a customer recovering from an outage can ask for “everything since Tuesday 14:00”.

Choosing a delivery guarantee #

Fire once Retry briefly At least once, with replay
What it fixes Simplest possible sender Survives blips Survives deploys, outages, bad nights
What it costs Nothing Little An outbox, a schedule, a log, an API
How it fails Silently, constantly Silently, during every deploy Duplicates and late arrivals, which receivers handle by design
Pick it when Never for anything that matters Notifications nobody acts on Money, calls, orders, anything someone counts

Remember this

  • An event that isn’t in an outbox can be lost by a crash.
  • Keep retrying for a day or more, with jitter; two quick retries won’t outlast a deploy.
  • Receivers: answer fast, deduplicate by ID, and reconcile on a schedule.

What to check on Monday #

  1. Is every event written to an outbox in the same transaction as the change?
  2. How long does the sender keep retrying? Is it longer than your longest outage?
  3. Are requests signed with a timestamp, and are private addresses blocked?
  4. Does the receiver return before doing the work, and deduplicate by event ID?
  5. Is there a reconciliation job, so missed events are found within a day?

What I admire in a good webhook design is its humility. It assumes the network will drop things, the receiver will be down, events will arrive twice and in the wrong order, and then it calmly arranges for none of that to matter. The missing calls never came back from that first month. Every month after, they were found the next morning.

Like (sign in first) 0 likes 💬 0

Written by

Vishal Kumar

Associate Director at CapGrid, running product and engineering in a CPTO capacity. 10+ years building distributed, multi-tenant SaaS and AI platforms. AWS Certified Solutions Architect - Professional. Based in Gurugram.

Comments

No comments yet.

Sign in to comment. We'll email you a link; no password needed.