At-Least-Once and Idempotency: Where the Guarantee Stops

Idempotent means doing something twice has the same result as doing it once. That's what you need on the other side of every activity.

The Gap That Can't Be Closed

  1. The activity worker calls Stripe. The charge goes through.
  2. Before the worker can call RespondActivityTaskCompleted, the process is killed. It could be out of memory, a node rotation, a network split, anything.
  3. All the server sees is an activity that started and never reported back. Start-To-Close runs out, the retry policy kicks in, and a new attempt charges the card again.

Nothing inside Temporal can fix this. The two systems don't share a transaction. The only way to know Stripe was called is for Stripe to say so, and that reply can get lost. (This is known as the "two generals problem.") Any system that promises "exactly-once side effects" is either wrong, or quietly expects the other side to remove duplicates.

WhatHow many times it happensWhy
A decision in workflow codeExactly once, in practiceIt's saved in history. Replay repeats it instead of deciding again.
An activity's result, as the workflow sees itExactly onceOnce it's in history, it is never worked out again
The code inside an activity: the real side effectAt least onceThe "done" message can be lost after the work already happened
A signal to a running workflowAt least once at the API, exactly once in historyA client retry can send the request twice, so give it a request ID

Two Idempotency Keys You Already Have

Workflow ID: stop duplicates at the start

The server makes sure workflow IDs are unique. Start the order workflow with the ID order-A-91, and a second start request with the same ID is either rejected or joined to the run that already exists. The ID reuse policy decides which.

Build the ID from a business key, like an order number, not a random UUID. Then double-submits from your API stop being something you have to handle.

Activity identity: stop duplicates at the call

Inside an activity, activity.GetInfo(ctx) gives you the workflow ID, run ID, activity ID, and attempt number. Workflow ID + activity ID stays the same on every retry of that activity. That's exactly what an idempotency key needs.

Pass it to Stripe as the Idempotency-Key, or use it as the primary key of the row you insert.

// Inside the activity — the key is identical on every retry.
info := activity.GetInfo(ctx)
key := fmt.Sprintf("%s/%s", info.WorkflowExecution.ID, info.ActivityID)
_, err := stripe.Charge(ctx, amount, stripe.WithIdempotencyKey(key))
  • Guarantees: Every activity either finishes with its result saved, or is retried until it does (or until its retry policy gives up). Nothing is skipped without anyone knowing.
  • Does not guarantee: That an outside side effect happens only once. Temporal guarantees the work moves forward, not that it happens once. To get "once," the other side has to use an idempotency key.