| 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. An application uses one for code that it |
| 5 | does not write itself: code that a customer uploads, or code that a model |
| 6 | writes. celld compiles that code into a separate V8 isolate on the node that |
| 7 | runs the loader, and it gives the loaded Worker no binding of its own, so the |
| 8 | loader hands over every capability that the loaded code can use, and the |
| 9 | durable state stays behind in the loader's own bindings. Cloudflare runs this |
| 10 | feature in an |
| 11 | [open beta](https://developers.cloudflare.com/changelog/post/2026-03-24-dynamic-workers-open-beta/), |
| 12 | therefore the API can still change. Read the |
| 13 | [Cloudflare Dynamic Workers documentation](https://developers.cloudflare.com/dynamic-workers/) |
| 14 | for the standard API behavior. |
| 15 | |
| 16 | ## Example |
| 17 | |
| 18 | The [Dynamic Workers example](../../examples/dynamic-worker-tails) loads a |
| 19 | Worker and sends its invocation records to a Tail Worker. |
| 20 | |
| 21 | <!-- celld-example: dynamic-worker-tails --> |
| 22 | |
| 23 | ## Loading a Worker |
| 24 | |
| 25 | A `worker_loaders` entry in the Wrangler configuration adds the binding, and |
| 26 | the example names it `LOADER`. Wrangler accepts only the `binding` name in |
| 27 | this entry, so set `tails` in a `WorkerCode` object and set `limits` there or |
| 28 | in a `getEntrypoint()` call. celld stops the deployment when the entry |
| 29 | contains either option. `env.LOADER.get(id, getCode)` gives a loaded |
| 30 | Worker a string id, and `env.LOADER.load(code)` loads an anonymous one. celld |
| 31 | calls `getCode` only when it must compile, so the callback can read the code |
| 32 | from a store, and it can be asynchronous. A throw inside `getCode` therefore |
| 33 | surfaces when the application first uses the Worker, and not at the `get()` |
| 34 | call. |
| 35 | |
| 36 | `getCode` returns a `WorkerCode` object. `mainModule` names the entry module, |
| 37 | and `modules` maps each module name to its source. A value in that map can also |
| 38 | be a module object such as `{ wasm: bytes }`, and the |
| 39 | [WebAssembly page](../wasm.md#dynamic-workers) shows that form. Bare bytes are |
| 40 | not a module, so celld refuses them as workerd does. A static or dynamic relative import resolves from the importing |
| 41 | module's name. Thus, `dir/a.js` can import `./b.js` when the map contains |
| 42 | `dir/b.js`. `compatibilityDate` is required, as in workerd, and |
| 43 | `compatibilityFlags` is optional. Both select the runtime behavior of the loaded |
| 44 | Worker. The module sources can total 64 MiB by default, matching workerd. |
| 45 | A fleet operator can set `CELLD_MAX_DYNAMIC_WORKER_CODE_BYTES` to a positive |
| 46 | byte count before starting each node to admit larger module sets. This is |
| 47 | node configuration; loaded code cannot raise it through `WorkerCode`. Heap, |
| 48 | execution, and admission limits continue to apply independently. |
| 49 | A `WorkerCode.limits` object can set |
| 50 | `cpuMs` and `subRequests` for each invocation. celld refuses a `WorkerCode` |
| 51 | that sets `allowExperimental`. Read the |
| 52 | [API reference](https://developers.cloudflare.com/dynamic-workers/api-reference/) |
| 53 | for the complete shape. |
| 54 | |
| 55 | The stub that `get()` and `load()` return invokes the loaded code. |
| 56 | `getEntrypoint(name, options)` gives a Fetcher for the default export or for a |
| 57 | named `WorkerEntrypoint`, and `getDurableObjectClass(name, options)` gives a |
| 58 | Durable Object class. Both accept a `props` option. `getEntrypoint()` also |
| 59 | accepts `limits` for its fetch and RPC calls. celld uses the lower value for |
| 60 | each limit when both the `WorkerCode` object and the call set it. The |
| 61 | `getDurableObjectClass()` options accept only `props`. |
| 62 | |
| 63 | ## The isolation boundary |
| 64 | |
| 65 | celld compiles the loaded code into its own V8 isolate, with its own context |
| 66 | and its own heap. That isolate runs in the same process as the loader, on the |
| 67 | same node. It is not a second process and it is not a virtual machine, so the |
| 68 | boundary scopes what the loaded code can address, and it is not a claim about |
| 69 | V8 escape safety. Run the code in a container under a named runtime when the |
| 70 | workload needs a kernel boundary, and read |
| 71 | [the isolation boundary](containers.md#the-isolation-boundary) on the |
| 72 | Containers page. |
| 73 | |
| 74 | celld builds the loaded Worker with an empty binding set. It carries no KV |
| 75 | namespace, no D1 database, no R2 bucket, no queue, no Workflow, no Durable |
| 76 | Object class, and no `vars` entry of the deployment. It also carries no Worker |
| 77 | Loader binding, so a loaded Worker cannot load another one. The loaded code |
| 78 | therefore reaches only the `env` that the `WorkerCode` supplies. That `env` |
| 79 | takes structured-clone values and Service Binding capabilities, and Cloudflare |
| 80 | gives the same |
| 81 | [capability rule](https://developers.cloudflare.com/dynamic-workers/usage/bindings/). |
| 82 | A capability call leaves the loaded isolate as an RPC into the loader's |
| 83 | isolate, so the loader keeps control of what the call does. |
| 84 | |
| 85 |  |
| 86 | |
| 87 | The runtime internals stay out of reach as well. celld passes each host |
| 88 | operation to its internal scripts as a function parameter and never as a |
| 89 | global, so loaded code cannot find one by reading `globalThis`. A loaded Worker |
| 90 | once reached the host through such a global, and the parameter form closed that |
| 91 | path. |
| 92 | |
| 93 | `globalOutbound` decides what the loaded code can dial. celld gives the loaded |
| 94 | Worker the loader's own egress policy when the `WorkerCode` omits the field, so |
| 95 | a deployed Worker with Internet access passes that access on. A `null` value |
| 96 | removes every ambient connection instead, and `fetch()` and `connect()` both |
| 97 | throw, which leaves an `env` capability as the only way out. A Fetcher |
| 98 | brokers each `fetch()` through the loader, which can read the request, change |
| 99 | it, or refuse it. celld does not broker a `connect()` or a WebSocket through a |
| 100 | Fetcher, and each of those throws, because celld's service protocol carries no |
| 101 | bidirectional tunnel and a direct socket would pass the broker in silence. Read |
| 102 | the |
| 103 | [egress control documentation](https://developers.cloudflare.com/dynamic-workers/usage/egress-control/) |
| 104 | for the Cloudflare behavior. |
| 105 | |
| 106 | ## Lifetime and caching |
| 107 | |
| 108 | `get(id, getCode)` memoizes the load by id inside one loader binding of one |
| 109 | host isolate. The first use of an id runs `getCode` and compiles the modules, |
| 110 | and a later use of that id in the same isolate reuses the compiled isolate and |
| 111 | runs no callback while the loaded Worker is alive. Module scope therefore |
| 112 | survives between the two calls. Another isolate holds its own map. A request |
| 113 | that a different node serves, or a fresh isolate on the same node, pays the |
| 114 | compile again. Two `worker_loaders` entries are two maps as well, so the same |
| 115 | id in each one loads twice. |
| 116 | Cloudflare describes the same reuse as a possibility and not as a guarantee. |
| 117 | |
| 118 | `load(code)` caches nothing and compiles on each call. For both methods, a |
| 119 | stub, an entrypoint, a Durable Object class, or a running facet keeps its |
| 120 | loaded Worker alive. The id map holds only a weak reference, so the garbage |
| 121 | collector can release a Worker after its last reference disappears. A later |
| 122 | `get()` call with that id then runs `getCode` again. The garbage collector does |
| 123 | not guarantee a release time. |
| 124 | |
| 125 | The `dispose()` method of a stub releases its loaded Worker explicitly, and it |
| 126 | removes the id from the map. The other stubs for that load cannot start new |
| 127 | calls, and a later `get()` call creates a replacement. An in-flight call |
| 128 | finishes with the Worker that started it. |
| 129 | |
| 130 | Every loaded Worker belongs to the isolate that created it. celld drops all of |
| 131 | them when that isolate retires, which happens when the request load falls or |
| 132 | when a new deployment supersedes the script. An in-flight call finishes first. |
| 133 | A loaded Worker therefore never outlives its loader, and a redeploy compiles |
| 134 | the code again. |
| 135 | |
| 136 | A process holds at most 256 live Dynamic Workers, and one generation of one |
| 137 | script holds at most 255 of them. The last slot stays reserved, so a single |
| 138 | runaway loader cannot take the whole process. A load over either limit throws. |
| 139 | |
| 140 | ## Differences from Cloudflare |
| 141 | |
| 142 | - The process limit is 256 live Dynamic Workers, and each script generation can |
| 143 | use 255 slots. Every loader and script shares the process limit. |
| 144 | - The process rejects the removed `CELLD_MAX_LOADED_WORKERS` environment |
| 145 | variable. |
| 146 | - `getEntrypoint()` supports `props` and `limits` options, while |
| 147 | `getDurableObjectClass()` supports only `props`. A structured-clone encoded |
| 148 | `props` value can be at most 1 MiB. |
| 149 | - A `globalOutbound` Fetcher cannot use `connect()` or a WebSocket. |
| 150 | - A `WorkerCode.tails` array accepts Service Binding Fetchers. Each Fetcher |
| 151 | receives one event after a Dynamic Worker fetch invocation finishes. |
| 152 | - The event contains the request metadata, the response status, the console |
| 153 | logs, the uncaught exception, and the invocation outcome. |
| 154 | - celld records at most 256 KiB of serialized console log records for each |
| 155 | invocation. It stops recording when the next complete record exceeds this |
| 156 | limit. |
| 157 | - Tail delivery starts after the response is available. A Tail Worker failure |
| 158 | does not change the response, and celld writes the failure to the console. |
| 159 | - celld enforces `cpuMs` and `subRequests` in `WorkerCode.limits` and in a |
| 160 | `getEntrypoint()` call. It rejects `allowExperimental`. |
| 161 | - `WorkerCode.env` accepts structured-clone values and Service Binding |
| 162 | capabilities. The encoded values and the capability props can total 1 MiB. |
| 163 | - A loaded Worker entrypoint cannot transfer to another Worker. Awaitable and |
| 164 | pipelined properties are also unavailable. |
| 165 | |
| 166 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page |
| 167 | lists the runtime APIs and the unsupported services. |