Idempotency
Idempotency is a property of an operation that ensures repeated attempts have the same effect as a single attempt.
In Workflow, idempotency shows up in two related places: step idempotency makes external calls safe when a step retries, and run idempotency makes duplicate requests start the same workflow run only once.
Step idempotency
In distributed systems (calling external APIs), it is not always possible to ensure an operation has only been performed once by seeing if it succeeds. Consider a payment API that charges the user $10, but due to network failures, the confirmation response is lost. When the step retries (because the previous attempt was considered a failure), it will charge the user again.
To prevent this, many external APIs support idempotency keys. An idempotency key is a unique identifier for an operation that can be used to deduplicate requests.
Every step invocation has a stable stepId that stays the same across retries.
Use it as the idempotency key when calling third-party APIs.
Why this works:
- Stable across retries:
stepIddoes not change between attempts. - Globally unique per step: Fulfills the uniqueness requirement for an idempotency key.
Run idempotency
Step idempotency protects side effects inside a workflow run. Run idempotency answers a different question: if the same API request is sent twice, should it create one workflow run or two?
Workflow uses a Hook token as the idempotency key. Pick a token from your domain, such as an order ID, invoice ID, import ID, or request ID, and pass it to start().
Start a run once per key
start({ experimental_hook }) admits the run and creates its start Hook in one atomic step. Exactly one run owns the token. A duplicate start() with the same token throws HookConflictError with the owner's conflictingRunId. The duplicate never becomes a run, and none of its code runs.
Start or route to the owner
A duplicate request often carries data the owner should receive, such as a follow-up message in a chat. Since the start Hook exists from the moment the run is admitted, you can resume it right away on a conflict, even before the owner starts executing:
Every caller either starts the run, delivers its payload to the owner, or learns which run holds the token. While the owner's start Hook is open, the loop needs at most two calls. A HookNotFoundError after a conflict means the owner's Hook is no longer resumable: the owner ended, or disposed its start Hook. The next start() may then take the token, or be refused again: by a finished owner that still holds it for its retention, or by another caller that started first.
If the token has a retention, a finished owner keeps refusing new starts until the retention passes, and its Hook can no longer be resumed, so the loop above gives up after its attempts and names that run. Use getRun(owner) to tell the caller what the finished run returned.
Receive the payloads in the workflow
To read the payloads, call createHook() in the workflow with the same token:
createHook({ token })with the run's start token returns a handle bound to the start Hook. It doesn't register a new Hook, so it adds no step, andawait hook.getConflict()resolvesnull.- Payloads resumed before the workflow opens the Hook are kept, and are delivered in order once it does.
- If the workflow never opens it, the token was only an idempotency key. The run completes normally, and unread payloads are simply left unread.
metadataandexperimental_minRetentionbelong onstart({ experimental_hook }); a start Hook can't be a webhook or force-claim its token. Passing any ofmetadata,experimental_minRetention,isWebhookorexperimental_forceto thiscreateHook()throws.
Keep the token after the run ends
The start Hook follows the ordinary Hook lifecycle, like a createHook() in the run. Without a retention, the token is released when the run ends, and a later start() with the token is admitted as a new run.
To refuse late duplicates too, set experimental_minRetention. It accepts the same duration string, milliseconds, or Date as createHook({ experimental_minRetention }), and a duration counts from start():
The run owns the token until it ends, even if the retention passes first. After the run ends, and until the retention passes:
- a duplicate
start()throwsHookConflictErrornaming the finished owner; getHookByToken()still returns the Hook, with thestart()metadata;resumeHook()throwsHookNotFoundError, because a finished run can't receive payloads.
There is no default retention.
Cancelling the owner ends the run, so it releases the token too, once any retention has passed.
Take care with the start Hook's lifecycle
The run holds its token only while its start Hook does. Disposing the start Hook, with hook.dispose() or when a using declaration in an inner scope ends, releases the token at once, as it does for any Hook. A later start() with the token is then admitted as a new run, even while this one is still running, and experimental_minRetention no longer applies: it only holds the token after a run that ends without disposing the Hook.
Open the start Hook with const, as in the examples above, and let the run's end release it. After disposing it, the run can call createHook({ token }) again for a new Hook, but only if no other run has taken the token in the meantime; otherwise getConflict() names that run.
Dispose the start Hook with using
using disposes a Hook when its scope ends, which reads well when the workflow only needs the payloads for part of its work:
Disposing the start Hook ends the run's idempotency: from the end of that block, another start() with the token is admitted while this run ships the order. Use it when that is what you want. To keep duplicates out for the whole run, use const. A using at the top level of the workflow function disposes the Hook only as the run returns, but with a retention it releases the token then instead of holding it.
When start() can't confirm the outcome
If start({ experimental_hook }) can't confirm whether its candidate run was queued or admitted, for example because the World timed out, it throws WorkflowStartError with the candidate's runId. The outcome is unknown, not failed. Retrying with the same token is safe: the retry either becomes the owner, or is refused with a HookConflictError naming the owner. Inside a workflow, the start() step retries it on its own.
World support
start({ experimental_hook }) requires a World that supports atomic start Hooks (capabilities.atomicStartHook), and Hook retention when you set experimental_minRetention. Otherwise start() throws before it enqueues anything.
| World | Support |
|---|---|
| Local World | Supported. |
| Postgres World | Supported, after applying migration 0028_add_start_hook_admissions. Stop workers running a previous version first; see Start Hooks. |
| Vercel World | Supported. |
See start() for the full option reference.
Coordinate inside the workflow
On a World without atomic start Hooks, or when a run should decide for itself how to treat a duplicate, coordinate with createHook() and getConflict() in the workflow instead. Hook tokens are unique while active: if another run already owns the token, await hook.getConflict() resolves with a Run handle for the owner, and awaiting the hook rejects with HookConflictError.
Create the hook near the beginning of the workflow, and check await hook.getConflict() before calling any step. Awaiting it suspends the workflow to commit the registration; steps called before that suspension start alongside it, so a duplicate run that learns of the conflict later may already have started them. See Registering a hook before a step uses it.
Outside the workflow, try resumeHook() first, and on HookNotFoundError call start() and retry the resume until the new run registers its hook.
Unlike start({ experimental_hook }), this doesn't stop a duplicate from becoming a run. Two concurrent requests can both see "no hook yet" and each call start(). The losing run is still created, queued, and started, and returns once getConflict() resolves with the owner. The route can detect it by comparing the resumed hook's runId with the run it started.
The token becomes available when its workflow ends, unless you set experimental_minRetention on createHook(). See createHook() minimum retention.
Conflict-handling strategies
Some workflow systems resolve duplicate IDs with a fixed, pre-declared policy, typically a static choice between rejecting the new execution, deferring to the existing one, or terminating it. Workflow has no policy enum. hook.getConflict() hands the duplicate run the conflicting Run itself, and the policy is ordinary code, including policies that inspect state before deciding, which static configuration can't express.
The example above implements reject the duplicate: return the owner's runId and let the caller decide. Other common strategies:
Adopt the owner's result. Wait for the active run to finish and return its result, so callers cannot tell which run did the work:
Inspect the owner before deciding. Reuse a completed owner's result, but reject other duplicates:
Signal the owner instead of doing the work. A conflict can refer to a finished run when experimental_minRetention is set, so check its status before sending data to its Hook:
Supersede the owner. Create the Hook with experimental_force: true so the newest run takes the token over from the active owner. This also works for a finished run holding the token under experimental_minRetention:
The previous owner's await request rejects with HookForceClaimedError, and every resumeHook() for the token reaches the new run from then on. Any run of this workflow can be superseded by a later one, so catch the error and exit cleanly:
A run started at a Workflow spec version below 8, including runs started by older SDK releases, can't be taken from. In that case the forced Hook rejects with HookConflictError, as it would without experimental_force.
getHookByToken() before start() is only an advisory pre-check in this pattern: the lookup and start() are not atomic, so the workflow should still check hook.getConflict().
Related docs
- Learn about retries in Errors & Retrying
- API reference:
getStepMetadata - API reference:
createHook() - API reference:
getHookByToken() - API reference:
resumeHook() - API reference:
start() - Learn about deterministic hook tokens in Hooks