| 1 | # Dynamic Workers |
| 2 | |
| 3 | A Dynamic Worker is Worker code that a running Worker supplies at runtime and |
| 4 | loads through a Worker Loader binding, such as code that a customer uploads or |
| 5 | that a model writes. celld compiles the code into a separate V8 isolate on the |
| 6 | node of the loader. Cloudflare runs this feature in an |
| 7 | [open beta](https://developers.cloudflare.com/changelog/post/2026-03-24-dynamic-workers-open-beta/), |
| 8 | so the API can change. Read the |
| 9 | [Cloudflare Dynamic Workers documentation](https://developers.cloudflare.com/dynamic-workers/) |
| 10 | and the |
| 11 | [API reference](https://developers.cloudflare.com/dynamic-workers/api-reference/) |
| 12 | for the standard API. |
| 13 | |
| 14 | ## Example |
| 15 | |
| 16 | The [Dynamic Workers example](../../examples/dynamic-worker-tails) loads a |
| 17 | Worker and sends its invocation records to a Tail Worker. |
| 18 | |
| 19 | <!-- celld-example: dynamic-worker-tails --> |
| 20 | |
| 21 | ## API |
| 22 | |
| 23 | - `env.LOADER.get(id, getCode)` loads a Worker under a string id, and |
| 24 | `env.LOADER.load(code)` loads an anonymous Worker. |
| 25 | - `getCode` returns a `WorkerCode` object: `mainModule`, `modules`, |
| 26 | `compatibilityDate`, `compatibilityFlags`, `env`, `globalOutbound`, `limits`, |
| 27 | and `tails`. |
| 28 | - `stub.getEntrypoint(name, options)` returns a Fetcher for the default export |
| 29 | or for a named `WorkerEntrypoint`. |
| 30 | - `stub.getDurableObjectClass(name, options)` returns a Durable Object class, |
| 31 | for example for a [facet](durable-object-facets.md). |
| 32 | - `stub.dispose()` releases the loaded Worker. |
| 33 | |
| 34 | ## Loading a Worker |
| 35 | |
| 36 | A `worker_loaders` entry accepts only the `binding` name, as in Wrangler. |
| 37 | celld stops the deployment when the entry sets `tails` or `limits`; set them in |
| 38 | the `WorkerCode` object, or set `limits` in a `getEntrypoint()` call. |
| 39 | |
| 40 | celld calls the `getCode` callback of `env.LOADER.get(id, getCode)` only when |
| 41 | it must compile, so a throw in `getCode` surfaces at the first use of the |
| 42 | Worker, not at `get()`. |
| 43 | |
| 44 | - A `modules` value can be a module object such as `{ wasm: bytes }`; the |
| 45 | [WebAssembly page](../wasm.md#dynamic-workers) shows that form. celld refuses |
| 46 | bare bytes, as workerd does. |
| 47 | - A relative import resolves from the name of the importing module, so |
| 48 | `dir/a.js` can import `./b.js` when the map contains `dir/b.js`. |
| 49 | - `compatibilityDate` is required, as in workerd. |
| 50 | - The module sources can total 64 MiB by default, as in workerd. An operator |
| 51 | can set `CELLD_MAX_DYNAMIC_WORKER_CODE_BYTES` on each node to a larger byte |
| 52 | count. Loaded code cannot raise it, and the heap, execution, and admission |
| 53 | limits still apply. |
| 54 | - When both `WorkerCode.limits` and `getEntrypoint()` set a limit, celld uses |
| 55 | the lower value. |
| 56 | |
| 57 | ## The isolation boundary |
| 58 | |
| 59 | The loaded isolate has its own context and heap, but it runs in the process of |
| 60 | the loader. It is not a second process or a virtual machine, so the boundary |
| 61 | scopes what the code can address and makes no claim about V8 escape safety. |
| 62 | For a kernel boundary, run the code in a container under a named runtime; read |
| 63 | [the isolation boundary](containers.md#the-isolation-boundary) on the |
| 64 | Containers page. |
| 65 | |
| 66 | The loaded Worker gets no binding and no `vars` entry of the deployment, and no |
| 67 | Worker Loader binding, so it cannot load another Worker. It reaches only the |
| 68 | `env` of its `WorkerCode`. A capability call is an RPC into the isolate of the |
| 69 | loader. celld passes host operations to its internal scripts as function |
| 70 | parameters, never as globals, so `globalThis` exposes none. |
| 71 | |
| 72 | When `WorkerCode` omits `globalOutbound`, the loaded Worker inherits the egress |
| 73 | policy of the loader. A `null` value makes `fetch()` and `connect()` throw. A |
| 74 | Fetcher brokers each `fetch()` through the loader. A `connect()` or a WebSocket |
| 75 | through a Fetcher throws, because the celld service protocol carries no |
| 76 | bidirectional tunnel. |
| 77 | |
| 78 | ## Lifetime and caching |
| 79 | |
| 80 | `get(id, getCode)` memoizes the compiled Worker by id inside one loader binding |
| 81 | of one host isolate, so module scope survives between calls there. Each |
| 82 | isolate, each node, and each `worker_loaders` entry has its own map, and pays |
| 83 | the compile again. Cloudflare describes this reuse as possible, not |
| 84 | guaranteed. `load(code)` compiles on each call. |
| 85 | |
| 86 | A stub, an entrypoint, a Durable Object class, or a running facet keeps its |
| 87 | loaded Worker alive. The id map holds a weak reference, so after the last |
| 88 | reference disappears the garbage collector can release the Worker, at no |
| 89 | guaranteed time, and a later `get()` runs `getCode` again. |
| 90 | |
| 91 | The `dispose()` method of a stub releases its loaded Worker and removes the id |
| 92 | from the map. The other stubs for that load cannot start new calls, and an |
| 93 | in-flight call finishes. |
| 94 | |
| 95 | celld drops every loaded Worker when its host isolate retires, after in-flight |
| 96 | calls finish. A loaded Worker never outlives its loader, and a redeploy |
| 97 | compiles the code again. |
| 98 | |
| 99 | ## Differences from Cloudflare |
| 100 | |
| 101 | - The process limit is 256 live Dynamic Workers, and each script generation can |
| 102 | use 255 slots, so one loader cannot take the whole process. Every loader and |
| 103 | script shares the process limit, and a load over either limit throws. |
| 104 | - The process rejects the removed `CELLD_MAX_LOADED_WORKERS` environment |
| 105 | variable. |
| 106 | - `getEntrypoint()` supports `props` and `limits` options, while |
| 107 | `getDurableObjectClass()` supports only `props`. A structured-clone encoded |
| 108 | `props` value can be at most 1 MiB. |
| 109 | - A `globalOutbound` Fetcher cannot use `connect()` or a WebSocket. |
| 110 | - A `WorkerCode.tails` array accepts Service Binding Fetchers. Each Fetcher |
| 111 | receives one event after a Dynamic Worker fetch invocation finishes. |
| 112 | - The event contains the request metadata, the response status, the console |
| 113 | logs, the uncaught exception, and the invocation outcome. |
| 114 | - celld records at most 256 KiB of serialized console log records for each |
| 115 | invocation. It stops recording when the next complete record exceeds this |
| 116 | limit. |
| 117 | - Tail delivery starts after the response is available. A Tail Worker failure |
| 118 | does not change the response, and celld writes the failure to the console. |
| 119 | - celld enforces `cpuMs` and `subRequests` in `WorkerCode.limits` and in a |
| 120 | `getEntrypoint()` call. It rejects `allowExperimental`. |
| 121 | - `WorkerCode.env` accepts structured-clone values and Service Binding |
| 122 | capabilities. The encoded values and the capability props can total 1 MiB. |
| 123 | - A loaded Worker entrypoint cannot transfer to another Worker. Awaitable and |
| 124 | pipelined properties are also unavailable. |
| 125 | |
| 126 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page |
| 127 | lists the runtime APIs and the unsupported services. |