Skip to content

feat: idempotency key on workflow start - #1855

Closed
mp-orkes wants to merge 1 commit into
mainfrom
feat/start-idempotency-key
Closed

mp-orkes wants to merge 1 commit into
mainfrom
feat/start-idempotency-key

Conversation

@mp-orkes

Copy link
Copy Markdown
Contributor

Split out of #1832.

Why

StartWorkflowRequest has an idempotencyKey and an idempotencyStrategy, but WorkflowExecutorOps.startWorkflow ignores them. A caller that retries a start after a network error gets a second workflow. #1832 makes the start use the key. This change is independent of the other changes in that PR. This PR moves it out, so that it can be reviewed on its own.

What changed

  • A start with an idempotency key gets a workflow id that Conductor derives from the workflow name and the key (UUID v3 of name:key).
  • A second start with the same name and key returns the id of the existing workflow. It does not create a workflow.
  • With the strategy FAIL, the second start gets a 409.
  • A start with no key does not change.

How to test

  1. POST /api/workflow with "name": "<workflow>" and "idempotencyKey": "k1". The response is a workflow id.
  2. Send the same request again. The response is the same workflow id, and there is one execution.
  3. Send it again with "idempotencyStrategy": "FAIL". The response is a 409.

Notes

  • The workflow version and the input are not part of the id.
  • The key is not stored on the workflow.
  • A start with a key puts the first evaluation on the decider queue. A start with no key evaluates in the request.
  • Code native workflows #1832 has no test for this path. The tests in this PR are new.

🤖 Generated with Claude Code

A start request with an idempotency key now gets a workflow id derived
from the workflow name and the key (UUID v3 of "name:key"), unless the
caller gives a workflow id. The start goes through
startWorkflowIdempotent: a second start with the same name and key
returns the id of the first workflow and creates nothing. With strategy
FAIL, a second start is a 409 conflict.

Before this change the engine ignored the key on startWorkflow.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@mp-orkes mp-orkes left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not ready to merge. The server ignored the idempotency key until now, and existing callers already send it. Two of the three strategies and the concurrent path do not do what the description states.

# Severity Location Issue
1 Blocking WorkflowExecutorOps.java:2780 FAIL_ON_RUNNING has no handling
2 Blocking SchedulerService.java:1032 A schedule with a key runs one time only
3 Blocking WorkflowExecutorOps.java:2780 The FAIL check runs outside the lock
4 Major WorkflowExecutorOps.java:2839 A duplicate create deletes or replaces the first workflow
5 Major WorkflowExecutorOps.java:2792 A start with a key loses the synchronous first decide
6 Major TestWorkflowExecutor.java:1071 The tests use mocks for the store and the lock
7 Minor WorkflowExecutorOps.java:2804 A lock timeout gives a 500
8 Minor WorkflowExecutorOps.java:2776 name:key can collide; the key is not stored

1. FAIL_ON_RUNNING. Only FAIL has a branch. FAIL_ON_RUNNING and a null strategy return the existing workflow in any status. A key stays bound to one execution, also after that execution is COMPLETED. Fix: give a 409 while the workflow runs, and define what a start does when the existing workflow is terminal. A derived id cannot hold a second execution.

2. Scheduler. swrFrom copies the key and the strategy of the schedule to each trigger. Before, the key had no effect. Now the first trigger starts a workflow, and each later trigger returns that same id and is recorded as EXECUTED. The schedule stops with no error. Fix: decide the scheduler contract (for example, add the scheduled time to the key) and add a scheduler test.

3. FAIL and the lock. The existence check is before the lock that startWorkflowIdempotent takes. Two concurrent FAIL starts can both get "not found". The second then finds the workflow in the locked section and returns it. No caller gets the 409. Fix: move the strategy decision into the locked section.

4. Duplicate create. The default lock is NoopLock, and the server default local_only is for one JVM. Two starts can both call createWorkflow. Postgres and MySQL: the second insert fails and the catch block removes the workflow of the first caller. Redis and Cassandra: the second create replaces the record. Fix: do not remove the workflow when the id exists; read it and return it. #1847 removes the delete, so this PR depends on it.

5. First decide. The path with no key calls decide in the request. The path with a key only pushes to the decider queue. A2AWorkflowAgent.sendMessage reads the workflow immediately after the start and gets a workflow with no tasks. Fix: run the first decide in the request for a new workflow.

6. Tests. startWithSameIdempotencyKeyReturnsExistingWorkflow makes one call against a stub. No test covers FAIL_ON_RUNNING, a terminal workflow, two concurrent starts, or a duplicate create. Fix: add one test for each strategy in the running and terminal states, and one test with a real DAO that starts two times with the same key.

7. Lock timeout. The lock wait is 500 ms by default. A retry that arrives during a slow first create gets a TransientException (500). Fix: on a lock timeout, read the workflow and return it.

8. Id and key. a:b + c gives the same id as a + b:c. WorkflowModel has no key field, so Workflow.idempotencyKey stays null. Fix: use an encoding with no ambiguity and store the key.

Not checked: the SQLite constraint on the workflow id, the lock implementations, and a run against a real server.

@mp-orkes mp-orkes self-assigned this Nov 18, 2026
@mp-orkes
mp-orkes requested a review from c4lm November 19, 2026 12:50
@mp-orkes
mp-orkes marked this pull request as ready for review November 19, 2026 12:50
@mp-orkes

Copy link
Copy Markdown
Contributor Author

Closing this in favor of #1857.

My strategy was to make these changes incrementally and discuss things like the scheduler: a schedule that sets an idempotency key now runs one time only, because every later trigger returns the first workflow.

@mp-orkes mp-orkes closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant