All Articles
Backend Reliability·November 2026·4 min read

Designing Idempotent Payment Webhooks That Survive Duplicate Retries

Payment providers use at-least-once delivery. Here is how to combine signature verification with atomic PostgreSQL event claiming so retries never double-process an order.

StripeWebhooksPostgreSQLAPI Design

Why Every Webhook Endpoint Will Receive Duplicates

Providers like Stripe guarantee at-least-once delivery, not exactly-once delivery. If your server processes a checkout.session.completed webhook, marks the order paid, and experiences a brief network hiccup before returning HTTP 200, Stripe assumes delivery failed and sends the exact same event ID again.

Even worse, two webhook deliveries for the same event can arrive concurrently if a timeout threshold is crossed.

Verify Signatures on Raw Bytes First, Claim Event IDs Second

First, always verify the cryptographic HMAC signature against the unparsed raw request body before touching the database. Parsing and re-serializing JSON alters whitespace or key order and invalidates signatures.

Second, record the provider's unique event_id in a dedicated table with a PRIMARY KEY or UNIQUE constraint using INSERT ... ON CONFLICT DO NOTHING inside the same transaction that transitions order state.

Atomic webhook claiming pattern in PostgreSQLsql
BEGIN;

INSERT INTO processed_webhook_events (provider, event_id, received_at)
VALUES ('stripe', $1, NOW())
ON CONFLICT (provider, event_id) DO NOTHING
RETURNING event_id;

-- If 0 rows are returned, this event was already handled (or is handled concurrently):
-- commit immediately and return HTTP 200 OK without side effects.

Keep External Side Effects Out of the Critical Transaction

A common mistake is sending confirmation emails or calling slow third-party APIs right inside the webhook database transaction. If the email service hangs for 10 seconds, your database row locks stay open and Stripe times out—triggering yet another retry.

Commit the order state transition and event claim fast, return 200 OK, and trigger downstream notifications asynchronously via an outbox table or background worker.

Key Takeaway

Treat every incoming webhook as both untrusted (verify HMAC first) and repeatable (claim event_id atomically with ON CONFLICT DO NOTHING).

Written by Prethu Raj Debnath

Full-Stack & Backend Developer · Questions or feedback? Reach out anytime.