Diffinery

homeflare/celldpublic

celld/docs/README.md
394 lines · 19.4 KBRaw History
1# celld documentation
2
3celld is a stateful distributed system. It runs server-side JavaScript on
4your machines. It keeps all shared data in an S3-compatible or Google
5Cloud Storage bucket that you own. The JavaScript API is the same API
6that Cloudflare Workers and Durable Objects supply.
7
8In Cloudflare terms, a cell is a Durable Object: a small server with a
9name and a private SQLite database. You make one cell for each user, each
10document, each chat room, or each AI agent. A cell serves HTTP, holds
11WebSocket connections, sets alarms, and makes outbound connections. Each
12cell runs on one thread, so two requests to the same cell never run at
13the same instant. A second request can interleave only while the first
14awaits, and storage operations are synchronous, so a storage operation
15never interleaves at all. The data in a cell therefore stays consistent.
16Cells share no database, and the application divides into cells from the
17start.
18
19A cell has the same states as a Durable Object. A **resident** cell is in
20memory: it is **active** while it does work, and **idle** when it waits.
21celld removes an idle cell from memory. A cell that then keeps its
22hibernatable WebSocket clients, and stays on its node, is **hibernated**.
23A cell that no node holds is **inactive**. An inactive cell is only an
24object in the bucket, so it costs almost zero, and every cell starts in
25this state.
26
27Memory holds nothing across these transitions, so the constructor runs
28again on the next event. A hibernated cell therefore starts as a cold
29start does, and only two facts separate the two states: the WebSocket
30clients stay connected, and the cell stays on its node.
31
32One 8 GB node holds 1,000 resident cells, so one resident cell costs
33approximately $0.05 each month.
34
35The bucket is the coordinator. There is no membership protocol, no
36failure detector, and no consensus service. One atomic write to the
37bucket gives a node the ownership of a cell. Replication sends the SQLite
38data of each cell to the bucket, and celld does not acknowledge a write
39before the data is there (RPO=0). The loss of a node therefore cannot
40lose an acknowledged write. To add a node to the fleet, point the node at
41the bucket. The [ownership and fencing](fencing.md) page gives the full
42mechanism and the exact properties that the bucket must provide.
43
44## What do you build with cells
45
46A cell fits a workload that divides into named, stateful units:
47
48- **Real-time applications.** A multiplayer game, a chat room, or a
49 collaborative document is one cell. The cell holds the WebSocket
50 connections and the state of one room, so the room needs no lock and
51 no external message bus.
52- **Agents.** Each AI agent is one cell. The cell holds the memory, the
53 schedule, and the inbox of one agent in its own SQLite database, and
54 an idle agent hibernates to the bucket, so a large agent fleet costs
55 almost nothing between events.
56- **Sharded web applications.** One cell for each user, each tenant, or
57 each device shards the application from the start. The contention of
58 one shared database does not appear, because no shared database
59 exists.
60
61## Contents
62
63- [What do you build with cells](#what-do-you-build-with-cells)
64- [Install](#install)
65- [Configure object storage](#configure-object-storage)
66- [Deploy an application](#deploy-an-application)
67- [Start a node](#start-a-node)
68- [Add nodes](#add-nodes)
69- [Diagnose a fleet](#diagnose-a-fleet)
70- [Environment variables](#environment-variables)
71- [Cloudflare compatibility](cloudflare-compat.md)
72- [Ownership and fencing](fencing.md)
73- [limitations](limitations.md)
74- [Security](security.md)
75- [Telemetry](telemetry.md)
76- [Testing](testing.md)
77- [WebAssembly](wasm.md)
78
79## Install
80
81The installer downloads the `celld` binary. Replication occurs in the
82celld process. A node does not need an external replicator. If your
83project contains Worker code, `celld deploy` needs esbuild. An asset-only
84project does not need esbuild.
85
86```sh
87curl -fsSL https://celld.dev/install.sh | sh
88```
89
90If the installer tells you, add `~/.local/bin` to `PATH`. To install one
91exact release, set `CELLD_VERSION` to the tag of that release, for example
92`v0.0.1`. To go back to a previous release, run the installer again with
93the tag of that release. The releases are on
94[GitHub](https://github.com/denoland/celld/releases). Each release has a
95GitHub Actions build attestation. To make sure that a downloaded file is
96correct, run `gh attestation verify <asset> --repo denoland/celld`.
97
98## Configure object storage
99
100For an S3-compatible bucket, celld uses the standard AWS credential
101chain. For Cloudflare R2, do these steps. Create a bucket. Create an S3
102API token that has access to that bucket. Then set these variables:
103
104```sh
105export AWS_ACCESS_KEY_ID=...
106export AWS_SECRET_ACCESS_KEY=...
107export AWS_REGION=auto
108export S3_ENDPOINT=https://ACCOUNT_ID.r2.cloudflarestorage.com
109export CELLD_BUCKET=s3://YOUR-BUCKET
110```
111
112For Google Cloud Storage, celld uses Application Default Credentials.
113Create a bucket. Then authenticate with `gcloud auth application-default
114login`, or point `GOOGLE_APPLICATION_CREDENTIALS` at a service-account
115key that has access to the bucket. Then set the bucket:
116
117```sh
118export CELLD_BUCKET=gs://YOUR-BUCKET
119```
120
121A `gs://` bucket takes no `S3_ENDPOINT` and no `AWS_*` credentials, and
122celld ignores the storage region.
123
124On a Compute Engine instance, celld can use the attached service
125account. The access scopes of the instance cap this credential, and the
126default scope permits only storage reads. Create the instance with the
127`cloud-platform` scope, so the IAM role of the service account controls
128the access.
129
130For Azure Blob Storage, the bucket NAME is the container, and the
131storage account comes from the environment. Create a container. Then
132set the account and one credential:
133
134```sh
135export AZURE_STORAGE_ACCOUNT_NAME=YOUR-ACCOUNT
136export AZURE_STORAGE_ACCOUNT_KEY=...
137export CELLD_BUCKET=az://YOUR-CONTAINER
138```
139
140celld accepts three Azure credential families: a storage account key, a
141managed identity, and a workload identity. You must configure exactly one
142family. A system-assigned managed identity needs only the account name. A
143user-assigned managed identity can use `AZURE_CLIENT_ID`, `AZURE_OBJECT_ID`,
144or `AZURE_MSI_RESOURCE_ID` as a selector. You must set exactly one of these
145three selectors, because two selectors can name two different identities and
146celld must not choose between them.
147
148celld reads the managed identity from the Azure instance metadata service.
149An Azure VM and an AKS node supply that service. Azure App Service and Azure
150Container Apps supply a different endpoint, so celld does not support a
151managed identity on those two platforms.
152
153The standard AKS workload identity environment contains
154`AZURE_AUTHORITY_HOST`, `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, and
155`AZURE_FEDERATED_TOKEN_FILE`. celld accepts the public authority host,
156`https://login.microsoftonline.com`, with or without a trailing slash, and it
157rejects a sovereign or custom authority host.
158
159celld rejects each recognized Azure configuration variable outside these
160families. This includes another credential source, an endpoint override, and
161the OneLake endpoint. celld ignores an `AZURE_*` name that `object_store`
1620.11.2 does not recognize, because that name cannot change the client.
163
164A managed identity or a workload identity must have Azure Blob data-plane
165permission to read, write, list, and delete blobs. The built-in `Storage Blob
166Data Contributor` role supplies these permissions. See the
167[Azure built-in roles](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles#storage-blob-data-contributor).
168An `az://` bucket takes no `S3_ENDPOINT` and no `AWS_*` credentials, and celld
169ignores the storage region.
170
171celld qualifies an `az://` bucket for a production fleet: the
172conditional-write contract and the multipart upload path were tested against
173a live Azure account on 2026-08-18, under an account key, a VM managed
174identity, and an AKS workload identity. See
175[ownership and fencing](fencing.md).
176
177For local development against Azurite, set
178`AZURE_STORAGE_USE_EMULATOR=true`. celld then accepts the emulator
179endpoint. Azurite is a development store, so celld does not qualify it
180for a fleet either.
181
182The bucket credentials give full control of the fleet. Keep them safe. The
183bucket contains the deployments, the SQLite replicas, the ownership
184records, the node leases, and the peer-authentication secret.
185
186A bucket value can add a key prefix: `s3://YOUR-BUCKET/PREFIX`. Every
187object of the fleet then goes below `PREFIX/`, so two fleets can share one
188bucket. A bucket value without a prefix keeps the objects at the root of
189the bucket, therefore an existing fleet does not move its data.
190
191The store must provide conditional writes and read-after-write
192consistency, because the ownership records depend on them. Amazon S3,
193Cloudflare R2, Google Cloud Storage, Tigris, and Azure Blob Storage
194qualify; MinIO (community edition), Backblaze B2, Hetzner, and
195DigitalOcean Spaces do not.
196See [ownership and fencing](fencing.md) for the exact requirements.
197
198## Deploy an application
199
200If the project contains Worker code, install `esbuild` on `PATH`. Then run
201`celld deploy` from an applicable Wrangler project:
202
203```sh
204git clone https://github.com/denoland/celld
205cd celld/examples/counter
206celld deploy . \
207 --bucket "$CELLD_BUCKET" \
208 --endpoint "$S3_ENDPOINT" \
209 --region "$AWS_REGION"
210```
211
212`celld deploy` accepts module Workers, Durable Object bindings, and static
213assets. An asset project can include a Worker or be asset-only. The asset
214functions include the assets binding, HTML handling, not-found handling,
215worker-first routes, `_headers`, and `_redirects`. If the Wrangler
216configuration contains an unknown key, the deploy stops with an error. See
217the [limitations](limitations.md) for the current deployment boundary.
218
219## Start a node
220
221For local development, the default listener is sufficient:
222
223```sh
224celld \
225 --bucket "$CELLD_BUCKET" \
226 --endpoint "$S3_ENDPOINT" \
227 --region "$AWS_REGION"
228```
229
230For a fleet node, bind the public and internal listeners separately. The
231ingress can reach the public listener, and the other nodes can reach the
232internal listener:
233
234```sh
235celld \
236 --bucket "$CELLD_BUCKET" \
237 --endpoint "$S3_ENDPOINT" \
238 --region "$AWS_REGION" \
239 --listen 0.0.0.0:8080 \
240 --internal-listen 10.0.0.12:8081 \
241 --advertise node-a.internal:8081
242```
243
244An explicit advertised address requires an explicit internal-listener address.
245Set both command-line options, or use their equivalent environment variables.
246celld also rejects an explicit non-loopback public listener without an explicit
247internal listener. This rule identifies an obsolete one-listener configuration.
248
249celld cannot verify that an advertised hostname or a translated port reaches
250the internal listener. You must route the advertised address to the internal
251listener, and you must not route it to the public Worker listener.
252
253## Add nodes
254
255Start each node with the same bucket settings. Give each internal listener a
256different address that the other nodes can reach. Set `--advertise` to that
257internal address. The nodes find each other through the leases in the bucket.
258There is no join command and no fixed membership list.
259
260The bucket supplies discovery and authority. The bucket does not supply
261network reachability. The peer HTTP protocol has a version, a body
262signature, an HMAC, a clock limit, and replay protection. celld does not
263terminate TLS. Put the advertised addresses on a private network that you
264trust, or on an encrypted overlay such as WireGuard or Tailscale. The internal
265listener also has an unauthenticated operator API, so do not show it to the
266public internet. See the [security](security.md) page for the complete boundary.
267
268## Shut down and roll out a node
269
270celld shuts a node down gracefully on SIGTERM or SIGINT, and these are the
271signals `systemctl stop`, `docker stop`, and a Kubernetes pod delete send.
272The `/__celld/health` path reports the node as unhealthy, so a load balancer
273stops routing to it. The node
274answers each new request with a 503 and closes the connection, so a client
275retries on a healthy node. The node then hands every resident cell to a
276peer by releasing its ownership, and it finishes the requests already in
277flight. A cell that serves a request is handed off when that request
278finishes. A peer takes over each released cell at once, so the node leaves
279without the takeover gap of an abrupt kill.
280
281The internal listener continues to accept `/state` requests during the drain.
282The response reports the `occupied`, `evicting`, and `restoring` values. The
283public health response identifies the active drain with a 503 status.
284
285The handoff runs at most `CELLD_RELEASES` releases at the same time, and
286the default is 128. This bound keeps a node with many cells from flooding
287the object store at shutdown. `CELLD_SHUTDOWN_DRAIN_MS` bounds the whole
288drain, and its default is 25000. The node exits when the handoff and the
289in-flight requests finish, or when this many milliseconds pass, whichever
290is first — an idle node exits immediately. You must set it below the stop
291grace of your orchestrator, such as systemd `TimeoutStopSec` or Kubernetes
292`terminationGracePeriod`, so the orchestrator does not send SIGKILL.
293
294To roll out a new version, use the rolling update of your orchestrator:
295stop each node with SIGTERM, wait for its replacement to report healthy,
296then move to the next node. celld has no rollout command, because the
297health signal lets the orchestrator pace the roll.
298
299The upgrade from v0.1.0 to v0.2.0 must not be a rolling update. Stop
300every v0.1.0 node, then start the v0.2.0 nodes. Two changes require
301this: v0.2.0 nodes advertise the internal listener, so ownership
302records that v0.1.0 nodes wrote name an address that v0.1.0 peers can
303not follow to a v0.2.0 node; and v0.2.0 compacts replicated data into
304block objects that a v0.1.0 reader can not restore. A fleet must not
305mix the two versions.
306
307The upgrade from v0.2.1 to v0.3.0 can use a rolling update. v0.3.0 changes
308the default durability from `bucket` to `fleet`. A single node keeps the
309v0.2.1 behavior, and a fleet of two or more nodes activates fleet replication
310automatically.
311
312Stage the v0.3.0 binary on every node, and restart one node at a time. Wait for
313the replacement to report healthy before you restart the next node. A mixed
314fleet stays safe, but a v0.3.0 node cannot replicate to a v0.2.x peer. The
315v0.3.0 node therefore acknowledges writes through the bucket and retries until
316the peer runs v0.3.0.
317
318Do not start a v0.2.x binary after that node runs v0.3.0 unless the shutdown log
319contains `node-log close: sealed epoch`. A graceful stop attempts this seal.
320A stop under load can leave the record open. A v0.2.x binary cannot read writes
321that wait in the replicated log or bundle objects, so this downgrade can lose
322acknowledged writes.
323
324The internal listener also provides an alpha operator API. `/state` reports the
325node state, and `POST /shutdown` starts the same graceful handoff. The
326`POST /shutdown?handoff=preserve` request prepares a clean same-node reload and
327keeps the ownership records. A release can change this API, so keep the
328operator tooling and the celld release together.
329
330## Diagnose a fleet
331
332`celld diagnose` reads the node leases in the bucket. Then it sends a
333probe to each live peer. It does not get a lease. It does not change
334ownership.
335
336```sh
337celld diagnose \
338 --bucket "$CELLD_BUCKET" \
339 --endpoint "$S3_ENDPOINT" \
340 --region "$AWS_REGION"
341```
342
343To probe only some nodes, use `--peer NODE_ID` one or more times. The
344report identifies expired records, unsafe or incorrect advertised
345addresses, peers that it cannot reach, authentication failures, and
346protocol versions that do not agree.
347
348Each node line also shows `restoring`. This value counts each cold route that
349holds an activation permit or waits for one. A capacity waiter already holds
350a permit, so the value counts each cold route once. During a rolling update,
351you must wait for every node to report `restoring=0` before you restart the
352next node. Therefore, one restart's cold work finishes before the next restart
353removes more warm capacity.
354
355## Environment variables
356
357For the full list, run `celld -h`. This table shows the primary settings:
358
359| variable | purpose |
360| --- | --- |
361| `CELLD_BUCKET` | The fleet bucket, and an optional key prefix. The same as `--bucket` |
362| `S3_ENDPOINT` | The S3-compatible endpoint. The same as `--endpoint` |
363| `AWS_REGION`, `AWS_DEFAULT_REGION` | The storage region |
364| `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN` | Explicit AWS credentials. The standard AWS credential chain is also available |
365| `GOOGLE_APPLICATION_CREDENTIALS`, `GOOGLE_SERVICE_ACCOUNT_KEY` | Google credentials for a `gs://` bucket. Application Default Credentials are also available |
366| `AZURE_STORAGE_ACCOUNT_NAME` | The storage account for an `az://` bucket. The bucket NAME is the container |
367| `AZURE_STORAGE_ACCOUNT_KEY` | The storage account key. Do not combine it with an identity selector |
368| `AZURE_AUTHORITY_HOST`, `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_FEDERATED_TOKEN_FILE` | The standard AKS workload identity environment for an `az://` bucket. The authority must be the public Azure host |
369| `AZURE_STORAGE_USE_EMULATOR` | Set to `true` to develop against Azurite. celld does not qualify Azurite for a production fleet |
370| `CELLD_ADDR` | The public Worker listener. The same as `--listen` |
371| `CELLD_INTERNAL_ADDR` | The peer and operator listener. The same as `--internal-listen` |
372| `CELLD_ADVERTISE` | The internal address that peers can reach. The same as `--advertise` |
373| `CELLD_UNSAFE_PUBLIC_ADVERTISE` | Set to `1` to permit a literal public IP in `CELLD_ADVERTISE`. This setting does not resolve a DNS name or restrict the internal listener |
374| `CELLD_NODE` | An explicit node-session ID |
375| `CELLD_WATCH` | The local work directory for SQLite and replication |
376| `CELLD_ESBUILD` | The path of the esbuild executable |
377| `CELLD_ACTIVATIONS` | The limit for concurrent cold-cell activations (default: the available CPU count or 128, whichever is smaller) |
378| `CELLD_OPERATION_DEADLINE_MS` | The deadline for a non-restore operation (default: 15000) |
379| `CELLD_WORKER_LOADER` | Bind a Worker Loader (Code Mode) at this `env` name. A Worker can then start isolates at runtime. Off unless set (experimental) |
380| `CELLD_MAX_LOADED_WORKERS` | The limit for concurrent loaded workers (default: 256) |
381| `CELLD_MAX_RESIDENT_CELLS` | The hard limit for resident cells, enforced at admission |
382| `CELLD_MAX_RSS_MB` | The memory threshold for pressure shedding, applied to the memory that the cells hold (default: 80% of the available memory; 0 disables the threshold and the absolute cap) |
383| `CELLD_OUTPUT_GATE` | The default is `1`, so celld proves each write durable before it acknowledges the write. Set `0` to remove the replication wait and accept possible loss of an acknowledged write |
384| `CELLD_LTX_COMPACTION` | The default is `1`: celld creates additive L1 objects, and a takeover reads tens of objects instead of thousands. Set `0` on every node of a mixed fleet until all nodes can read v0.5.2 block objects, because an old reader cannot take over a cell after its first L1 publication |
385| `CELLD_LTX_COMPACTION_MIN_TXIDS` | The durable TXID distance that queues an L1 attempt (default: 256) |
386| `CELLD_LTX_COMPACTIONS` | The node-wide limit for concurrent L1 attempts (default: 2) |
387| `CELLD_VAR_*`, `CELLD_VARS_FILE` | Worker variable overrides |
388| `RUST_LOG` | The runtime log filter |
389
390The help output also shows the advanced tuning switches and their
391defaults.
392
393An unset variable selects its documented default. A Boolean variable accepts
394only `0` or `1`. celld exits during startup when a supplied value is invalid.