Taking an order into n8n is easy. A shop posts to a webhook, you save the order, you pass it on to a supplier. The first version takes an afternoon and works every time you try it.
The trouble starts the first time something is slightly off. A field is missing. The shop’s server retries and sends the same order again. The supplier’s API takes ten seconds and then says nothing. n8n restarts between saving the order and sending it. None of those are exotic, and each one, handled badly, ends in a lost order, a double order or a silent hole.
So I built an intake that is meant to be boring when those things happen, and wrote down exactly what it does in each case. It is a demo for my own portfolio: the shop, the supplier and every order are made up. It runs on my self-hosted n8n, with Postgres in Docker next to it.
Answer fast, do the work after
The intake is four workflows. The first one receives the order and does only the quick, cheap things: check it, save it, reply. The slow, risky part, calling the supplier, is handed to a second workflow after the shop has already got its answer.
The reply tells the shop what happened:
- 400 if anything is wrong, with every problem listed, not just the first. Nothing is saved.
- 202 if the order is new and accepted.
- 200
duplicateif it has seen this exact order before. - 409
conflictif the order id has been used before with different content.
The 409 matters more than it looks. If the same order id arrives with a changed quantity, the easy thing is to overwrite the stored order. That is how an order quietly changes after you have already acted on it. The intake leaves the stored order alone and says so.
The same order, twice
Shops resend webhooks. It is normal behaviour, not a fault, so the intake has to treat a repeat as a non-event: one row, one supplier call, whichever way the copies arrive.
The harder case is two copies arriving at the same moment. A check that reads “does this order exist?” and then writes can pass for both copies. So the save is a single insert with ON CONFLICT DO NOTHING, followed by a separate read to find out which copy won. One gets a 202, the other a 200, and there is one row.
I would not have trusted that on reading it, so there is a test that opens two database connections and has them race to save the same order.
Not every supplier failure deserves a retry
Calls to the supplier fail in two different ways, and they want opposite treatment.
If the supplier times out, or answers 408, 429 or any 5xx, the order is probably fine and the supplier is having a bad moment. Those are retried, up to five attempts, with random waits of up to 1, 2, 4 and then 8 seconds. The randomness stops a burst of failed orders from all retrying at the same instant.
If the supplier answers 400, 401, 403, 404, 409 or 422, it is telling you the order itself is wrong or you are not allowed to send it. Retrying would just repeat the same refusal five times. The order is held straight away for a person to look at.
If the supplier sends a Retry-After header, the intake obeys it up to 30 seconds. For anything longer, the order is held until then rather than sitting in a workflow.
The call that worked, and the reply that got lost
This is the failure that causes double orders. The supplier receives the order, creates it, and the reply never makes it back. From the intake’s side that looks identical to a failed call. If it retries naively, the supplier gets the order twice.
The fix is an Idempotency-Key on every call. A retry sends the same key, and a supplier that supports it recognises the order and returns the same reference instead of creating another. The fake supplier in the demo behaves this way, so there is a test for exactly this case.
What happens to an order that keeps failing
After a round of five attempts, an order that has not gone through is held, not dropped. A separate workflow, Retry Held, runs every 10 minutes and picks up held orders that are due. It tries again after 10 minutes, then 30 minutes, 1 hour, 3 hours and 6 hours, and only then marks the order failed.
A Telegram alert goes out the first time an order is held and again if it finally fails. It does not go out on every retry, because an alert that arrives forty times gets muted.
Two more cases fall out of the same workflow:
- n8n restarts halfway through an order. An order stuck in “accepted” for 15 minutes is picked up by Retry Held, so a restart delays an order and does not lose it.
- Two Retry Held runs overlap. The claim uses
FOR UPDATE SKIP LOCKED, so each held order is taken by exactly one run.
Every step writes a row to an events table with the n8n execution id. If anyone asks what happened to an order, the answer is one query.
How I tested it
A workflow that handles failures is only as good as your evidence that it does. A few choices made that evidence cheap:
- The logic lives in plain JavaScript files, not in the editor. A build script writes the workflow JSON from them, so the code I tested is the code that gets imported. A check fails if the exported workflows ever drift from what the build would write.
- Every database write is a Postgres function, tested against a throwaway local Postgres, including the race above.
- The fake supplier is scripted by the order. A test order’s data says what the supplier should do:
503,500,429:<seconds>,timeout,422orok. That lets a test provoke each failure on demand instead of waiting for a real supplier to misbehave. - Live tests run against the real workflows, each with its own run id and fresh order ids, and delete their own rows afterwards.
I also left Postgres going down as a manual test, and said so. Put a wrong password in the credential and send an order: the caller gets a 500 and the error alert arrives. Put the password back and send the same order: it is accepted once.
What it does not do
- The supplier is another n8n workflow. A real one would need its own field mapping and authentication.
- It takes one order per call. There is no batch intake.
- An order held for a person waits until someone fixes it and sets its next try time in the database. There is no screen for that.
- Retry Held runs every 10 minutes, so a due order can wait up to 10 minutes past its time.
- Alerts go to Telegram only.
What I would take from it
- Decide what the shop is told in every case, and make sure each answer is different.
- Treat a repeated webhook as normal. Make saving atomic, not check-then-write.
- Split supplier failures into “try again” and “a person has to look”, and never retry the second kind.
- Send an idempotency key, so a lost reply cannot become a double order.
- Hold, do not drop. A failed order that someone can see is better than one that vanished.
- Make failure testable by scripting it, so you are not waiting for the real thing to go wrong.
None of this is clever. It is the list of things that go wrong with every intake sooner or later, with a decision made in advance for each one.