Why your n8n webhook runs twice (and the one-statement fix)

Webhook providers deliver at least once. Why n8n processes the same event twice, why check-then-insert fails, and a race-safe Postgres fix.

By StackSignal ·

You built a clean workflow: Stripe sends order.paid, n8n creates the invoice and emails the customer. It works for weeks. Then a customer gets two invoices.

Nothing is wrong with your logic. Duplicates are a property of webhooks, and the fix belongs in your workflow, not in the provider's.

Why it happens

1. Providers deliver at least once. Most webhook senders promise that an event arrives, not that it arrives exactly once. If they don't get a timely 2xx, they resend. Network blips and deploys on either side cause the same thing.

2. Your workflow may be the one causing retries. If the Webhook node answers only after the last node finishes, a 20-second workflow can blow past a provider's timeout. The provider gives up on the first delivery, sends again, and both runs complete.

3. Parallel deliveries race. The usual fix is "look up the event id; if it exists, stop; otherwise insert it and continue". That works for duplicates that arrive minutes apart. When two copies arrive within the same second, both lookups run before either insert, both find nothing, and both continue.

4. n8n's $execution.id isn't an event id. Every delivery and every retry gets a new execution id, so deduplicating on it removes nothing.

Step 1: respond first

Set the Webhook node to respond Immediately, or put a Respond to Webhook node right after the trigger. Do the slow work after the response. That removes the retries you were causing yourself.

Step 2: pick a stable key

Use the id the provider assigns to the event:

ProviderKey
Stripebody.id (evt_…)
ShopifyX-Shopify-Webhook-Id header
GitHubX-GitHub-Delivery header
Form toolsthe submission/response id
Your own APIrequire an Idempotency-Key header

No id available? Hash the fields that define "the same request" (e.g. email + amount + date).

Step 3: let the database refuse the second one

The only check that can't race is one the database enforces. A primary key on the event id, and a single statement that inserts the claim or reports that it already exists:

CREATE TABLE IF NOT EXISTS webhook_events (
  key          TEXT PRIMARY KEY,
  status       TEXT NOT NULL,            -- claimed | done
  lease_until  TIMESTAMPTZ NOT NULL,
  completed_at TIMESTAMPTZ
);

-- claim: returns a row only for the run that should proceed
INSERT INTO webhook_events (key, status, lease_until)
VALUES ($1, 'claimed', now() + interval '5 minutes')
ON CONFLICT (key) DO UPDATE
  SET status = 'claimed', lease_until = EXCLUDED.lease_until
  WHERE webhook_events.status = 'claimed' AND webhook_events.lease_until < now()
RETURNING key;

In n8n, run this in a Postgres node (Execute Query, with $1 set to your key) right after the respond step. If it returns a row, continue. If it returns nothing, stop: the event is done or another run is handling it. After your side effect succeeds:

UPDATE webhook_events SET status = 'done', completed_at = now() WHERE key = $1;

Why the lease: if a run crashes after claiming, the claim expires and the provider's next retry can take over. Nothing gets stuck, and there's nothing to clean up by hand.

Set the Postgres node's "Always Output Data" setting if you want to branch on the empty result, rather than letting the flow stop there.

Why not the Remove Duplicates node, static data, or a Google Sheet?

  • Remove Duplicates compares against previous executions. That's handy for list processing, but it has none of the atomic claim semantics above for concurrent webhooks.
  • Workflow static data: n8n's docs say it isn't saved in test runs and may be unreliable under high-frequency executions.
  • A spreadsheet has no unique constraint, so the race is back.

Belt and braces for money

If the downstream API supports idempotency keys (Stripe and many payment APIs do), send the same event id as an Idempotency-Key header too. Then a bug in your dedupe and a provider retry both have to happen before money moves twice.

How we tested it

We pointed 25 simultaneous claims for one event at this pattern on Postgres 16. Exactly one got "process"; the other 24 were told it was in progress. The test is part of the open test suite in our kit (tests/sql-tests.js) if you want to reproduce it.

Check the rest of your instance

Duplicates are one of eight things our free n8n Reliability Audit checks. It's a read-only workflow that grades every active workflow A–F and lists what to fix first. Get the free audit →

If you'd rather not build it yourself, the Idempotency Gate in the Reliable Automation Kit is this pattern packaged as a drop-in sub-workflow: claim, complete and release actions, duplicate counters, and the daily report that shows how many duplicates it stopped.

Related