Distributed Systems / Backend
Make webhook retries safe with a transaction
Deduplicate webhook deliveries, commit work durably, and use an outbox when an event triggers email or updates another service.

A workshop booking is confirmed and your service sends the student an email. The service fails before the sender receives a successful response. When the webhook is delivered again, the student should keep one booking and receive one confirmation. The receiver must be able to distinguish work that finished from work that still needs to happen.
Give each delivery a durable identity
Use the identifier documented by the service that sends the event. For example, GitHub redeliveries keep the same X-GitHub-Delivery value. Stripe recommends tracking processed event IDs and notes that separate event objects can sometimes describe the same underlying object and type. Both cases matter. The event ID protects ingestion. A separate key such as booking ID plus notification type protects the business operation. GitHub webhook best practices · Stripe webhook duplicate handling
Authenticate deliveries using the provider's supported signature verification. Verify the raw request body when the provider's verifier requires it. Once verified, handle the event types your application expects and prefer storing the minimal fields your receiver needs.
Let the database decide which arrival wins
Two requests can arrive concurrently. Checking for an existing row and writing it later leaves a window in which both requests see an empty result. Use a unique constraint as the arbiter.
CREATE TABLE webhook_receipt (
provider text NOT NULL,
event_id text NOT NULL,
received_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (provider, event_id)
);
CREATE TABLE notification_outbox (
booking_id bigint NOT NULL,
kind text NOT NULL,
queued_at timestamptz NOT NULL DEFAULT now(),
sent_at timestamptz,
PRIMARY KEY (booking_id, kind)
);
The sample below represents an already verified booking confirmation. Run it as one database statement. The parameters contain the provider name, the event ID and the booking ID. The CTE returns a row only when the receipt is new. The outbox has a second uniqueness check for distinct events that describe the same booking confirmation.
WITH incoming AS (
INSERT INTO webhook_receipt (provider, event_id)
VALUES ($1, $2)
ON CONFLICT (provider, event_id) DO NOTHING
RETURNING event_id
)
INSERT INTO notification_outbox (booking_id, kind)
SELECT $3, 'booking-confirmed'
FROM incoming
ON CONFLICT (booking_id, kind) DO NOTHING;
If the event must also change the booking's local state, do that work in the same transaction before acknowledging success. If the transaction rolls back, a retry can safely do the work. If it committed before the response was lost, the retry encounters the existing receipt and can return success. PostgreSQL INSERT and ON CONFLICT
Keep the external side effect retryable
A worker picks up the durable outbox entry after the transaction commits. It can retry timeouts and temporary failures without keeping the webhook connection open. Use a lease or suitable database locking when more than one worker can claim jobs. Record successful completion. When the downstream provider supports an idempotency key, pass a deterministic value derived from the booking and notification kind.
A worker can still stop after the provider reports success and before the worker records it. The outbox solves the transaction boundary between your own tables. A downstream idempotency key or a reconciliation procedure handles ambiguity at the external boundary. Also protect state transitions from stale events if the provider can deliver events out of order.
Test the failure windows
Send the same signed fixture twice, first serially and then with overlapping requests. Send two different event IDs for the same confirmed booking. Simulate a process restart after the database commits and simulate an external timeout. Inspect database rows and worker attempts. A successful HTTP response alone does not prove the business effect happened once.
Cover illustration by ACM MPSTME.