Diffinery

homeflare/celldpublic

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