Skip to content

Reliable webhooks: delivery, retries and idempotency between systems

B2Bnetwork team · · 6 min

Integrations, data and cloud

In brief

A webhook is reliable when the sender stores the event before sending it and retries with growing intervals, and the receiver verifies the signature, answers quickly with a 2xx code, puts the message on a queue and spots duplicates by the event ID. On its own, a webhook guarantees at most at-least-once delivery: a message can arrive twice, late or out of order. Events that still go missing are recovered by a periodic reconciliation through the API.

Why webhooks get lost and duplicated

A webhook is an ordinary HTTP POST request that system A sends to system B when something changes: an order is paid, a courier delivers a parcel, an invoice is created in the ERP. Its simplicity is both its strength and the source of trouble. Typical cases from production:

  • the receiver did not answer in time because it processed the message synchronously and a database query took too long; the sender treated the delivery as failed and sent it again, although the receiver had already saved the change;
  • the sender sent the event before committing the transaction in its own database; the transaction rolled back, and the receiver learned about something that does not exist;
  • the sender restarted in the middle of delivery and the event was lost because it lived only in memory;
  • two events for the same object arrived in reverse order: "shipped" before "packed";
  • a certificate or secret expired and the receiver has been rejecting every message for a week without anyone noticing.

Each of these cases has a known fix on one of the two sides.

The sender's side: storage, retries, limits

  1. Store the event in the same transaction as the data change (the outbox pattern): a separate table of events to send, read by the delivery process. A restart does not lose events, and a rolled-back transaction sends nothing false.
  2. Give every event a stable ID and keep it unchanged on retries. That is how the receiver recognizes a duplicate.
  3. Retry with exponential backoff and random jitter, over several days rather than minutes. The Standard Webhooks specification gives a sample schedule from an immediate attempt to 24-hour gaps, about 75 hours in total.
  4. Set a response timeout of around 15–30 seconds and treat only 2xx codes as success. On 429, 502 and 504, slow down. On 410, disable the endpoint.
  5. When retries run out, notify the receiver through another channel, such as an email to the integration owner, and show a list of undelivered events with an option to resend.

The receiver's side: signature, queue, idempotency

StepWhat to doWhy
signaturecompute HMAC-SHA256 over the ID, timestamp and body, compare in constant timeyou reject messages from anyone who only knows the URL
timestampreject messages older than a few minutesan old intercepted message cannot be replayed
fast answerwrite the message to a queue and answer 2xx immediatelythe sender does not retry because of slow processing
idempotencystore the ID of each processed event and skip repeatsthe same transfer is not booked twice
orderingcompare the object's version or change time with what you holdan older event does not overwrite a newer state
fresh statewhen in doubt, fetch the object through the APIyou decide on current data

The Standard Webhooks specification settles on header names, so the receiver can use a ready-made library:

POST /webhooks/zamowienia HTTP/1.1
webhook-id: msg_2Lk9...
webhook-timestamp: 1764496800
webhook-signature: v1,K5oZfzN95Z9UVu1EsfQmfVNQhnkZ2pj9o9NDN/H/pI4=
content-type: application/json

Many vendors send only the event ID and type in the webhook and expect you to fetch the full object through the API. That costs one more request, but it solves the problems of ordering and stale data in the message body. When processing takes a long time or events come in large numbers, a queue on the receiver's side is a must, as described in Queues and asynchronous processing.

Security on both sides

The receiver exposes a public URL, so it has to check the signature on every message, accept HTTPS only and limit the body size. Keep the signing secret in a secrets vault, not in application configuration. The specification allows several signatures in one header, which makes it possible to rotate the secret without downtime.

A sender that lets customers enter their own webhook URL opens the door to SSRF: someone enters the address of an internal service and makes your server send a request to it. OWASP's guidance is to allow only specific schemes and ports, block private and local addresses after DNS resolution, and send from a separate network with no access to internal systems. API protection in general is covered in API security.

Reconciliation, monitoring and the contract

Even a well-built webhook does not replace reconciliation. Every few hours or once a day, the receiver fetches through the API the list of objects changed since the last reconciliation and compares it with what it received. Missing events go through the same path as webhooks. For an online store, this means an order paid at night, while the warehouse system was down, still reaches picking in the morning.

For monitoring, three numbers per sender are enough to start: messages accepted per hour, messages rejected because of the signature, and the age of the oldest unprocessed message in the queue. A sudden drop to zero during business hours is an outage, even when the servers are healthy. More in Integration monitoring.

Describe webhooks in the contract just like an API. OpenAPI 3.2.0 has a separate `webhooks` field for messages your system sends, and AsyncAPI describes event channels. Documentation is covered in Integration documentation: OpenAPI, AsyncAPI and an interface catalog.

B2Bnetwork engineers design such flows as part of our System integration service, together with resilience tests: a receiver that is down, duplicated and reordered messages, an expired secret. One example is the integration of an online store with marketplaces and a warehouse system, where the target was zero lost or duplicated messages in resilience tests.

Questions and answers

Does a webhook guarantee that a message is delivered?

No. Even with retries it guarantees at most at-least-once delivery, and once retries run out a message can be lost. That is why the receiver recognizes duplicates by event ID and recovers missing events with a periodic reconciliation through the API. The webhook shortens reaction time; reconciliation gives certainty.

Webhook or a message queue such as Kafka?

A webhook suits notifications between companies and systems with no shared infrastructure: an online store, payments, a carrier. A queue or event platform suits the inside of an organization, when many systems read the same events and you need history and replay. The two are often combined: an external webhook lands on an internal queue.

What happens if our system is down for a few hours?

If the sender retries over several days, the messages arrive once the system is back, but out of order and in one large burst. The receiver has to cope: a queue, rate-limited processing and checking object versions. If the sender retries for a shorter time, reconciliation through the API saves you.

How do you test webhooks before go-live?

The test set should send the same message twice, two messages in reverse order, a message with a wrong signature and one with an expired timestamp, and simulate the receiver being unavailable for a few hours. You check the result in the data: one order, one payment, the correct final status.

What it looks like in a project

Connecting systems or moving them to the cloud? Talk to an architect.

Let's talk

Read more: Integrations, data and cloud

All in Integrations, data and cloud →