Idempotency
You can set an idempotency key for a workflow to guarantee it executes only once, even if called multiple times with that key. This is especially useful if your operations have side effects like making a payment or sending an email.
An idempotency key can be any string, but we recommend using UUIDs. Idempotency keys are required to be globally unique for your application.
Use DBOS.withNextWorkflowID
to set an idempotency key for a workflow.
This will also set the workflow ID of that operation.
For example:
class Example {
@DBOS.workflow()
static async exampleWorkflow(var1: str, var2: str) {
return var1 + var2;
}
}
async function main() {
// This sets the ID of the workflow to the supplied key
await DBOS.withNextWorkflowID("very-unique-id", async () => {
return await Example.exampleWorkflow("one", "two");
});
}
When starting a workflow with DBOS.startWorkflow
, the idempotency key can either be passed directly to DBOS.startWorkflow
, or DBOS.withNextWorkflowID
can be used.