Diffinery

homeflare/celldpublic

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