Design and receive webhooks: handle duplicates, ordering, and retries safely
A webhook is a notification pattern in which one system sends an HTTP request to a registered URL when a subscribed event occurs. Compared with polling an API on a schedule, webhooks can deliver changes sooner and avoid unnecessary reads, but receivers must allow for duplicates, reordering, delays, and failures. A webhook is therefore more than a callback URL: it is a delivery contract covering event subscriptions, verified receipt, prompt acknowledgment, safe background processing, and recovery.
A webhook is a notification pattern in which one system sends an HTTP request to a registered URL when a subscribed event occurs. Compared with polling an API on a schedule, webhooks can deliver changes sooner and avoid unnecessary reads, but receivers must allow for duplicates, reordering, delays, and failures. A webhook is therefore more than a callback URL: it is a delivery contract covering event subscriptions, verified receipt, prompt acknowledgment, safe background processing, and recovery.
How a webhook delivers an event
The event provider records which event types a subscriber wants and where to send them. When a subscribed event occurs, the provider typically sends an HTTP request with a JSON body to the receiver endpoint. For example, GitHub can deliver events when an issue opens or code is pushed; Stripe sends notifications about payment state changes. Event names, headers, payload formats, signatures, and retry policies vary by provider, so one service's behavior is not a universal webhook rule.
Webhooks and polling are different delivery choices:
| Method | How it works | When it fits and its cost |
|---|---|---|
| Webhook | The provider calls the receiver when an event occurs | Can reduce read traffic when changes must be handled promptly or many resources are monitored, but requires a reachable receiver and delivery recovery |
| Polling | The consumer checks an API at intervals | Simple for one-time or occasional checks on a small set of resources, but makes requests when nothing changed and detection can wait until the next interval |
GitHub describes webhooks as a way to receive data when events happen and notes they can require fewer requests than polling when monitoring many resources. For a one-time check or a small set of resources, an ordinary API call may be simpler. Choose based on whether events are needed, acceptable delay, and whether you can operate a receiver and recovery process.
Separate quick acknowledgment from the actual work
The provider expects a response that confirms receipt; if the response fails or takes too long, it may retry. Doing expensive work before responding can cause timeouts or a backlog during a delivery spike. A reliable receiver flow is:
Accept the request over HTTPS and verify its origin and integrity with the provider's signature mechanism. The precise signature procedure is a separate contract and is outside this article's scope.
Check that the event type is allowed and record the provider's delivery ID and body, or place them in a durable queue.
After the event is safely accepted, return a success response within the provider's deadline.
Let a background worker apply the event to business state and record its outcome and any failure reason.
Retry temporary failures without repeating side effects, and recover missed events from the provider's delivery log or API.
It is safer to define acknowledgment as “the event was accepted so it will not be lost,” not “all business work has finished.” Returning success before persisting or queueing can lose the event if the process stops. Waiting until all business work finishes can make the provider time out and resend the same event. Consider making durable storage and queueing atomic, or withhold the success response if storage fails.
Deadlines are provider-specific. GitHub says a receiver should return a 2xx response within 10 seconds; otherwise GitHub terminates the connection and treats delivery as failed. Stripe also recommends deferring complex processing and responding promptly. The 10-second deadline is GitHub's contract, not a standard shared by every provider.
Treat duplicates and reordering as normal conditions
Idempotency means that processing the same event more than once leaves the business outcome as though it had been processed once.
An event may be delivered again, so a handler should not send an order twice or create an account twice when it receives the same input again. Idempotency means that processing the same event more than once leaves the business outcome as though it had been processed once. A common approach is to store processed provider event IDs and avoid repeating business effects for an ID already recorded. Some services can also create separate event objects for the same underlying domain change; in those cases, the resource ID and event type may both be needed. Stripe recommends event IDs for duplicate deliveries and, for some distinct event objects describing the same change, the data.object ID together with the event type.
Delivery order can differ from event creation order. GitHub and Stripe both document out-of-order delivery. Do not infer order only from an event timestamp. Stripe snapshot events use seconds, so distinct events can share a timestamp. For state changes where order matters, use a provider-supplied version or sequence number when available. Otherwise, consider retrieving the current resource state from the API and applying work against that state.
Duplicate handling is not the same as replay defense. Recording an event ID helps prevent repeated business processing; it does not prove that a request came from the trusted provider. Verify the sender separately. Signature and replay-defense details are covered by separate topics.
Example: fulfill an order after payment
Imagine a store that accepts payments with Stripe Checkout. It should not ship an order solely because the customer's browser returned to a success page. Stripe explains that a customer might pay successfully but never load that page, so a redirect alone is not a reliable fulfillment signal.
The server can receive checkout.session.completed, check the Checkout Session ID, and make fulfillment safe to run only once. With delayed payment methods, the actual payment result can arrive after the Checkout Session completes, so the integration should also consider checkout.session.async_payment_succeeded and checkout.session.async_payment_failed. Stripe's guidance says to retrieve the session, check payment_status, and make the fulfillment function safe when called more than once or concurrently for the same session ID. This example describes Stripe behavior; integrations with other payment providers must follow their own event contracts.
| Event received | Example of what to decide |
|---|---|
checkout.session.completed | Retrieve the session and check whether its payment state allows fulfillment |
checkout.session.async_payment_succeeded | Confirm the delayed payment succeeded, then fulfill the order |
checkout.session.async_payment_failed | Do not ship; notify the customer or take another follow-up action |
The key is not to treat all three events as the same “payment complete” signal. Events are notices of business-state changes; decide whether to fulfill using the provider's documented state and the session's current value.
Subscription, security, and recovery checklist
Subscribe only to the event types you need. GitHub and Stripe both warn that listening to unnecessary events increases processing load.
Use HTTPS and leave provider certificate verification enabled. Never put API keys or other secrets in the payload URL.
Check the event type and action before processing. GitHub, for example, identifies the event with `X-GitHub-Event` and may include an `action` at the top level of the body.
Record delivery ID, processing status, attempt count, last error, and business outcome so you can investigate duplicate or failed deliveries. Minimize sensitive payload data and follow a retention policy.
Automatic retry counts and durations vary by provider. For example, Stripe currently documents exponential retries for up to three days in live mode and three retries over a few hours in sandbox. Do not generalize this policy to another provider; recheck the product documentation when operating the integration.
Use delivery logs to find missed or failed events, and define how to redeliver them or reconcile event IDs against current resource state. Redelivery is still a duplicate, so the handler must be idempotent.
If a delivery appears to be missing, first check whether the provider's recent-deliveries log shows an attempt. If there is no attempt, confirm that the event occurred in the correct project or repository, that the event type is subscribed, and that the webhook is active. If an attempt failed, compare its HTTP status with server logs. GitHub notes that delivery records can take a few minutes to appear, so do not immediately conclude that no delivery occurred.
Sources
[GitHub Docs: About webhooks](https://docs.github.com/en/webhooks/about-webhooks)
[GitHub Docs: Best practices for using webhooks](https://docs.github.com/en/webhooks/using-webhooks/best-practices-for-using-webhooks)
[GitHub Docs: Troubleshooting webhooks](https://docs.github.com/en/webhooks/testing-and-troubleshooting-webhooks/troubleshooting-webhooks)
[Stripe Docs: Receive events in your webhook endpoint](https://docs.stripe.com/webhooks)
[Stripe Docs: Fulfill orders with Checkout](https://docs.stripe.com/checkout/fulfillment.md?ui=full-page)