Skip to content

Commit 681cb1a

Browse files
committed
feat: clarfy concurrency limits and waiting state in workflows
1 parent 1d756e0 commit 681cb1a

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

src/content/docs/workflows/reference/limits.mdx

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Many limits are inherited from those applied to Workers scripts and as documente
2424
| Maximum `step.sleep` duration | 365 days (1 year) | 365 days (1 year) |
2525
| Maximum steps per Workflow [^5] | 1024 | 1024 |
2626
| Maximum Workflow executions | 100,000 per day [shared with Workers daily limit](/workers/platform/limits/#worker-limits) | Unlimited |
27-
| Concurrent Workflow instances (executions) per account | 25 | 10,000 |
27+
| Concurrent Workflow instances (executions) per account [^7] | 25 | 10,000 |
2828
| Maximum Workflow instance creation rate | 100 per second [^6] | 100 per second [^6] |
2929
| Maximum number of [queued instances](/workflows/observability/metrics-analytics/#event-types) | 10,000 | 100,000 |
3030
| Retention limit for completed Workflow state | 3 days | 30 days [^2] |
@@ -42,8 +42,65 @@ Many limits are inherited from those applied to Workers scripts and as documente
4242

4343
[^6]: Workflows will return a HTTP 429 rate limited error if you exceed the rate of new Workflow instance creation.
4444

45+
[^7]: Only instances with a `running` state count towards the concurrency limits. If an instance is in a `waiting` it does not count these limits.
46+
4547
<Render file="limits_increase" product="workers" />
4648

49+
### `waiting` instances do not count towards instance concurrency limits
50+
51+
Instances that are on a `waiting` state - either sleeping, waiting for a retry or waiting for an event - do **not** count towards concurrency limits. It means that other `queued` instances will be scheduled when an instance goes from a `running` state to a `waiting` one, usually the oldest instance queued, in a best-effort basis. This state transition - `running` to `waiting` - might not happen if the duration of the wait is too short.
52+
53+
As an example, let's say we have a workflow that does some work, waits 30 days, and continues other work:
54+
55+
```ts title="src/index.ts"
56+
import { WorkflowEntrypoint, WorkflowStep, WorkflowEvent } from 'cloudflare:workers';
57+
58+
type Env = {
59+
// Add your bindings here, e.g. Workers KV, D1, Workers AI, etc.
60+
MY_WORKFLOW: Workflow;
61+
};
62+
63+
// User-defined params passed to your workflow
64+
type Params = {
65+
email: string;
66+
metadata: Record<string, string>;
67+
};
68+
69+
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
70+
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
71+
// Can access bindings on `this.env`
72+
// Can access params on `event.payload`
73+
const apiResponse = await step.do('initial work', async () => {
74+
let resp = await fetch('https://api.cloudflare.com/client/v4/ips');
75+
return await resp.json<any>();
76+
});
77+
78+
await step.sleep('wait 30 days', '30 days');
79+
80+
await step.do(
81+
'make a call to write that could maybe, just might, fail',
82+
// Define a retry strategy
83+
{
84+
retries: {
85+
limit: 5,
86+
delay: '5 second',
87+
backoff: 'exponential',
88+
},
89+
timeout: '15 minutes',
90+
},
91+
async () => {
92+
// Do stuff here, with access to the state from our previous steps
93+
if (Math.random() > 0.5) {
94+
throw new Error('API call to $STORAGE_SYSTEM failed');
95+
}
96+
},
97+
);
98+
}
99+
}
100+
```
101+
102+
While a given Workflow instance is waiting for 30 days to pass, it will go to a `waiting` state, allowing other `queued` instances to run - if concurrency limits are exhausted.
103+
47104
### Increasing Workflow CPU limits
48105

49106
Workflows are Worker scripts, and share the same [per invocation CPU limits](/workers/platform/limits/#worker-limits) as any Workers do. Note that CPU time is active processing time: not time spent waiting on network requests, storage calls, or other general I/O, which don't count towards your CPU time or Workflows compute consumption.

0 commit comments

Comments
 (0)