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

AUTHORS
PUBLISHED
Dec 02, 2026
CATEGORY
DURATION
9 MIN
  • Retries
  • Durable Execution
  • Architecture
  • Typescript
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.

// 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:

// 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 suggests the Run ID plus the Activity ID as a fallback key, and its Idempotent Workflows section 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. You’ve probably set one yourself. I have. It feels like free reliability.

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.

Each Run mints new keys, so each Run charges the card.

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 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.

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 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 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 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. 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.

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.

Keys derived from the Workflow ID let all three Runs send the same charge key, so the provider charges once.

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 cover the conflict and reuse policies for the starter side.

Temporal Cloud

Ready to see for yourself?

Sign up for Temporal Cloud today and get $150 in free credits.

Build invincible applications

It sounds like magic, we promise it's not.