You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/workflows/build/rules-of-workflows.mdx
+54-1Lines changed: 54 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@ can be applied multiple times without changing the result beyond the initial app
17
17
As an example, let us assume you have a Workflow that charges your customers, and you really do not want to charge them twice by accident. Before charging them, you should
@@ -84,6 +87,7 @@ export class MyWorkflow extends WorkflowEntrypoint {
84
87
}
85
88
}
86
89
```
90
+
</TypeScriptExample>
87
91
88
92
Otherwise, your entire Workflow might not be as durable as you might think, and you may encounter some undefined behaviour. You can avoid them by following the rules below:
89
93
@@ -92,6 +96,7 @@ Otherwise, your entire Workflow might not be as durable as you might think, and
92
96
- 🔴 Do not make too many service calls in the same step (unless you need it to prove idempotency).
93
97
- 🔴 Do not do too much CPU-intensive work inside a single step - sometimes the engine may have to restart, and it will start over from the beginning of that step.
@@ -105,13 +110,15 @@ export class MyWorkflow extends WorkflowEntrypoint {
105
110
}
106
111
}
107
112
```
113
+
</TypeScriptExample>
108
114
109
115
### Do not rely on state outside of a step
110
116
111
117
Workflows may hibernate and lose all in-memory state. This will happen when engine detects that there is no pending work and can hibernate until it needs to wake-up (because of a sleep, retry, or event).
112
118
113
119
This means that you should not store state outside of a step:
114
120
121
+
<TypeScriptExamplefilename="index.ts">
115
122
```ts
116
123
function getRandomInt(min, max) {
117
124
const minCeiled =Math.ceil(min);
@@ -152,9 +159,11 @@ export class MyWorkflow extends WorkflowEntrypoint {
152
159
}
153
160
}
154
161
```
162
+
</TypeScriptExample>
155
163
156
164
Instead, you should build top-level state exclusively comprised of `step.do` returns:
157
165
166
+
<TypeScriptExamplefilename="index.ts">
158
167
```ts
159
168
function getRandomInt(min, max) {
160
169
const minCeiled =Math.ceil(min);
@@ -192,11 +201,13 @@ export class MyWorkflow extends WorkflowEntrypoint {
192
201
}
193
202
}
194
203
```
204
+
</TypeScriptExample>
195
205
196
206
### Do not mutate your incoming events
197
207
198
208
The `event` passed to your Workflow's `run` method is immutable: changes you make to the event are not persisted across steps and/or Workflow restarts.
199
209
210
+
<TypeScriptExamplefilename="index.ts">
200
211
```ts
201
212
interfaceMyEvent {
202
213
user:string;
@@ -224,12 +235,13 @@ export class MyWorkflow extends WorkflowEntrypoint {
224
235
// Will always be the same if this step is retried
225
236
})
226
237
```
238
+
</TypeScriptExample>
227
239
228
240
### Name steps deterministically
229
241
230
242
Steps should be named deterministically (even if dynamic). This ensures that their state is cached, and prevents the step from being rerun unnecessarily. Step names act as the "cache key" in your Workflow.
@@ -261,3 +273,44 @@ export class MyWorkflow extends WorkflowEntrypoint {
261
273
})
262
274
}
263
275
```
276
+
</TypeScriptExample>
277
+
278
+
### Instance IDs are unique
279
+
280
+
Workflow [instance IDs](/workflows/build/workers-api/#workflowinstance) are unique per Workflow. The ID is the the unique identifier that associates logs, metrics, state and status of a run to a specific an instance, even after completion. Allowing ID re-use would make it hard to understand if a Workflow instance ID referred to an instance that run yesterday, last week or today.
281
+
282
+
It would also present a problem if you wanted to run multiple different Workflow instances with different [input parameters](/workflows/build/events-and-parameters/) for the same user ID, as you would immediately need to determine a new ID mapping.
283
+
284
+
If you need to associate multiple instances with a specific user, merchant or other "customer" ID in your system, consider using a composite ID or using randomly generated IDs and storing the mapping in a database like [D1](/d1/).
285
+
286
+
<TypeScriptExample filename="index.ts">
287
+
```ts
288
+
// This is in the same file as your Workflow definition
0 commit comments