Idempotency in GTM Workflows: Stop Duplicate Records and Double Emails
Idempotency means running a workflow twice has the same effect as running it once. Here is why GTM automations fire twice, and four patterns that stop duplicate records and double emails.
Idempotency means running an operation twice has the same effect as running it once. In GTM workflows, it's what stops a retried webhook from creating a second contact, a second task or a second cold email. Build it with four patterns: upsert on a unique ID, idempotency keys, a ledger of processed events and a state check before every side effect.
Here's why it matters. Every serious automation tool retries. Webhooks arrive twice. Users double-click. A timeout makes you unsure if the last call worked. Without idempotency, each of those becomes a duplicate record or a prospect who gets the same email twice.
This explainer covers where duplicates come from, the four patterns, a code example, the money math and a checklist you can run against every workflow you own.
Key takeaways
- Assume every event arrives twice. Webhooks and retries deliver at least once, not exactly once.
- Upsert records on a unique ID. Never create blind.
- Key every side effect. Emails, tasks and charges need their own repeat check.
- Build keys from the event. A new random key on each retry defeats the point.
- Outlast the retry window. Keep processed IDs longer than any sender retries.
What is idempotency?
MDN's definition of idempotent is the clearest: a method is idempotent if the intended effect of one request is the same as the effect of several identical requests. In plain words, repeats are harmless.
HTTP methods differ here. Per MDN, safe read methods like GET, plus PUT and DELETE, are idempotent. POST and PATCH are not guaranteed to be.
| Method | Idempotent? | GTM example |
|---|---|---|
| GET | Yes | Read a contact |
| PUT | Yes | Set a property to a fixed value |
| DELETE | Yes | Archive a record (second call finds it gone) |
| POST | No | Create a contact, send an email |
| PATCH | Not guaranteed | Add 10 to a score |
Most GTM writes are POSTs. "Create contact," "enroll in sequence" and "create task" all add something new each time. So idempotency is your job, not the protocol's. If the REST API basics are new, the API basics for GTM guide covers them.
Why do GTM workflows run twice?
Because delivery is "at least once" almost everywhere. Here are the common causes.
| Cause | What happens | Example |
|---|---|---|
| Webhook retries | Sender didn't get a fast success, so it sends again | HubSpot retries failed webhook notifications |
| Workflow retries | A step failed on a rate limit and runs again | HubSpot custom code retries on 429 and 5XX |
| Timeouts | Your call worked, but the reply was lost, so you retry | A CRM write times out after it committed |
| Replays | Someone re-runs a failed batch or replays a zap | Manual replay after an outage |
| Human repeats | Double form submit, double click on "send" | A buyer submits a demo form twice |
The numbers make this concrete. HubSpot's error handling docs say it retries failed webhook notifications up to 10 times over about 24 hours. It counts connection failures, responses slower than 5 seconds and any 4xx or 5xx response as failures. The same page says custom code workflow actions retry on rate limits and 429 or 5XX errors for up to three days.
Retry logic is good. It's how systems survive outages. But retries plus non-idempotent handlers equal duplicates. The webhook basics guide covers the sender side.
The four patterns that make workflows idempotent
Use all four. Each one covers a different kind of repeat.
1. Upsert on a unique ID
Never create a record blind. Match on a unique ID and update if it exists. HubSpot's object API guide describes batch upsert: you pass an idProperty and an id, and HubSpot updates existing records and creates the rest. It notes that partial upserts aren't supported when email is the idProperty for contacts, so use a custom unique property for partial updates.
Per HubSpot's properties guide, you can have up to ten unique ID properties per object. Store the source system's ID, like a Stripe customer ID or product user ID, in one of them. The HubSpot custom objects guide shows this for synced records, and HubSpot imports without duplicates covers the CSV side.
2. Idempotency keys on outbound calls
When you call an API that accepts keys, send one. Stripe's idempotent requests docs are the model. You send an Idempotency-Key header on POST requests. Keys can be up to 255 characters. For its v1 API, Stripe saves the status code and body of the first request and returns it for retries with the same key, even if it was a 500 error. Keys can be pruned after 24 hours. If a retry's parameters differ from the original, Stripe returns an error.
Stripe also says not to use sensitive data, like email addresses, as keys. Build them from IDs instead.
3. A ledger of processed events
Most GTM tools don't accept idempotency keys. So keep your own ledger. Before you handle an event, try to insert its key into a table with a unique constraint. If the insert fails, you've seen it before, so stop.
4. A state check before every side effect
Records can be upserted. Emails can't be unsent. Before any side effect, check state: is the contact already in this sequence? Does a task for this event already exist? Is the "welcome sent" flag already true? This is your last line of error handling.
What does an idempotent handler look like?
Here's a trimmed webhook handler for a demo form. It acknowledges fast, dedupes on a key built from the event and checks state before it sends.
// Express-style handler. db.ledger has a UNIQUE index on idem_key.
app.post("/webhooks/demo-request", async (req, res) => {
const evt = req.body;
// Key comes from the event, so every retry carries the same key.
const idemKey = `demo_request:${evt.source}:${evt.submission_id}`;
try {
await db.ledger.insert({ idem_key: idemKey, received_at: new Date(), status: "processing" });
} catch (e) {
if (e.code === "UNIQUE_VIOLATION") return res.status(200).send("duplicate ignored");
throw e;
}
res.status(200).send("ok"); // acknowledge well under 5 seconds
queue.push({ idemKey, evt }); // do the slow work in the background
});
async function processDemoRequest({ idemKey, evt }) {
const contact = await crm.upsertContact({ idProperty: "email", id: evt.email, properties: evt.fields });
if (!(await crm.hasOpenTask(contact.id, idemKey))) {
await crm.createTask(contact.id, { subject: "Call demo request", external_key: idemKey });
}
await db.ledger.update(idemKey, { status: "done" });
}
And here's the ledger row it writes. Keep the key readable, because you'll search for it when something goes wrong.
{
"idem_key": "demo_request:website_form:sub_7f3a91",
"received_at": "2026-12-02T16:04:12Z",
"status": "done",
"attempts_seen": 3,
"expires_at": "2026-12-09T16:04:12Z"
}
The attempts_seen count is a free health check. If many events show 3 or more, your handler is too slow and the sender is retrying.
Worked example: what duplicates cost
Say your form and product webhooks send 2,000 events a day into HubSpot. Assume 1% arrive twice because of retries and slow responses. That's 20 duplicates a day, or about 600 a month.


