Webhook Reliability: Preventing Duplicate Events and Actions

A webhook automation can appear to work perfectly during initial testing and still create problems after it goes live. An order may be processed twice, a notification may be sent more than once, or the same database record may be updated repeatedly.

These problems often happen because webhook delivery is not the same as exactly-once processing. A service may retry a request when it does not receive a successful response, even if the receiving application has already started working on the event.

The solution is to design webhook handlers so that repeated deliveries do not repeat the same business action. This is commonly achieved through idempotent processing, durable event records, and careful response handling.

Understand Why Duplicate Deliveries Happen

Webhook providers send HTTP requests to notify another application when an event occurs. For example, an online payment service may send an event when a payment succeeds or a subscription changes.

Delivery failures can happen for several reasons:

  • The receiving server is temporarily unavailable.
  • A network connection drops before the response arrives.
  • The application takes too long to respond.
  • A proxy or gateway interrupts the request.
  • The provider retries because it cannot confirm successful delivery.

A particularly important case occurs when the receiving application completes an action but fails to return a successful response. The provider may interpret the delivery as unsuccessful and send the same event again.

The application must therefore assume that the same event can arrive more than once.

Assign a Stable Identity to Every Event

The first step is to identify each webhook delivery using an event identifier supplied by the provider.

When a request arrives, extract the documented event ID after verifying the request. Store that ID in a persistent database or event-processing table.

A simplified record might contain:

  • Event ID
  • Event type
  • Received timestamp
  • Processing status
  • Completion timestamp
  • Error details, if processing failed

Before performing a business action, check whether the event has already been recorded or processed.

A database uniqueness constraint on the event ID is more reliable than checking an in-memory list. Memory can disappear when an application restarts, and separate application instances may not share the same state.

Make the Business Action Idempotent

Idempotency means that repeating an operation does not produce additional unintended effects after the first successful execution.

Consider an automation that receives a payment-success event and marks an order as paid.

A fragile implementation might create a new fulfillment record every time the webhook arrives. If the same event is delivered twice, two fulfillment records may be created.

A safer implementation associates the fulfillment action with a stable order or event identifier. Before creating a new record, the application checks whether the corresponding action already exists.

For operations that update an existing record, use conditional updates or database constraints where appropriate. The important point is to protect the actual business operation, not just the incoming webhook endpoint.

Separate Event Reception from Long-Running Work

Webhook endpoints should generally avoid performing lengthy operations before responding to the sender.

A practical architecture separates receiving an event from processing its business logic.

  1. Receive the webhook request.
  2. Verify its signature using the provider’s documented method.
  3. Extract and validate the event identifier.
  4. Store the event durably or place it in a reliable queue.
  5. Return the appropriate successful HTTP response.
  6. Process the queued event in a background worker.

This approach reduces the time spent inside the request handler and makes processing easier to monitor.

However, returning success should not happen before the application has safely recorded or queued the event. Otherwise, a server failure could result in an event being acknowledged but lost.

Handle Retries Without Repeating Side Effects

A duplicate event should not trigger a second payment, email, shipment, or other irreversible action.

If an event has already been durably processed, the handler can acknowledge the repeated delivery without running the business logic again.

If the event was received but processing failed, the system needs a different path. It should retain the original event, record the failure, and retry processing according to a controlled policy.

It is useful to distinguish between these states:

  • Received: The request has been accepted and recorded.
  • Processing: A worker is currently handling the event.
  • Completed: The required action finished successfully.
  • Failed: Processing stopped and requires retry or investigation.

A retry should resume the unfinished work rather than blindly repeat everything that happened earlier.

Do Not Assume Events Arrive in Order

Another issue is event ordering. Two related events may arrive in a different order from the order in which the underlying changes occurred.

For example, an application might receive an update notification followed by an older event that was delayed during delivery.

Simply applying every event in arrival order can leave the local database with outdated information.

When event ordering matters, use provider-documented version numbers or sequence information where available. In other cases, retrieve the current object state from the provider’s API before applying an important update.

Monitor Failed and Repeated Events

A reliable automation should make processing problems visible.

Keep logs that allow developers to trace an event from its initial delivery through its final processing result. Include the event ID, processing status, attempt information, and relevant error messages.

Useful monitoring checks include:

  • Events that remain in the processing state for too long.
  • Repeated delivery attempts for the same event.
  • Failed database writes.
  • Queue backlogs.
  • Actions that cannot be safely retried automatically.

Webhook security also matters. Verify signatures according to the provider’s instructions and protect against replay attacks. The OWASP Webhook Security Cheat Sheet discusses signature verification, replay protection, duplicate events, and idempotent processing.

Duplicate deliveries are a normal condition that webhook-based applications should be designed to handle. By combining stable event IDs, durable records, idempotent business actions, and clear retry rules, developers can make automation much more predictable even when networks and external services behave imperfectly.

← Back to Home