---
title: "Why making every Activity idempotent doesn’t make your Workflow idempotent"
description: "Idempotent Activities are the first step. Use Workflow IDs and derived Activity keys to make a Temporal Workflow idempotent across retries and Resets."
canonical: "https://temporal.io/blog/idempotent-workflows-temporal.md"
source: "https://temporal.io/blog/idempotent-workflows-temporal"
published: "2026-12-02"
authors:
  - "Mostafa Ibrahim"
category: "How-To"
tags:
  - "Retries"
  - "Durable Execution"
  - "Architecture"
  - "Typescript"
reading_time_minutes: 9
---

# Why making every Activity idempotent doesn’t make your Workflow idempotent

| TL;DR |
| ---------- |
| In Temporal, an idempotent Activity makes its own retries safe, but it doesn’t make the Workflow idempotent. Keys created per Run don’t carry over to a new Run from a Workflow retry or Reset, or to a duplicate start. To make a Workflow idempotent, use a business ID as the Workflow ID and build every Activity key from it. |

You did what everyone says to do. You set up your e-commerce checkout Workflow with three Activities, one to reserve the stock, one to charge the card, and one to send the receipt, and each takes an idempotency key and passes it to the service it calls. Retries are safe, so you sent it and moved on.

But then the dreaded support ticket arrives. A customer was charged three times and never got a receipt. You open the Workflow and find it failed on the receipt step, which explains the missing email but not the charges. The charge Activity passed its key to the payment provider and never retried… so where did the other two charges come from?

You made your Activities idempotent, which is what everyone says to do. But idempotent Activities don’t add up to an idempotent Workflow, because a key only protects the layer that created it. Generate it per Run and a second Run gets a second key.

## A checkout Workflow where every Activity is idempotent

Three steps: reserve the stock, charge the card, send the receipt.

```typescript
// workflows.ts
import { proxyActivities, uuid4 } from '@temporalio/workflow';
import type * as activities from './activities';
import type { Cart } from './types';

const { reserveStock, chargeCard, sendReceipt } = proxyActivities<typeof activities>({
  startToCloseTimeout: '1 minute',
  retry: { nonRetryableErrorTypes: ['CardDeclinedError'] },
});

export async function checkout(cart: Cart): Promise<void> {
  await reserveStock(cart.items, uuid4());
  const charge = await chargeCard(cart.total, cart.paymentMethod, uuid4());
  await sendReceipt(cart.email, charge.id, uuid4());
}
```

The charge Activity passes that key straight through to the payment provider:

```typescript
// activities.ts (reserveStock and sendReceipt omitted)
import { paymentClient } from './payments';

export async function chargeCard(amount: number, paymentMethod: string, idempotencyKey: string) {
  return paymentClient.create(
    {
      amount,
      currency: 'usd',
      payment_method: paymentMethod,
      confirm: true,
      automatic_payment_methods: { enabled: true, allow_redirects: 'never' },
    },
    { idempotencyKey },
  );
}
```