With no protection, all 600 create something twice: a duplicate contact, a second task or a second email. Add upsert on records, and the duplicate contacts stop. But side effects still fire, say 120 a month of double tasks and emails. Add a ledger and state checks, and you're down to the rare race, say 2 a month.
| Protection | Duplicate side effects a month | What still leaks |
|---|---|---|
| None | 600 | Records, tasks and emails |
| Upsert only | 120 | Tasks and emails |
| Upsert plus ledger and state checks | 2 | Rare races between parallel workers |
Now the cost. Say each duplicate contact or task takes a rep or ops person 4 minutes to find and fix. 600 a month is 40 hours. At an assumed $60 an hour, that's $2,400 a month of cleanup. The bigger cost is invisible: a prospect who gets two "thanks for your demo request" emails starts to doubt you before the call.
How do you build good idempotency keys?
The key must be the same on every retry of the same event, and different for different events. Build it from facts in the event, not from the clock or a random number generated at handling time.
| Key recipe | Good? | Why |
|---|---|---|
| Source event ID | Yes | The sender repeats it on every retry |
| Object + record ID + action + business date | Yes | Stable, readable, unique per action |
| Random UUID made when the request arrives | No | Each retry gets a new one |
| Current timestamp | No | Changes between retries |
| Email address alone | No | Not unique per action, and it's personal data |
Then size the window. Keep ledger rows longer than any retry that can reach you. HubSpot webhooks retry for about a day, and custom code for up to three days, so I keep seven. The enrichment worker in CRM enrichment automation uses the same idea to avoid paying twice for one lookup.
The idempotency checklist
Run this against every workflow that writes to your CRM or sends anything.
| # | Check | Pass looks like |
|---|---|---|
| 1 | Creates use upsert | Every record write matches on a unique ID |
| 2 | Events carry a stable key | Key built from the source event, logged |
| 3 | Ledger exists | Unique index on the key, retained 7 days |
| 4 | Side effects check state | No email, task or enrollment without a prior check |
| 5 | Handler acks fast | Success returned in well under 5 seconds |
| 6 | Outbound calls send keys | Keys sent wherever the API accepts them |
| 7 | Duplicates are measured | Weekly count of ignored repeats on a dashboard |
The last check is about observability. A spike in ignored duplicates is often the first sign that a handler slowed down. The GTM ops observability guide covers dashboards and alerts.
Common mistakes
- Trusting "exactly once." Plan for at least once, always.
- Deduping only records. Deduplication of contacts doesn't stop double emails.
- Random keys per attempt. The key must come from the event.
- Short ledgers. A one-hour window misses a retry that comes back the next day.
- Slow handlers. Doing enrichment inside the webhook call triggers retries. Queue it.
- Counters as writes. "Add 10 to score" isn't idempotent. Recompute scores from events instead.
No-code tools need the same thinking. In n8n or Zapier, add a lookup step before every create, and store a key in a sheet or table. The n8n guide and Zapier for RevOps show where those steps go. This is event-driven architecture done safely, and it belongs in your data contracts.
What to do this week
- List every webhook and workflow that creates records or sends messages.
- Add a unique ID property for each source system, and switch creates to upserts.
- Add a ledger table with a unique key and a seven-day expiry.
- Move slow work out of webhook handlers into a queue.
- Add a state check before every email, task and enrollment.
- Chart ignored duplicates weekly.
The webhooks for GTM playbook, the Stripe to CRM webhook playbook and the form to CRM webhook playbook all build these patterns in. Duplicate-free routing also matters most where speed counts, like speed to lead. If you want your workflows audited and hardened, that's part of our GTM engineering work.
FAQ
What does idempotency mean in simple terms?
It means doing the same thing twice has the same effect as doing it once. Pressing an elevator button five times still calls one elevator. In a GTM workflow, it means a webhook that arrives twice still creates one contact, sends one email and books one task, no matter how many times the event is delivered or retried.
Why do webhooks arrive more than once?
Most senders deliver at least once, not exactly once. If your endpoint is slow, times out or returns an error, the sender retries. HubSpot, for example, retries failed webhook notifications up to 10 times over about 24 hours, and treats responses slower than 5 seconds as failures. So the same event can reach you several times.
What is an idempotency key?
It is a unique value sent with a request so the receiver can spot a repeat. Stripe's API, for example, accepts an Idempotency-Key header on POST requests and returns the saved result if the same key comes back. Build the key from the event itself, like the source event ID, so every retry carries the same key.
Is upsert the same as idempotency?
Upsert makes record writes idempotent: the same input twice updates one record instead of creating two. But it doesn't protect side effects like sending an email, creating a task or charging a card. Those need their own check, usually a ledger of processed event IDs or a sent flag on the record.
How long should you keep processed event IDs?
Keep them longer than the longest retry window that can reach you. HubSpot webhooks retry for about 24 hours and HubSpot custom code actions can retry for up to three days. A seven-day window covers both with room to spare. Add a cleanup job so the table doesn't grow forever.