A webhook is a POST that says something happened. It arrives late, twice, out of order, or not at all, and the sender’s retry policy is usually invisible. Any system that treats a webhook as a reliable event stream will eventually process a payment twice, ship an order twice, or send the same notification to a customer three times within a minute.
The short version: every webhook consumer needs idempotency, an explicit ordering assumption, and a retry policy of its own on the receiving side. All three are cheap, and all three are usually missing.
Idempotency: identify the event, not the side effect
Idempotency starts with a stable key from the sender: an event id in the payload or a signature header. Store the key when processing begins, and reject or short-circuit duplicates. The store must be transactional with the side effect, or you get a window in which a crash loses both.
Keys should be the sender’s event id where available, and a hash of the payload plus event type where not. Timestamps are not keys: two distinct events can share a second, and a retried event always carries the same timestamp, so timestamp-keying both drops real events and fails to drop duplicates.
The second half of idempotency is making your own side effects repeatable. If the handler charges a card, the charge must be idempotent at the payment provider using your own key. If it sends an email, the send must be logged so a retry does not produce a second message. Retry logic without idempotent side effects just multiplies the damage.
Ordering: assume nothing, or assume everything
Senders rarely guarantee ordering, and those that do often guarantee it only per resource. A webhook stream that delivers created then updated can, under retry, deliver them in the other order: the first attempt of the update succeeds after the created event’s retry. Consumers must therefore either carry a sequence number and drop stale events, or reconcile against the source of truth after applying any event.
Reconciliation is the more robust choice when the sender’s sequence is unreliable: after processing, fetch the current state from the API and overwrite local state. That turns webhooks into invalidation signals rather than a state machine, which is a much simpler thing to operate and survives missed events.
Retry storms, from the other side
When your endpoint is down, the sender retries with backoff and typically gives up after some hours. During that window, retries arrive as bursts and can saturate the endpoint exactly when it is recovering. Two controls help: accept the webhook quickly, persist the raw payload, and process asynchronously; and apply your own backoff when the downstream dependency is unavailable, rather than failing fast and inviting more retries.
Acknowledge before processing where the sender requires a fast 2xx. The acknowledgement means “received”, not “done”, and the processing status must be tracked separately. Senders that treat a 2xx as completion will not retry, so a silent processing failure becomes a lost event; that is why the persisted payload plus a processing status is the actual safety net.
Signature verification and replay windows
Most providers sign the payload with a shared secret and include a timestamp to prevent replay. Verification must happen before parsing, using a constant-time comparison, and the timestamp must be checked against a window of a few minutes. Skipping the timestamp check allows an attacker who captured one request to replay it forever, which collides with your idempotency keys in a way that is hard to unwind.
Rotate secrets on a schedule and support two valid secrets during rotation. A single-secret deployment means a rotation is an outage unless the provider supports overlap, and many do not.
A worked example of a duplicate that cost money
A payment webhook fires when a subscription renews. The handler charges the stored card, writes an invoice, and emails the customer. One afternoon the endpoint returns a 500 because the invoice service is briefly unavailable, and the sender retries with backoff. Four attempts arrive over twelve minutes; three of them charge the card again before someone notices, because the handler keys idempotency on the request timestamp and each retry carries a new one.
The corrected version keys on the sender’s event id, checks it against a table inside the same transaction that records the charge, and passes a derived idempotency key to the payment provider. A retry now short-circuits in milliseconds. Ordering is handled by reconciling against the subscription API after each event, so an out-of-order cancellation followed by a renewal cannot resurrect a cancelled plan.
The endpoint also changes shape: it verifies the signature, writes the raw payload to a queue, returns 202 immediately, and processes asynchronously with a visible status. Retries during the invoice outage now become queued work rather than duplicate charges, which is the difference between a backlog and a refund process.
What to check before you commit
- Key idempotency on the sender’s event id or a payload hash, never on a timestamp.
- Make side effects idempotent at the downstream provider with your own key.
- Carry a sequence number or reconcile against the source after every event.
- Acknowledge fast, persist the raw payload, process asynchronously with recorded status.
- Verify signatures constant-time and enforce a replay window.
- Support two signing secrets so rotation is not an outage.
Webhooks are best treated as hints that something changed. Once a consumer is idempotent and reconciles against the source, most of the drama in webhook operations disappears, and the retry storm becomes a queue backlog rather than an incident.
A final detail that saves real incidents: expose the processing status. When a webhook is acknowledged and processed asynchronously, someone will eventually ask whether a specific event was handled, and the answer must be a lookup rather than a log search. Store the event id, the received time, the processing result, and the attempt count, and surface it in whatever internal tool the support team already uses. That table is the difference between a five-minute answer and an hour of uncertainty during a customer call.