Every Activity passes its key to the outside service, which returns the original result on a repeat. That covers an Activity retry: a timeout fires after the provider took the charge, so the attempt reruns and the key catches it. A Run is one execution of `checkout`. A Workflow retry or a Reset starts a new Run under the same Workflow ID, and so does Continue-as-New. `uuid4()` inside Workflow code is seeded per Run, so a Worker restart replays history and gets the same three keys. A Workflow retry is a fresh Run with no history to replay. Temporal’s own [post on idempotency](https://temporal.io/blog/idempotency-and-durable-execution.md) suggests the Run ID plus the Activity ID as a fallback key, and its [Idempotent Workflows section](https://temporal.io/blog/idempotency-and-durable-execution.md#idempotent-workflows) recommends business IDs as Workflow IDs. That fallback key survives Activity retries but dies with the Run. This checkout’s `uuid4()` keys die with it too.

## Why a Workflow retry charges the customer again

The handler that starts `checkout` has a [Retry Policy on the Workflow itself](https://docs.temporal.io/encyclopedia/retry-policies). You’ve probably set one yourself. I have. It feels like free reliability.

```typescript
import { randomUUID } from 'node:crypto';

await client.workflow.start(checkout, {
  taskQueue: 'checkout',
  workflowId: `checkout-${randomUUID()}`,
  retry: { maximumAttempts: 3 },
  args: [cart],
});
```

Then `sendReceipt` hit a template bug and threw a non-retryable error. That snag stopped the Activity’s retries, not the Workflow’s. The Workflow failed, and the Retry Policy did what it was told: a new Run under the same Workflow ID, with the same input and an empty history.

A Workflow retry doesn’t replay anything. It runs the function from the top with a new random seed, so all three Activities run again, `uuid4()` hands the provider a key it has never seen, and the provider charges the card. The template bug is still there, so the receipt fails again and attempt three charges a third time. Stock was reserved three times too. Every Activity ran once per Run, and there were three Runs.

![Diagram of one checkout Workflow with a Retry Policy of three attempts. Each of Run 1, Run 2, and Run 3 reserves stock, charges $42, and fails on sendReceipt with a template error. Each Run sends the payment provider a different idempotency key (a3f9, 7c21, d8b2), so the provider records three new charges, $126 in total.](https://images.ctfassets.net/0uuz8ydxyd9p/4AKsm3mj7J8y5O7Ee3D4Wg/c890c0896ef2db89369455b5fe7b7971/idempotent-workflows-per-run-keys-excalidraw.png){.zoom}
*Each Run mints new keys, so each Run charges the card.*{.caption}

Think of a hotel key card. Each stay is a Run, and each stay issues a new card, so the card belongs to the stay rather than the room. The room number is the Workflow ID, which doesn’t change between stays. The fix below keys the lock off the room number instead, and the card from a second stay then opens the same door.

A Reset starts a new Run too, keeping history to the reset point, so every Activity after it gets a fresh key. [Continue-as-New](https://docs.temporal.io/workflow-execution/continue-as-new) closes a long-running Workflow’s Run, a monthly subscription say, and carries on in a fresh one. Finished work isn’t redone, so a per-Run key is safe there. The trap runs the other way: a key built only from the Workflow ID would dedupe cycle two against cycle one.

That handler has a second gap. A shopper taps “Place order,” the Workflow starts, and the response dies in a tunnel. The app retries, the handler mints a fresh Workflow ID, and now two unrelated `checkout` Executions run with their own keys. I hadn’t thought about that path until I saw two Executions for one cart in the UI. The phone did nothing wrong; it retried, which is what phones do.

## How to make a Temporal Workflow idempotent

The fix has two parts, and the first one is in the handler. Use the cart ID, the business ID here, as the Workflow ID, so two requests for the same cart start one Workflow instead of two.

```typescript
await client.workflow.start(checkout, {
  taskQueue: 'checkout',
  workflowId: `checkout-${cart.id}`,
  workflowIdConflictPolicy: 'USE_EXISTING', // TypeScript SDK 1.11.5+
  workflowIdReusePolicy: 'REJECT_DUPLICATE',
  args: [cart],
  // no Workflow retry policy
});
```

Temporal guarantees [at most one running Execution per Workflow ID](https://docs.temporal.io/workflow-execution/workflowid-runid) per Namespace, which makes the Workflow ID the checkout’s own idempotency key, enforced by the server, so the phone’s retry can’t start a second one. [`USE_EXISTING`](https://docs.temporal.io/workflow-execution/workflowid-runid#workflow-id-conflict-policy) returns a handle to the running Execution instead of throwing. After it closes, the default reuse policy lets a late retry start another. [`REJECT_DUPLICATE`](https://docs.temporal.io/workflow-execution/workflowid-runid#workflow-id-reuse-policy) throws `WorkflowExecutionAlreadyStartedError` instead, for as long as the closed Execution is inside the retention period. Catch it and call `client.workflow.getHandle(workflowId).result()`, which throws `WorkflowFailedError` if that Execution failed. To let a failed checkout run again, use `ALLOW_DUPLICATE_FAILED_ONLY`. With the second fix below, that re-run sends the same key and, while the provider still holds it, gets the original result, even a decline.

That only closes the duplicate-start gap. The Retry Policy is gone too, since the [Temporal docs say it belongs only in specific situations](https://docs.temporal.io/encyclopedia/retry-policies#workflow-execution). I left it in for a while out of habit, and took it out once I saw that every retry it bought me was another Run. But a Reset is still a new Run, and so is any retry a teammate adds later, and each one would generate new keys.

## Derive Activity idempotency keys from the Workflow ID

Build each key from the Workflow ID plus a step name, so a new Run reproduces the same keys.

```typescript
import { workflowInfo } from '@temporalio/workflow';

export async function checkout(cart: Cart): Promise<void> {
  const { workflowId } = workflowInfo();
  const key = (step: string) => `${workflowId}:${step}`;

  await reserveStock(cart.items, key('reserve'));
  const charge = await chargeCard(cart.total, cart.paymentMethod, key('charge'));
  await sendReceipt(cart.email, charge.id, key('receipt'));
}
```

Now a retried Run reproduces `checkout-<cartId>:charge` and gets the original charge.

![Two-panel diagram of the same $42 checkout across three Workflow Runs. Top panel, per-Run keys from uuid4(): Runs 1, 2, and 3 send keys a3f9, 7c21, and d8b2, and the payment provider creates three charges totaling $126. Bottom panel, a business key built from workflowId plus ':charge': all three Runs send checkout-8813:charge, Runs 2 and 3 receive the original response, and the provider creates one $42 charge.](https://images.ctfassets.net/0uuz8ydxyd9p/10tEGVCVQR5DpYY5mFDodU/cb41b64f7faec7523c6496f11aa22b01/idempotent-workflows-business-key-excalidraw.png){.zoom}
*Keys derived from the Workflow ID let all three Runs send the same charge key, so the provider charges once.*{.caption}

If an Activity runs in a loop, or that subscription continues as new, `workflowId:step` collides, so add the line item ID or cycle number. A deliberate second charge gets a new Workflow ID.

## Activity, Run, and Workflow idempotency: what stops the second one?

“If this whole thing runs twice, what stops the second one?”

| Layer | What runs twice | What stops it |
| --- | --- | --- |
| Activity | one Activity’s attempts | idempotency key passed in |
| Run | Workflow retry, Reset | Workflow ID key, plus an index where work repeats |
| Workflow | duplicate start | business ID as the Workflow ID |

Last check: the outside service must hold keys as long as your Workflows live. Providers set their own retention window, and it can be as short as 24 hours, so a Reset a week later can send a key the provider no longer has and charge again.

## Check your own Workflows

Run the question from the table against your own code: if this whole thing runs twice, what stops the second one? In practice that comes down to four checks.

1. Look at where your starter code sets the Workflow ID. A random value means two requests for the same order become two Workflows.
2. Search your Workflow code for `uuid4()`, random values, or a Run ID used to build an idempotency key. Those keys disappear with the Run.
3. See whether anything sets a Retry Policy on the Workflow itself, including a teammate’s starter script. Every retry is a new Run.
4. Compare how long your downstream service keeps idempotency keys with how long your Workflows run and how far back you would ever Reset.

I’d start with the second check, because it is a one-line search and it is the one that would have caught this checkout. If one of these turns up a gap, the fix is usually in the starter and in how keys are built. The Activities themselves rarely need to change, which is also why this gap is easy to miss in review. The [Workflow ID and Run ID docs](https://docs.temporal.io/workflow-execution/workflowid-runid) cover the conflict and reuse policies for the starter side.
