Idempotent booking writes from n8n to a mock calendar
Apply each approved proposal as exactly one mock booking, even when an acknowledgement is lost and the action is retried.
By Invicly Tech, , in n8n, AI Automation and Voice Agents. About 8 minutes to read.
In short
Idempotent means doing something twice has the same result as doing it once. Networks lose replies, so a retry can create a second booking unless the write is protected. Use a stable action key, store the outcome, and reconcile before you retry.
Think of it like this: It is like a numbered cinema ticket. Showing it twice still admits one person.
Explained simply
Computers retry things. If a request times out, the sender does not know if it worked, so it tries again. For a booking this is dangerous: the first call may have worked and only the reply was lost, and the retry makes a second booking.
Idempotent is the word for fixing this. An operation is idempotent if doing it twice leaves the same result as doing it once. Setting a light switch to on is idempotent. Adding a booking is not, unless you protect it.
The protection is a key. Before you write, you claim an action key that comes from stable facts, such as the proposal id and version. A database rule says each key can exist only once. A second attempt with the same key finds the first and stops.
Also keep the outcome. Store whether the action started, applied or failed. After a timeout you look up the booking by key. If it exists, mark it applied. If not, retry safely. Then check that every approved proposal has exactly one booking.
Press Play on a diagram to walk through it one step at a time.
- n8n to Action log: Insert action key (once)1Insert action key (once)
- n8n to Mock calendar: Create booking with the key2Create booking with the key
- Mock calendar to n8n: Reply lost3Reply lost
- n8n to Mock calendar: Retry with same key4Retry with same key
- Mock calendar to n8n: Returns the existing booking5Returns the existing booking
- n8n to Action log: Store outcome applied6Store outcome applied
- Timeout then retry
- Calendar creates a second booking
- Customer gets two slots
- Hard to detect later
- Timeout then lookup by key
- Existing booking is found
- Only one booking exists
- Outcome stored in the actions table
In depth
Stable keys
The key must be the same for every attempt of the same action and different for different actions. Good: proposal id plus version, for example prop_42:v2:create_booking. Bad: a random id made on each run, or the execution id, because a retry may be a new execution. Keep the key in your actions table with a unique constraint. If the calendar supports an idempotency key header on writes, send the same value so the calendar also recognises repeats. This idea is described in an IETF draft for an Idempotency-Key header, but check whether your target API supports it.
- Same action, same key, every attempt
- Built from proposal id and version
- Unique constraint in your database
- Send it to the calendar if supported
Persist the outcome with ON CONFLICT
Insert the action row first with status started, using INSERT ... ON CONFLICT DO NOTHING. If a row comes back, you own the action and may call the calendar. If nothing comes back, another attempt already claimed it, so you read the stored status instead of writing again. After the calendar answers, update the row to applied with the booking id, or to failed with the error. This gives you a durable record that survives crashes.
- Claim before you write
- No row returned means already claimed
- Update to applied with booking id
- Record the error on failure
- Step 1Insert action key
ON CONFLICT DO NOTHING
- Step 2Row returned?
No means already claimed
- Step 3Call calendar with key
- Step 4Update action row
applied or failed
Reconcile a lost acknowledgement
A timeout leaves you unsure. Do not retry blindly and do not assume failure. Ask the calendar whether a booking with your key exists. If yes, record applied and move on. If the calendar says no, retry with the same key. If the calendar cannot be asked, mark the action unknown and queue it for a person. Reconciliation can also run on a schedule, comparing your applied actions with the calendar and listing differences, which catches problems nobody noticed.
- Look up by key after a timeout
- Exists: record applied
- Missing: retry with the same key
- Unknown: send to a person
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Use stable event and action keys~35m
- 2Persist the action outcome~35m
- 3Use an idempotency key on writes~35m
- 4Reconcile a lost acknowledgement~35m
- 5Verify one booking per proposal~35m
Key points
- Build the key from stable facts, for example the proposal id plus version, never a random value per attempt.
- A unique constraint in Postgres, with INSERT ... ON CONFLICT, stops a second action row.
- Send the key to the calendar as an idempotency key if it supports one.
- After a timeout, look up the booking by key before retrying.
Key terms
Tap a card to flip it. 0/4 known
Examples
INSERT INTO actions (action_key, proposal_id, status)
VALUES ($1, $2, 'started')
ON CONFLICT (action_key) DO NOTHING
RETURNING action_key;
-- no row returned: someone else already claimed itClaim the action once
The lost reply, step by step
n8n claims key prop_42:v2 and calls the mock calendar. The calendar creates booking b-7 but the reply never arrives. n8n times out and looks up key prop_42:v2. The calendar returns b-7. n8n updates the action row to applied with b-7. A final check counts one booking for proposal 42.
Claim the key and verify
The claim query and a verification query that must return one for each applied proposal.
CREATE TABLE actions (
action_key text PRIMARY KEY,
proposal_id uuid NOT NULL,
status text NOT NULL,
booking_id text
);
INSERT INTO actions (action_key, proposal_id, status)
VALUES ($1, $2, 'started')
ON CONFLICT (action_key) DO NOTHING RETURNING action_key;
-- verify: should be 1 for each applied proposal
SELECT proposal_id, count(*) FROM actions
WHERE status = 'applied' GROUP BY proposal_id;Quick reference
- Idempotent: doing it twice equals doing it once
- Key = stable facts, such as proposal id plus version
- Never make a new random key per retry
- INSERT ... ON CONFLICT DO NOTHING claims the action once
- After a timeout: look up by key before retrying
- Verify: one booking per approved proposal
Common mistakes
- Generating a new key on every retry, which defeats the protection.
- Retrying blindly after a timeout without checking if the first call worked.
- Marking the action applied before the write is confirmed.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write the action key for proposal 42, version 3, action create_booking.
- Exercise 2medium
Create the actions table and run the claim query twice with the same key.
- Exercise 3hard
Simulate a lost reply: a mock calendar that creates a booking but fails the response once. Show your workflow ends with one booking.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is a database unique constraint enough?
- It protects your records. The downstream system also needs an idempotency key or a lookup, or it may still create two bookings.
- Does n8n's own retry setting cause duplicates?
- It can, if the write is not idempotent. Retry On Fail repeats the call, so protect the write first.
- What if the calendar has no lookup and no idempotency key?
- Mark ambiguous outcomes as unknown and let a person check, rather than guessing.