Concepts
Machine sizes
Each task declares the CPU and memory an attempt gets. Name a preset, or set them exactly, and override the size for a single run when you need to.
On this page
Presets#
| Machine | vCPU | Memory |
|---|---|---|
micro | 0.25 | 256 MiB |
small | 0.5 | 512 MiB |
small-2x | 1 | 1 GiB |
medium | 1 | 2 GiB |
medium-2x | 2 | 4 GiB |
large | 4 | 8 GiB |
large-2x | 8 | 16 GiB |
A task without a size gets 0.5 vCPU and 512 MiB, the same as small, and a 30-second timeout.
Size a task#
Name a preset in the task's compute:
{"id": "images.resize", "kind": "task", "runtime": "node", "entrypoint": "tasks/resize.mjs", "handler": "resize", "compute": {"machine": "large", "timeout_ms": 120000}}Or set CPU and memory exactly, where 1000 cpu_millis is one vCPU:
"compute": {"cpu_millis": 1500, "memory_mib": 3072}| Field | Range | Default |
|---|---|---|
machine | A preset above | None |
cpu_millis | 100 to 8,000 | 500 |
memory_mib | 128 to 16,384 | 512 |
timeout_ms | 100 to 86,400,000 (24 hours) | 30,000 |
A manifest entry uses machine or the two exact fields, never both.
Size one run#
Pass machine when you trigger to run this one execution on another preset. The task's timeout and everything else stay the task's own.
await tasks.trigger('images.resize', payload, {machine: 'large-2x'});tasks.trigger("images.resize", payload, machine="large-2x")A workflow sizes a child per call, with ctx.callTask(task, input, {key, machine}) in Node and ctx.call_task(task, input, key, machine=...) in Python. ctx.trigger and ctx.triggerAndWait take machine the same way, and each ctx.batchTriggerAndWait item takes it in its options.
The size is recorded with the run, so its retries keep it. It is part of the trigger's idempotency check: the same key with another machine is a ConflictError.
How the size is enforced#
| Where the attempt runs | What the size means |
|---|---|
| A Docker container (private workers, a local control plane) | Hard CPU and memory limits on the container. |
A private worker that pins cores (agent --pin-cores) | The same limits, on cores of its own. A run of 1 vCPU or more owns whole physical cores, both hardware threads of each, that no other run uses. A micro or small run shares a core only with other runs under 1 vCPU. |
| A Bok Joy cloud worker | A machine of at least that size. Cloud machines come in fixed shapes, so the size rounds up: a run can get more than it asked for, never less. When several runs of a task wait, one machine can run two of them at once. It is then sized for both, and the two share its CPU and memory. |
A size larger than the run's environment allows for its placement is refused when you trigger, with a CapacityError that says what the run needs. On a local control plane, the built-in pools hold up to 2 vCPU and 2 GiB per run, so large and large-2x need bigger pools or a bigger worker.
Out of memory#
An attempt that needs more memory than its machine has fails as out of memory. Its error names the machine's memory, and the console labels the attempt "Out of memory". An attempt ran out of memory when:
- the kernel stopped its own process for using more memory than its machine has,
- Node's heap reached its limit (
JavaScript heap out of memory) and Node stopped, - or the task threw
OutOfMemoryError, or Python'sMemoryError.
Bok Joy only ends a run for these. When it is less sure, such as when a process the task started was stopped, the attempt may still move the run to a larger machine, but never costs it its retries. The console labels such an attempt "Suspected out of memory". See How it is detected.
Another attempt on the same machine would most likely run out of memory again, so Bok Joy never retries it there. Name a larger machine in the task's retry.outOfMemory.machine, and the run moves to it:
{"id": "video.encode", "kind": "task", "runtime": "node", "entrypoint": "tasks/encode.mjs", "handler": "encode", "compute": {"machine": "small"}, "retry": {"outOfMemory": {"machine": "large"}}}import { task } from '@bokjoy/sdk/trigger'; export const encode = task({ id: 'video.encode', machine: 'small-1x', retry: { outOfMemory: { machine: 'large-1x' } }, run: async (payload, { ctx }) => { // ... },});define_task(id="video.encode", runtime="python", kind="task", entrypoint="tasks/encode.py", handler="encode", compute={"machine": "small"}, retry={"out_of_memory": {"machine": "large"}})- The next attempt runs on that machine, after the usual wait before a retry, and counts against
max_attempts. Every later attempt of the run stays there, andctx.machinenames it. - The machine must be at least as large as the task's own, in CPU and in memory. Deploy refuses a smaller one.
- An attempt that runs out of memory on that machine too ends the run.
- Without
retry.outOfMemory.machine, an attempt that runs out of memory fails its run at once, whatever attempts it had left. Its error says why and what to change. An attempt that only probably ran out of memory is retried as usual instead. - When the run's environment cannot hold the larger machine for its placement, the run fails at once, with an error that says so.
Say it yourself#
A task that knows the work needs more memory than it has, such as one that reads a file's size before it decodes it, can throw OutOfMemoryError. The run then moves to the larger machine just as if the kernel had stopped it. In Node, Bok Joy knows the error by its name, so Trigger.dev's own works too. In Python it must be the SDK's OutOfMemoryError, or a subclass: an error of another library with the same name, such as the Redis client's for a full server, is an ordinary failure.
import {OutOfMemoryError} from '@bokjoy/sdk'; export async function encode(payload, ctx) { if (payload.bytes * 4 > ctx.machine.memoryMib * 1024 * 1024) { throw new OutOfMemoryError(`a ${payload.bytes}-byte video needs a larger machine`); } // ...}from bokjoy import OutOfMemoryError async def encode(payload, ctx): if payload["bytes"] * 4 > ctx.machine.memory_mib * 1024 * 1024: raise OutOfMemoryError(f"a {payload['bytes']}-byte video needs a larger machine") # ...ctx.machine is the machine the attempt runs on: its preset's name, cpuMillis and memoryMib (Python: name, cpu_millis and memory_mib). The name is unset for a task sized with exact CPU and memory. In a Trigger.dev-style task, ctx.machine has Trigger.dev's name, such as large-1x, with cpu and memory in vCPU and GB.
Run it again on a larger machine#
A run that failed out of memory can run again on another machine. Replay it with one, which runs a new run of the same task and payload:
durable-compute replay <run-id> --machine largeOr trigger with machine, as in Size one run.
How it is detected#
Bok Joy reads what the kernel, Node or the task said about the attempt itself. It is sure in these cases, which fail the run when there is no larger machine:
| Where the attempt runs | Out of memory when |
|---|---|
| A Docker container (private workers, a local control plane) | The kernel stopped the task's own process for the container's memory limit, which is the machine's. |
| Anywhere | Node's heap message is on the task's standard error and Node stopped as it does when its heap is full, or the task threw OutOfMemoryError or MemoryError. |
In these cases it is less sure. The attempt moves the run to retry.outOfMemory.machine when it can, and is otherwise an ordinary failure, retried as usual:
| Where the attempt runs | Probably out of memory when |
|---|---|
| A Docker container | The kernel stopped a process the task started, and the task then failed with an error that says a process was killed, such as ffmpeg was killed with signal SIGKILL. |
| A Bok Joy cloud worker or a native private worker | The kernel stopped the task when the machine ran out of memory, and the task had used more than the memory it reserved. On a cloud worker, also when it was the only task on its machine. |
A machine runs several attempts at once when it has room. When one of them uses more than it reserved and the machine runs out, the kernel may stop another. That attempt is not out of memory: it is retried as usual, and its error says the machine ran out of memory, naming the machine's memory and how much the attempt used. Anything else that stops an attempt, such as a crash or a kill -9, is an ordinary failure and is retried as usual too.