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: docs/queue-concurrency.mdx
+17-98Lines changed: 17 additions & 98 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,25 +3,24 @@ title: "Concurrency & Queues"
3
3
description: "Configure what you want to happen when there is more than one run at a time."
4
4
---
5
5
6
-
When you trigger a task, it isn't executed immediately. Instead, the task [run](/runs) is placed into a queue for execution. By default, each task gets its own queue with unbounded concurrency—meaning the task runs as soon as resources are available, subject only to the overall concurrency limits of your environment. If you need more control (for example, to limit concurrency or share limits across multiple tasks), you can define a custom queue as described later in this document.
6
+
When you trigger a task, it isn't executed immediately. Instead, the task [run](/runs) is placed into a queue for execution.
7
+
8
+
By default, each task gets its own queue and the concurrency is only limited by your environment concurrency limit. If you need more control (for example, to limit concurrency or share limits across multiple tasks), you can define a custom queue as described later.
7
9
8
10
Controlling concurrency is useful when you have a task that can't be run concurrently, or when you want to limit the number of runs to avoid overloading a resource.
9
11
10
12
It's important to note that only actively executing runs count towards concurrency limits. Runs that are delayed or waiting in a queue do not consume concurrency slots until they begin execution.
11
13
12
14
## Default concurrency
13
15
14
-
By default, all tasks have an unbounded concurrency limit, limited only by the overall concurrency limits of your environment. This means that each task could possibly "fill up" the entire
15
-
concurrency limit of your environment.
16
-
17
-
Each individual queue has a maximum concurrency limit equal to your environment's base concurrency limit. If you don't explicitly set a queue's concurrency limit, it will default to your environment's base concurrency limit.
16
+
By default, all tasks have an unbounded concurrency limit, limited only by the overall concurrency limits of your environment.
18
17
19
18
<Note>
20
19
Your environment has a base concurrency limit and a burstable limit (default burst factor of 2.0x
21
20
the base limit). Individual queues are limited by the base concurrency limit, not the burstable
22
21
limit. For example, if your base limit is 10, your environment can burst up to 20 concurrent runs,
23
22
but any single queue can have at most 10 concurrent runs. If you're a paying customer you can
24
-
request higher limits by [contacting us](https://www.trigger.dev/contact).
23
+
request higher burst limits by [contacting us](https://www.trigger.dev/contact).
25
24
</Note>
26
25
27
26
## Setting task concurrency
@@ -72,11 +71,11 @@ export const task2 = task({
72
71
73
72
In this example, `task1` and `task2` share the same queue, so only one of them can run at a time.
74
73
75
-
## Setting the concurrency when you trigger a run
74
+
## Setting the queue when you trigger a run
76
75
77
-
When you trigger a task you can override the concurrency limit. This is really useful if you sometimes have high priority runs.
76
+
When you trigger a task you can override the default queue. This is really useful if you sometimes have high priority runs.
@@ -124,7 +123,7 @@ export async function POST(request: Request) {
124
123
125
124
If you're building an application where you want to run tasks for your users, you might want a separate queue for each of your users (or orgs, projects, etc.).
126
125
127
-
You can do this by using `concurrencyKey`. It creates a separate queue for each value of the key.
126
+
You can do this by using `concurrencyKey`. It creates a copy of the queue for each unique value of the key.
128
127
129
128
Your backend code:
130
129
@@ -135,18 +134,20 @@ export async function POST(request: Request) {
135
134
const data =awaitrequest.json();
136
135
137
136
if (data.isFreeUser) {
138
-
//free users can only have 1 PR generated at a time
137
+
//the "free-users" queue has a concurrency limit of 1
@@ -158,7 +159,7 @@ export async function POST(request: Request) {
158
159
159
160
## Concurrency and subtasks
160
161
161
-
When you trigger a task that has subtasks, the subtasks will not inherit the concurrency settings of the parent task. Unless otherwise specified, subtasks will run on their own queue
162
+
When you trigger a task that has subtasks, the subtasks will not inherit the queue from the parent task. Unless otherwise specified, subtasks will run on their own queue
162
163
163
164
```ts /trigger/subtasks.ts
164
165
exportconst parentTask =task({
@@ -198,11 +199,6 @@ For example, if you have a queue with a `concurrencyLimit` of 1:
198
199
- When the executing run reaches a waitpoint and checkpoints, it releases its slot
199
200
- The next queued run can then begin execution
200
201
201
-
<Note>
202
-
We sometimes refer to the parent task as the "parent" and the subtask as the "child". Subtask and
203
-
child task are used interchangeably. We apologize for the confusion.
204
-
</Note>
205
-
206
202
### Waiting for a subtask on a different queue
207
203
208
204
When a parent task triggers and waits for a subtask on a different queue, the parent task will checkpoint and release its concurrency slot once it reaches the wait point. This prevents environment deadlocks where all concurrency slots would be occupied by waiting tasks.
When the parent task reaches the `triggerAndWait` call, it checkpoints and transitions to the `WAITING` state, releasing its concurrency slot back to both its queue and the environment. Once the subtask completes, the parent task will resume and re-acquire a concurrency slot.
233
-
234
-
### Waiting for a subtask on the same queue
235
-
236
-
When a parent task and subtask share the same queue, the checkpointing behavior ensures that recursive task execution can proceed without deadlocks, up to the queue's concurrency limit.
237
-
238
-
```ts /trigger/waiting-same-queue.ts
239
-
exportconst myQueue =queue({
240
-
name: "my-queue",
241
-
concurrencyLimit: 1,
242
-
});
243
-
244
-
exportconst parentTask =task({
245
-
id: "parent-task",
246
-
queue: myQueue,
247
-
run: async (payload) => {
248
-
//trigger a subtask and wait for it to complete
249
-
awaitsubtask.triggerAndWait(payload);
250
-
},
251
-
});
252
-
253
-
exportconst subtask =task({
254
-
id: "subtask",
255
-
queue: myQueue,
256
-
run: async (payload) => {
257
-
//...
258
-
},
259
-
});
260
-
```
261
-
262
-
When the parent task checkpoints at the `triggerAndWait` call, it releases its concurrency slot back to the queue, allowing the subtask to execute. Once the subtask completes, the parent task will resume.
263
-
264
-
However, you can only have recursive waits up to your queue's concurrency limit. If you exceed this limit, you will receive a `RECURSIVE_WAIT_DEADLOCK` error:
265
-
266
-
```ts /trigger/deadlock.ts
267
-
exportconst myQueue =queue({
268
-
name: "my-queue",
269
-
concurrencyLimit: 1,
270
-
});
271
-
272
-
exportconst parentTask =task({
273
-
id: "parent-task",
274
-
queue: myQueue,
275
-
run: async (payload) => {
276
-
awaitsubtask.triggerAndWait(payload);
277
-
},
278
-
});
279
-
280
-
exportconst subtask =task({
281
-
id: "subtask",
282
-
queue: myQueue,
283
-
run: async (payload) => {
284
-
awaitsubsubtask.triggerAndWait(payload); // This will cause a deadlock
285
-
},
286
-
});
287
-
288
-
exportconst subsubtask =task({
289
-
id: "subsubtask",
290
-
queue: myQueue,
291
-
run: async (payload) => {
292
-
//...
293
-
},
294
-
});
295
-
```
296
-
297
-
This results in a `RECURSIVE_WAIT_DEADLOCK` error because the queue can only support one level of recursive waiting with a concurrency limit of 1:
0 commit comments