Skip to content

Commit 30fca37

Browse files
LuisDuarte1pombosilvaOxyjun
authored
feat: clarfy concurrency limits and waiting state in workflows (#26273)
* feat: clarfy concurrency limits and `waiting` state in workflows * Apply suggestions from code review Co-authored-by: Olga Silva <[email protected]> * make example smaller * Apply suggestions from code review Co-authored-by: Olga Silva <[email protected]> * fix typo * PCX Review --------- Co-authored-by: Olga Silva <[email protected]> Co-authored-by: Jun Lee <[email protected]>
1 parent 1ce8050 commit 30fca37

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

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

Lines changed: 48 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,55 @@ 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. Instances in the `waiting` state are excluded from 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. This 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, on a best-effort basis. This state transition - `running` to `waiting` - may not occur if the wait duration is too short.
52+
53+
For example, consider a Workflow that does some work, waits for 30 days, and then continues with more work:
54+
55+
```ts title="src/index.ts"
56+
import { WorkflowEntrypoint, WorkflowStep, WorkflowEvent } from 'cloudflare:workers';
57+
58+
type Env = {
59+
MY_WORKFLOW: Workflow;
60+
};
61+
62+
export class MyWorkflow extends WorkflowEntrypoint<Env> {
63+
async run(event: WorkflowEvent<unknown>, step: WorkflowStep) {
64+
65+
const apiResponse = await step.do('initial work', async () => {
66+
let resp = await fetch('https://api.cloudflare.com/client/v4/ips');
67+
return await resp.json<any>();
68+
});
69+
70+
await step.sleep('wait 30 days', '30 days');
71+
72+
await step.do(
73+
'make a call to write that could maybe, just might, fail',
74+
{
75+
retries: {
76+
limit: 5,
77+
delay: '5 second',
78+
backoff: 'exponential',
79+
},
80+
timeout: '15 minutes',
81+
},
82+
async () => {
83+
if (Math.random() > 0.5) {
84+
throw new Error('API call to $STORAGE_SYSTEM failed');
85+
}
86+
},
87+
);
88+
}
89+
}
90+
```
91+
92+
While a given Workflow instance is waiting for 30 days, it will transition to the `waiting` state, allowing other `queued` instances to run if concurrency limits are reached.
93+
4794
### Increasing Workflow CPU limits
4895

4996
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)