Diffinery

homeflare/celldpublic

celld/README.md
403 lines · 18.5 KBRaw History
1# celld
2
3Self-hosted, distributed **Durable Objects**.
4
5celld is an open-source daemon that runs a Cloudflare Workers application
6on your own machines: Workers, Durable Objects, KV, Queues, D1, R2,
7Workflows, Cron Triggers, and static assets, deployed from the
8`wrangler.json` that you already have. Each object is a cell: a named
9server with its own SQLite database. celld stores the long-term state in
10your S3-compatible, Google Cloud Storage, or Azure Blob Storage bucket.
11It needs no serving control plane or consensus service.
12An inactive cell costs almost nothing. Learn more at
13[celld.dev](https://celld.dev) or read the
14[documentation](https://celld.dev/docs).
15
16## How it works
17
18A node is one `celld` process, and you run one on each machine. Every
19node embeds V8 and executes Wrangler bundles. The nodes that share one
20bucket are a fleet, and that bucket holds the deployments, the cell
21state, and small ownership records. A conditional bucket write gives a node the
22ownership of a cell, so exactly one node owns a cell at a time. The fleet
23needs no membership protocol, failure detector, or consensus service. Signed
24peer HTTP provides routing and replicated-log transport.
25
26celld captures each committed SQLite write in the LTX replication format.
27A single node proves the write durable by uploading that data to the bucket. A fleet of two or more nodes proves it
28sooner: the owner sends the data to one or two other nodes, and the write
29is durable as soon as they hold it on their disks. celld uploads the data
30to the bucket afterwards. A single node has no other node to send to, so
31each write waits for the bucket, and those writes are much slower.
32Before a takeover restores a cell, celld recovers an open log from the
33prior owner, so the bucket holds the long-term state and nodes stay
34replaceable. See [what celld guarantees](docs/guarantees.md) for the
35complete protocol.
36
37## What runs on celld
38
39celld runs the programmatic Workers platform: the runtime, and each binding
40that Cloudflare builds on Workers and Durable Objects. A KV namespace, a
41queue, a D1 database, and a Workflow are each a cell, so
42they get the same lease, the same replication, and the same failover as a
43Durable Object. R2 bindings read, write, and list objects directly in the
44fleet bucket. Each row links to a project that deploys as-is:
45
46| service | example |
47| --- | --- |
48| Workers: fetch handlers, service bindings, JS RPC, Node.js compat | [`hello`](examples/hello) |
49| Durable Objects: SQLite storage, alarms, hibernating WebSockets | [`counter`](examples/counter) |
50| KV: list, metadata, expiration, bulk import | [`kv`](examples/kv) |
51| Queues: producers, batching consumers, retries, dead letters | [`queues`](examples/queues) |
52| D1: SQL databases, batches, migrations | [`d1`](examples/d1) |
53| R2: reads, writes, lists, multipart uploads | [`r2`](examples/r2) |
54| Workflows: durable steps, sleeps, events, pause and restart | [`workflow`](examples/workflow) |
55| Cron Triggers: one run for each occurrence across the fleet | [`cron`](examples/cron) |
56| Static assets: asset-only or with a Worker, `_headers`, `_redirects` | [`static-assets`](examples/static-assets) |
57| Dynamic Workers: runtime-loaded code and Tail Worker observability | [`dynamic-worker-tails`](examples/dynamic-worker-tails) |
58| Containers (experimental): a Durable Object that supervises a container, `@cloudflare/containers` | [`container`](examples/container) |
59| Sandboxes: the Cloudflare Sandbox SDK, `@cloudflare/sandbox`, on a container per sandbox | [`sandbox`](examples/sandbox) |
60
61A product that needs the Cloudflare network, a GPU, or a browser farm is out
62of scope. The [Cloudflare compatibility](docs/cloudflare-compat.md) page
63lists each gap in the services above.
64
65## Install
66
67The installer downloads the `celld` binary (provenance is verifiable with
68`gh attestation verify`):
69
70```sh
71curl -fsSL https://celld.dev/install.sh | sh
72```
73
74Put `~/.local/bin` on your `PATH` if the installer asks you to.
75
76Worker projects deployed with `celld deploy` need
77[esbuild](https://esbuild.github.io) on `PATH`; asset-only projects do not.
78
79The installer keeps each release under `~/.local/lib/celld/releases` and points
80one symlink at the current one. To remove celld, delete the symlink and the
81releases:
82
83```sh
84rm `which celld` && rm -rf ~/.local/lib/celld
85```
86
87## Container
88
89The release image contains the `celld` binary and is published for Linux
90x86-64 and ARM64:
91
92```sh
93docker run --rm ghcr.io/denoland/celld --version
94```
95
96Persist the runtime's local state and pass the standard AWS credential
97environment through:
98
99```sh
100docker volume create celld-state
101docker run --rm --network host \
102 -e AWS_ACCESS_KEY_ID \
103 -e AWS_SECRET_ACCESS_KEY \
104 -e AWS_SESSION_TOKEN \
105 -e CELLD_WATCH=/var/lib/celld/state \
106 -v celld-state:/var/lib/celld \
107 ghcr.io/denoland/celld \
108 --bucket s3://my-cells-bucket \
109 --endpoint https://ACCOUNT.r2.cloudflarestorage.com \
110 --region auto \
111 --listen 0.0.0.0:8080 \
112 --internal-listen 10.0.0.12:8081 \
113 --advertise node-a.internal:8081
114```
115
116Drop `--endpoint` and `--region` for AWS S3. Expose port 8080 through the load
117balancer, and keep port 8081 on the private network.
118
119## Run it
120
121Run an application locally without a cloud bucket:
122
123```sh
124celld dev
125```
126
127The command starts one celld node with a local object store, so it needs
128no Docker or cloud bucket. The Worker listener uses
129`http://127.0.0.1:9876`. Use `celld dev --port PORT` to select a different
130Worker port. Use `celld dev --host IP` to select a different interface. A
131non-loopback IP exposes the Worker listener to the network, and the internal
132operator listener stays on loopback. The command keeps the application state
133in `.celld/dev`, so a later invocation uses the same durable data.
134
135The state also survives a configuration change, and celld does not migrate it,
136so an object can keep a value that the new configuration rejects. The failure
137then looks unrelated to the change. Use `celld dev --clean` to delete
138`.celld/dev` before the server starts and run from an empty local state.
139
140The default display highlights the application URL and hides the node
141warning and information logs. Use `celld dev --logs` to show these logs.
142The errors remain visible without this flag. Set `NO_COLOR` to disable color,
143or set `FORCE_COLOR` to enable color when the output is not a terminal.
144`NO_COLOR` always takes priority.
145
146The command watches the project and rebuilds the application after a
147source or configuration change. It keeps the current application running
148if a build fails, and a successful restart retains the durable state. See
149the [documentation](docs/README.md#develop-an-application-locally) for
150the complete local-development contract.
151
152celld uses the standard AWS credential chain. On Amazon EKS, celld reads the
153Pod Identity credentials from the injected environment variables and the
154authorization-token file. Deploy to an S3-compatible bucket, then start celld
155against the same bucket:
156
157```sh
158celld deploy . \
159 --bucket s3://my-cells-bucket
160
161celld \
162 --bucket s3://my-cells-bucket \
163 --listen 0.0.0.0:8080 \
164 --internal-listen 10.0.0.12:8081 \
165 --advertise 10.0.0.12:8081
166```
167
168One node proves each write through the bucket, so a write costs one storage
169round trip. Start a second node against the same bucket, and celld sends each
170write to it: the write then finishes as soon as the second node holds the data
171on its disk, which is much faster than the round trip.
172
173Run two or more nodes if write latency matters to you. The second node needs
174no extra configuration, because a node finds the other nodes through the
175bucket.
176
177How much faster depends on how far your bucket is and how loaded the fleet
178is. A write to a region-local store takes about 90 ms. One loaded lab fleet
179measured about 600 ms against a store that was not region-local, and about
18025 ms once a second node held the write. See
181[what celld guarantees](docs/guarantees.md#the-ensemble-needs-two-nodes).
182
183Use `--endpoint` for another S3-compatible service and `--region` when it
184cannot be inferred. A `gs://` bucket selects Google Cloud Storage: celld then
185uses the Cloud Storage XML API with generation preconditions and
186authenticates with Application Default Credentials. celld rejects an S3
187`--endpoint` for a `gs://` bucket, and it ignores the storage region:
188
189```sh
190celld deploy . --bucket gs://my-cells-bucket
191celld --bucket gs://my-cells-bucket --listen 0.0.0.0:8080 \
192 --internal-listen 10.0.0.12:8081 --advertise 10.0.0.12:8081
193```
194
195An `az://` bucket selects Azure Blob Storage, where the NAME is the
196container and `AZURE_STORAGE_ACCOUNT_NAME` names the storage account.
197celld requires exactly one credential family: a storage account key, a
198managed identity, or a workload identity. An AKS workload identity uses
199`AZURE_AUTHORITY_HOST`, `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, and
200`AZURE_FEDERATED_TOKEN_FILE`, and the authority host must identify the
201public Azure cloud. A Microsoft Entra identity needs data-plane permission
202to read, write, list, and delete blobs; the `Storage Blob Data Contributor`
203role supplies these. celld rejects an S3 `--endpoint` for an `az://` bucket,
204and it ignores the storage region. See
205[what celld guarantees](docs/guarantees.md) for the qualification:
206
207```sh
208export AZURE_STORAGE_ACCOUNT_NAME=myaccount
209celld deploy . --bucket az://my-cells-container
210celld --bucket az://my-cells-container --listen 0.0.0.0:8080 \
211 --internal-listen 10.0.0.12:8081 --advertise 10.0.0.12:8081
212```
213
214A fleet runs one application, and every node loads its latest successfully
215committed deployment from `deploy/current.json`. `celld deploy` invokes
216`esbuild` from `PATH` for Worker code, accepts the supported Wrangler config
217subset (including co-deployed or asset-only static assets), and writes the
218deployment objects directly, using the documented types in
219`crates/celld/protocol.rs`. Every node discovers owners and peers from
220bucket leases; there is no account or join service. Run `celld --help` for
221the complete command line.
222
223An upgrade from v0.4.1 preserves the existing bucket and node data directories.
224Stop all old nodes, then start the new binaries with the same configuration.
225Startup upgrades the wake format automatically before a node serves traffic.
226Mixed versions cannot share a serving fleet.
227The [wake format contract](docs/guarantees.md#start-a-fleet-with-this-format)
228describes the startup checks and the boundary for existing fleet data.
229
230Peer HTTP and the operator API use the internal listener. Put every
231advertised address on a trusted private network or an encrypted overlay such
232as WireGuard or Tailscale, and do not publish the internal port. celld
233rejects a literal public IP unless you supply `--unsafe-public-advertise`.
234An explicit advertised address requires an explicit internal-listener
235address, and you must route the advertised address to the internal
236listener, because celld cannot verify a hostname or a translated port. The first
237current node creates `fleet/peer-auth.json` in the bucket. Cell fetch and RPC
238requests carry a protocol version and depend on the trusted private network.
239Peer-control and reserved-cell operator requests use the fleet HMAC. This HMAC
240binds each body and supplies a clock limit and replay protection. Treat access
241to the bucket and its credentials as fleet administrator access.
242
243## Operate a fleet
244
245`celld diagnose` enumerates every node lease by default, then performs a signed
246direct probe of each live peer:
247
248```sh
249celld diagnose --bucket s3://my-cells-bucket
250```
251
252The report keeps checking after an individual failure and distinguishes
253expired records, malformed or unsafe advertise addresses, unreachable peers,
254and incompatible protocols. It also prints each node's coarse owned-cell,
255resident-cell, WebSocket, RSS, CPU, file-descriptor, pressure, and shedding
256sample. Pass one or more `--peer NODE_ID` options to restrict the check.
257
258`celld cell list` lists the Durable Object instances in the fleet bucket:
259
260```sh
261celld cell list --bucket s3://my-cells-bucket
262```
263
264The command prints one `Class:ID` cell scope per line. Give a class name to
265list only the instances of that class, and pass `--json` for one JSON object
266per line. An instance appears after the first event reaches it, because its
267owner then writes an ownership record to the bucket. An ID that an application
268only derives does not appear.
269
270The listing is bounded. One storage request returns at most 1000 instances,
271so the command prints at most 1000 and reports on stderr that more exist.
272Pass `--after SCOPE` to continue from the last instance printed, or `--all`
273to read the whole listing.
274
275`celld d1` runs SQL and migrations against a deployed D1 database. It finds a
276node through the same node leases, and that node sends the work to the node
277that owns the database:
278
279```sh
280celld d1 migrations apply ledger --bucket s3://my-cells-bucket
281```
282
283The migration file extension is ASCII case-insensitive, so `.sql` and `.SQL`
284files are migrations. The command ignores a file with a different extension.
285
286`celld kv` reads and writes a deployed KV namespace. Its bulk commands use the
287Wrangler file format, so a Wrangler export can migrate directly into celld:
288
289```sh
290celld kv bulk put sessions wrangler-export.json \
291 --bucket s3://my-cells-bucket
292```
293
294`celld r2` reads and writes the objects behind an `r2_buckets` binding. The
295command reads the fleet bucket directly, so it needs no running node and a
296release pipeline can publish an artifact before it deploys the Worker:
297
298```sh
299celld r2 put assets app.zip --path dist/app.zip \
300 --content-type application/zip \
301 --metadata '{"release":"1.2.3"}' \
302 --bucket s3://my-cells-bucket
303```
304
305`celld queue` inspects and controls a deployed Queue. A queue can continue to
306accept messages while delivery is paused:
307
308```sh
309celld queue info jobs --bucket s3://my-cells-bucket
310celld queue pause jobs --bucket s3://my-cells-bucket
311celld queue resume jobs --bucket s3://my-cells-bucket
312```
313
314Set a hard resident-cell limit on each loaded node:
315
316```sh
317CELLD_MAX_RESIDENT_CELLS=1000 \
318celld --bucket s3://my-cells-bucket --listen 0.0.0.0:8080 \
319 --internal-listen 10.0.0.12:8081 --advertise node-a.internal:8081
320```
321
322celld balances ownership across the fleet. Every node reads a shared fleet
323sample every five seconds. One node refreshes the sample from the node
324leases, so each refresh reads each lease once for the fleet. The node with
325the most owned cells per unit of weight hands at most 32 hibernated cells
326per sample to the peer that is furthest below its share. A hibernated cell moves as one record write, and
327its parked hibernatable WebSockets close with code 1012 so the clients
328reconnect to the new owner. A resident cell moves only after idle eviction
329hibernates it (`CELLD_IDLE_EVICT_S`). Set `CELLD_PLACEMENT_WEIGHT` to give a
330node a larger or a smaller share than its CPU count, and set
331`CELLD_REBALANCE_INTERVAL_MS=0` to disable balancing. `POST /rebalance/pause`
332on the internal listener of any node pauses the fleet.
333
334celld enables a memory-pressure threshold at 80% of the available memory by
335default. Set `CELLD_MAX_RSS_MB` to change the threshold, or set it to `0` to
336disable memory-pressure shedding. In a Linux cgroup, the threshold uses the
337greater of the allocator-adjusted RSS and the active cgroup working set. celld
338calculates the working set as `memory.current` less `inactive_file` from
339`memory.stat`, then it removes the measured allocator slack. This calculation
340includes active kernel charges that process RSS does not report, and it excludes
341file pages and allocator pages that celld cannot return by shedding a cell.
342Before each sample, celld returns the memory that the C allocator keeps after
343a cell stops, so a hibernated cell does not hold its storage cache in RSS. The
344`/state` route reports all four input measurements.
345
346A separate absolute cap applies to the complete cgroup charge at 95% of the
347available memory. celld uses the process RSS when it cannot read a cgroup
348charge. The cap protects the node when shedding cannot return a kernel charge.
349The node logs a warning when the cap applies. The cap is a share of the
350available memory, not a share of the threshold. Therefore, a
351`CELLD_MAX_RSS_MB` value at or above 95% makes the cap the effective limit, and
352celld reports the decision at startup.
353`CELLD_MAX_RSS_MB=0` disables the threshold and the cap together. When celld
354cannot read the size of the available memory, it applies a cap of 125% of an
355explicit threshold.
356
357Under pressure, celld durably replicates and fences the least-recently used
358idle cells, publishes them as unowned without resetting their epochs, and
359refuses to reacquire new unowned cells. It does not shed a cell with active
360work or a live host WebSocket. A spare node receives no assignment; it
361acquires a released cell through the same bucket protocol when normal
362traffic reaches it. Each limit releases separately. The threshold releases at
36380% of its value, and the cap releases at 80% of its value. Therefore, a
364crossing of one limit does not hold the node against the other.
365
366Each isolate also has a V8 heap limit, separate from the memory of the node.
367The default is 128 MB, and it matches the limit of a Durable Object on
368Cloudflare; set `CELLD_V8_HEAP_LIMIT_MB` to change it. Each hibernatable
369WebSocket client holds state in the heap, so the limit decides how many
370clients a cell can carry: approximately 50,000 at the default, and
371approximately 512 MB for 100,000.
372
373An isolate above 90% of this limit refuses a new hibernatable WebSocket, and
374it accepts again when the use of the heap falls under 90%. An isolate that
375reaches the limit also stops the materialization of a SQL result set; both
376errors name the heap. celld measures the heap before each event, and the
377isolate serves again when the use falls under 75% of the limit. An idle
378isolate holds a dead heap until something allocates, so celld forces a
379collection when a measurement is above that share. A restart of the process
380is not necessary.
381
382## Contributions
383
384Pull requests are disabled. Coding agents make it too easy to send a large,
385low-context change that costs maintainers more time than it saves. Thoughtful
386contributions are welcome; please understand the code, keep the patch focused,
387and respect the review time you are asking for.
388
389Send a `git format-patch` attachment to [[email protected]](mailto:[email protected]).
390
391Contributor License Agreement: By emailing a patch, you certify that you have
392the right to submit it and assign to Deno Land Inc. all rights in the patch
393that you can assign. Where a right cannot be assigned, you grant Deno Land
394Inc. a perpetual, irrevocable, worldwide, royalty-free, transferable,
395sublicensable license to use, modify, combine, relicense, redistribute, or
396publish the patch, in whole or in part, with or without attribution.
397
398## License
399
400[Apache-2.0](LICENSE)
401
402See the [limitations](docs/limitations.md) and
403[security](docs/security.md) pages before operating a public fleet.