Recognize delivery retries before changing business logic
A webhook arrives twice and the application creates two tasks, emails, or records. The payloads may look identical, yet their request timestamps differ. This does not automatically mean the provider generated two business events. A delivery can be repeated after a timeout, a connection failure, or a manual redelivery. Treat repeated delivery as an expected integration condition.
Start by recording the provider, delivery identifier, event type, response status, and processing outcome. Keep those operational fields separate from private payload data. For GitHub, the X-GitHub-Delivery header identifies a delivery and remains useful when investigating redelivery. Other providers have different identity rules, so confirm their contract before choosing a key.
Put the duplicate check in the database
A common implementation first searches for an event, then inserts it if absent. Two requests can both pass that search before either inserts. Moving the same check into application memory only changes the race window and fails across processes. The database must enforce the invariant for every worker and request.
For a durable inbox, use a composite primary key containing the provider and delivery identifier. Save the verified payload and initial state atomically. The example accepts a new inbox item only when INSERT returns a row. A duplicate that was already durably accepted can receive a successful acknowledgement without running its side effects again.
CREATE TABLE webhook_inbox (
provider text NOT NULL,
delivery_id text NOT NULL,
payload jsonb NOT NULL,
state text NOT NULL DEFAULT 'pending',
received_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (provider, delivery_id)
);
INSERT INTO webhook_inbox (provider, delivery_id, payload)
VALUES ($1, $2, $3::jsonb)
ON CONFLICT (provider, delivery_id) DO NOTHING
RETURNING delivery_id;Separate acceptance from completion
Verify the webhook signature before storing or trusting any identifier. Validate the supported event type, limit request size, and store only what the worker needs. Commit the inbox transaction before returning success. If the write fails, return a retryable failure rather than pretending the event was accepted.
The inbox prevents duplicate acceptance; it does not magically make an external email or API call exactly once. A worker can crash after an external action succeeds but before its local status changes. Use the downstream service's idempotency mechanism where available, or design a reconciliation path with a stable operation identifier. Keep pending, processing, completed, and failed states visible.
Verify the behavior under concurrency
Use a development webhook and a harmless downstream action. Replay the same signed delivery sequentially and simultaneously. There should be one inbox row and one intended business effect. Then test a genuinely different delivery identifier to confirm that legitimate new work still proceeds.
Do not erase failed inbox rows to force another attempt. Preserve the original record and make retry state explicit. Define retention based on the provider's redelivery horizon and your operational needs; deleting identifiers too soon makes old redeliveries appear new. Inspect ambiguous side effects before retrying a partially completed job.
- Replay one delivery twice and inspect row count.
- Send two concurrent copies and check the unique constraint.
- Simulate an inbox write failure; confirm success is not returned.
- Crash a worker after an external action and test reconciliation.