Diffinery

homeflare/celldpublic

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