Skip to content

Commit 95f3b05

Browse files
committed
updated rules of workflows with batch creation instructions
1 parent 77d5ebc commit 95f3b05

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/content/docs/workflows/build/rules-of-workflows.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,32 @@ export class MyWorkflow extends WorkflowEntrypoint {
449449
}
450450
```
451451
</TypeScriptExample>
452+
453+
### Batch multiple Workflow invocations
454+
455+
When creating multiple Workflow instances, use the [`createBatch`](/workflows/build/workers-api/#createBatch) method to batch the invocations together. This allows you to create multiple Workflow instances in a single request, which will reduce the number of requests made to the Workflows API and increase the number of instances you can create per minute.
456+
457+
<TypeScriptExample filename="index.ts">
458+
459+
```ts
460+
export default {
461+
async fetch(req: Request, env: Env): Promise<Response> {
462+
let instances = [{"id": "user1", "params": {"name": "John"}}, {"id": "user2", "params": {"name": "Jane"}}, {"id": "user3", "params": {"name": "Alice"}}, {"id": "user4", "params": {"name": "Bob"}}];
463+
464+
// 🔴 Bad: Create them one by one, which is more likely to hit creation rate limits.
465+
for (let instance of instances) {
466+
await env.MY_WORKFLOW.create({
467+
id: instance.id,
468+
params: instance.params
469+
});
470+
}
471+
472+
// ✅ Good: Batch calls together
473+
// This improves throughput.
474+
let instances = await env.MY_WORKFLOW.createBatch(instances);
475+
return Response.json({ instances })
476+
},
477+
};
478+
```
479+
480+
</TypeScriptExample>

0 commit comments

Comments
 (0)