Duplicate orders

Hi
I have an n8n workflow that receives webhook events and creates orders in PostgreSQL.
Normally it works fine, but during high traffic I can sometimes have two webhook executions for the same order running almost simultaneously. Both executions check whether the order exists, both see that it doesn’t, and both create a new record.
The simplified flow is:
Webhook
↓
Validate Order
↓
Check PostgreSQL
↓
Create Order
↓
Call Payment API
I’m considering adding a unique constraint on external_order_id and using:
INSERT INTO orders (
external_order_id,
customer_id,
amount
)
VALUES ($1, $2, $3)
ON CONFLICT (external_order_id)
DO NOTHING
RETURNING id;
How would you handle the case where the first workflow creates the order but crashes before calling the payment API?
Should the payment request also have its own idempotency key?Would you handle this entirely in PostgreSQL or add an idempotency layer inside n8n?

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

@Gabby_Timothy

You can do this

You need a Status Column (e.g., pending, processing, completed) in your PostgreSQL table to act as a state machine.

Yes, absolutely. This is your final line of defense against double-charging if a crash occurs after the API call succeeds but before the database is updated.

Hey there @Gabby_Timothy make the database the final authority for idempotency, rather than relying only on an n8n “check if it exists” step.

The unique constraint is important because two executions can still pass the existence check at almost the exact same time.

INSERT INTO orders (
external_order_id,
customer_id,
amount,
status
)
VALUES ($1, $2, $3, ‘created’)
ON CONFLICT (external_order_id)
DO NOTHING
RETURNING id;

If no row is returned, another execution already created that order, so the workflow should stop or fetch the existing record instead of creating another one.

For the payment API, I’d use the same external order ID as an idempotency key if the API supports it

That protects you from another failure scenario: the payment succeeds, but n8n crashes before saving the response. When the workflow retries, the payment provider can recognize the same idempotency key instead of charging twice.

So I’d generally use two layers:PostgreSQL unique constraint → prevents duplicate orders

Payment API idempotency key → prevents duplicate side effects

Hi @Gabby_Timothy
The Retry button cannot stand in for a sweeper here. Both retry options replay the previous execution data through the same nodes, so the insert runs again, conflicts, returns no row, and the IF stops the run. The retry finishes green having never reached the payment call, which reads as though the order was already handled.
Recovery has to start from the row rather than from the execution, which is what the status column buys you.