Diffinery

homeflare/celldpublic

celld/docs/guarantees.md
412 lines · 21.2 KBRaw History
1# What celld guarantees
2
3celld makes two promises about your data. Exactly one node serves a cell
4at a time, so two machines never write the same database. And celld does
5not answer a write until that write survives a failure, so nothing you
6were told succeeded is lost.
7
8This page explains how celld keeps both promises. Fencing prevents a
9slow, paused, or disconnected node's writes from damaging the current
10owner's data after it loses a cell.
11Each ownership epoch has a separate storage prefix, so a stale node's
12writes cannot overwrite the current owner's data.
13Both guarantees require a bucket with working conditional writes and
14ranged reads, and a supervisor that restarts the process.
15
16## What the bucket must provide
17
18celld needs four properties from the object store:
19
20- A conditional create: the write must fail when the object exists.
21- A conditional overwrite: the write must fail when the object changed
22 after the read.
23- Read-after-write consistency: a read after a successful write must
24 return that write.
25- Ranged reads: a read must return the requested byte range and the bytes
26 from that range.
27
28The qualified stores are Amazon S3, Cloudflare R2, Tigris, Google Cloud
29Storage, and Azure Blob Storage. celld's release tests run against R2,
30and the S3 path uses the same client and the same headers.
31
32Backblaze B2, Hetzner Object Storage, and DigitalOcean Spaces do not
33implement the required conditional writes. celld is not correct on such
34a store: two nodes can then own one cell. A store can also accept the
35conditional headers and ignore the condition, and that store fails late
36and silently, so run the storage test below.
37
38MinIO (the community edition) implements the conditional writes and
39passes the storage test, but celld has not qualified it for production.
40One release is broken: RELEASE.2025-09-06T17-38-46Z answers the
41conditional create of an absent object with `NoSuchKey`, so the first
42deploy fails (denoland/celld#162). Use RELEASE.2025-09-07T16-13-09Z or
43later.
44
45The request dialect differs per provider. An S3-compatible bucket gets
46the `If-None-Match: *` and `If-Match` headers, and the condition
47compares the etag. A `gs://` bucket selects the Cloud Storage XML API
48with the `x-goog-if-generation-match` precondition and OAuth
49credentials, because Cloud Storage does not apply `If-Match` to a PUT.
50An `az://` bucket (the NAME is the container) uses the same `If-`
51headers, which Put Blob applies. The adapter treats `AlreadyExists` and
52`Precondition` (HTTP 412 on Azure) as clean conditional-write
53rejections and keeps every other error ambiguous, because an ambiguous
54write can have changed the object.
55
56Azure was qualified on 2026-08-18 under an account key, a VM managed
57identity, and an AKS workload identity, single-node. A managed identity
58on Azure App Service or Azure Container Apps does not work; see
59[limitations](limitations.md).
60
61## The storage test
62
63No store publishes these properties, so celld asks the store directly.
64The command `celld diagnose` sends four conditional writes to your
65bucket and reports the result:
66
67```
68ok bucket conditional write (create, reject-create, update, reject-stale)
69```
70
71Two of the four writes must fail. A store that accepts either one
72cannot fence a cell, so celld names the store as the fault and the
73command exits with an error. Each node also runs these writes before it
74serves. The node then requests part of a second object and verifies the
75returned range and bytes.
76
77The startup test uses new objects for each attempt. It makes at most
78three attempts when an operation fails without a clear cause. The node
79starts with a warning after all three attempts fail because a temporary
80outage can end after startup. The node stops immediately when a required
81conditional write or ranged read is unsupported. It also stops when the
82store ignores a condition or returns a wrong range or wrong bytes. A node
83cannot disable this startup test. Use `celld diagnose --read-only` with a
84credential that cannot write.
85
86The diagnose test writes and deletes one small object under `probe/`.
87The startup test uses a second object for the ranged read. A process
88that stops mid-test can leave an object behind; it is small, and celld
89never reads it.
90
91celld reserves `probe/`, `cells/`, `nodes/`, `node-cells/`, `fleet/`,
92`deploy/`, `deploy-blobs/`, `log/`, `wake/`, and `telemetry/`. celld deletes
93objects under some of these prefixes, so an application must not write under
94any of them.
95
96celld requires ranged reads for stored cell data. The startup test stops
97a node when the store ignores the `Range` header or returns incorrect
98bytes.
99
100## The supervisor
101
102You must run celld under a supervisor that restarts the process, such
103as systemd, Docker with a restart policy, or Kubernetes. A node fences
104itself when it loses its lease (the mechanism is below), and a fenced
105process exits; without a restart, the fleet loses that capacity until
106an operator intervenes.
107
108The supervisor must restart without an attempt limit, and it must wait
109at least one lease lifetime between attempts. A node that cannot
110acquire a lease at startup retries and does not exit, so a repeated
111fence needs a node that acquires a lease and then loses it, and the
112wait keeps that cycle slow enough to observe.
113
114## The mechanism
115
116The short version: the ownership records use conditional writes, so two
117nodes cannot acquire one cell. The replicated data carries its fencing
118epoch in the object key, so a node that lost ownership writes only into
119a superseded prefix. And before celld acknowledges a write, it proves
120the write durable and confirms that it still owns the cell.
121
122### The ownership record
123
124Each cell has one ownership record in the bucket. The record names the
125owner node's session and carries a fencing epoch. A node acquires a
126cell with a conditional create when no record exists, or a compare-and-swap
127on an existing record. The bucket accepts only one competing write, so two
128nodes cannot acquire the same cell.
129
130Every activation advances the epoch, a takeover and a local wake alike.
131Each owner therefore replicates under a fresh epoch, and an epoch never
132has two writers.
133
134### The epoch prefix
135
136The replicator copies each cell's SQLite data to the bucket under
137`cells/<cell>/ltx/e<epoch>/`, with plain unconditional PUTs. The epoch
138in the key is the fence: a node that lost ownership can keep writing,
139but its writes land in a superseded prefix, and a restore selects the
140current lineage. (The tiering path can first combine segments from many
141cells into a node bundle, and it drains each segment into its per-cell
142prefix later.)
143
144The compactor reads retained bundles when a cell's segments leave the
145in-memory index. A failed compaction retries after 30 seconds, and repeated
146failures increase the delay to a maximum of 300 seconds. The bundle collector
147advances through retained objects and deletes a bundle only when the per-cell
148prefixes cover every segment in that bundle.
149
150The prefix protects the new owner's data from stale writes. The next
151two sections protect the durability promise.
152
153### The acknowledgement rule (RPO=0)
154
155A gate holds each write response until a durability proof covers the
156write. A read-only response waits in the same way when the object has
157committed a write that no proof covers yet. An error answer waits under
158the same rule, because the message of a handler that throws can carry a
159value that the handler read. An R2 mutation waits for the source object's write
160proof. Therefore, the mutation cannot change the application bucket before the
161source write is durable. A raw TCP connection, write, TLS upgrade, or shutdown
162waits for the same proof. A response body that streams gets the same rule for
163each chunk, so a chunk that an object produces after the response head waits
164for a proof of the writes it can reveal. A client therefore cannot act on a
165value that a crash can still lose. After a
166bucket proof, celld reads the ownership record once and acknowledges
167only if the record still names this node at this epoch. A partitioned
168node can commit locally and replicate into its superseded prefix, but
169the ownership read then shows the new owner, so celld does not
170acknowledge the write. The check reads the record instead of comparing a
171clock, so a paused process or a skewed clock cannot pass it.
172
173A fleet proof does not require this read. The owner sends each write to
174one or two other nodes, which hold a copy of its recent writes; those
175nodes are its followers, and the set of them is the ensemble. Every
176follower must fsync the write, and a takeover seals the prior node-log
177session before it restores, so the stale owner cannot complete another
178fleet proof.
179
180An unfinished SQL write cursor can return rows before SQLite commits the
181write. Outside an explicit transaction, the application must consume those
182rows before output or `storage.sync()`, so the gate can cover the commit.
183celld rejects that output while the write cursor remains unfinished.
184
185### The ensemble needs two nodes
186
187A node picks its followers from the other nodes in the fleet, so it never
188counts itself. One follower is enough, therefore a fleet needs two
189running celld nodes before any node can complete a fleet proof.
190`CELLD_DURABILITY=fleet` is the default, so a fleet of one node requests the
191fleet posture and does not get it.
192
193A node recruits up to two followers, so a fleet of three or more nodes holds
194three copies of an acknowledged write. The ensemble keeps acknowledging while
195one follower remains, therefore a fleet does not fall back to the bucket each
196time it loses a follower.
197
198A node without an ensemble stays correct. It acknowledges each write on a
199bucket proof instead, so celld still does not acknowledge a write before a
200durability proof covers it. The cost is latency: the write waits for the object
201store, and an object store round trip is much slower than a follower fsync.
202
203### The takeover recovery gate
204
205The default fleet mode can acknowledge a write once the node-log
206ensemble stores it; the bucket upload can complete later. Each process
207session therefore creates a conditional node-log record before its
208first fleet-durable acknowledgement.
209
210A cold activation checks the prior owner's log records before it reads
211the bucket. An absent record proves that the session never acknowledged
212past the bucket, and a sealed record proves that recovery completed. An
213open or recovering record makes the activation run recovery: it fences
214the record with a compare-and-swap, seals the reachable followers,
215uploads their retained segments and bundles into the per-cell prefixes,
216and then marks the record sealed. The activation cannot restore until
217this sequence completes.
218
219A restarting node serves authenticated follower seal and tail requests before
220it completes its own predecessor recovery. Therefore, nodes that restart
221together can recover acknowledged writes from their surviving follower disks.
222The node accepts application requests and new follower appends only after
223startup completes.
224
225A large dead node can hold this recovery open for minutes. The recovery
226reads the retained bundles in windows of at most 512 MiB. It uploads the
227rows of one window and releases them before it reads the next window, so
228the memory of the recovery does not grow with the size of the session. A
229cell that has rows in several windows receives one object for each window.
230A recovery attempt that fails or times out does not fail the waiting
231requests.
232The cell waits, and it retries the activation with a backoff
233(`CELLD_RECOVERY_RETRY_MS`, default 1000). The requests fail with a
234resolve error only after the retry budget is spent
235(`CELLD_RECOVERY_RETRIES`, default 240).
236
237### Epoch-chain restore
238
239A restore composes the epoch prefixes that contain LTX data into one
240chain. It starts at the newest prefix and walks down. An epoch that
241opened with a whole-database snapshot starts the chain at transaction
242one. An epoch that paged in continues its predecessor: its prefix holds a
243marker object at the successor of the cut it paged from, and then its own
244deltas, so the chain links the predecessor up to that cut and the epoch
245from the cut onward. A link must end exactly at its successor's cut, so an
246epoch that does not continue the chain is not part of it. celld no longer
247writes an epoch seal object, and a legacy `e<epoch>.seal.json` object does
248not limit the chain.
249
250A fenced node can append an unacknowledged tail to an older prefix. When
251a successor opened with a whole-database snapshot, a later restore reads
252the full chain and can expose that tail. This does not violate the
253contract, because a failed or absent acknowledgement does not prove that
254the write is absent, and a node-log recovery or a bundle drain can add an
255acknowledged tail after an earlier restore. When a successor paged in, the
256chain clips the older prefix at the successor's cut. This is safe because
257the takeover recovery gate seals the prior node-log session before the
258successor restores, so no write past the cut can be acknowledged.
259
260A paged cell's restore reads no chain up front. The cell opens over a
261sparse local file, and each page the cell touches is read from the
262objects on first use. The node then fills the rest of the file in the
263background, and a filled cell reads only its local file. A chain smaller
264than `CELLD_LTX_PAGED_MIN_MB` is downloaded whole, as before.
265
266### Self-fencing
267
268Each node holds a lease in the bucket. The lease carries an expiry, and
269the node renews it after one third of the lifetime (`CELLD_TTL_MS`,
270default 10000 ms). A renewal that does not reach the bucket does not
271fence the node, because the node retries while the published expiry has
272not passed.
273
274A node that cannot reach the bucket cannot renew and cannot replicate,
275so it must not own cells. When its published expiry passes, it fences
276itself: it stops each active cell, and it fails every request that it
277has not completed. A node also fences at once when its lease record is
278gone, or no longer matches the record that the node published, because
279the node can no longer prove that it holds the authority.
280
281The fence writes nothing to the bucket. Each peer already reads the
282lease as dead or replaced, so a peer can acquire the cells through the
283ownership records. And a request is safe even before the fence runs,
284because celld compares the current time against the published expiry
285each time it routes a request.
286
287An ingress checks a cached remote route against its observed lease deadline
288for each new request. At the deadline, the ingress reads ownership again,
289even if the previous owner keeps its connections open without a response.
290Recovery still needs the bucket and the required durable data.
291A draining ingress can resolve and forward to a live remote owner. It
292refuses new ownership of an unowned cell or a cell with an expired owner.
293
294A request already sent to the previous owner can remain incomplete. The
295ingress does not replay that request, because the handler can have committed
296a write before it stopped. The caller can cancel the request.
297
298A fenced node logs a line that starts with `SELF-FENCE:` and stops with
299the exit code 3. celld reports other internal failures with the same
300prefix and code, so the line names the cause. An expired lease uses the
301`node_lease_watchdog_fence` event. A missing record uses the
302`node_lease_record_missing_fence` event, and a record that no longer
303matches the node uses the `node_lease_record_mismatch_fence` event. The
304mismatch event does not name an author, because the node cannot prove
305which writer wrote that record. The fenced state is terminal:
306only a restart returns the node to the fleet, through the same
307cold-activation path that a peer failure uses.
308
309The fence line names the lease state, and it cannot name the store
310request that produced that state. A removed record, a replaced record
311and a failed read are three different operator problems, so celld can
312report each lease request on its own. Set
313`RUST_LOG=celld=info,store=debug` to turn this on. The node then logs a
314`node_lease_read` event and a `node_lease_write` event for each request
315against its own lease record. Each event carries an `outcome` field:
316`found`, `missing`, `applied`, `rejected` or `error`. An `error`
317outcome carries the store failure in an `error` field, and a `found`
318outcome carries the `generation` field of the record that came back.
319The target is off at every other filter, so a node in production pays
320nothing for it.
321
322The failure of a node is a normal input, not a recovery procedure; the
323[testing page](testing.md) shows the kill tests that exercise this
324path.
325
326## Alarm discovery and the wake format
327
328SQLite stores the alarm deadline, consumption, retry state, and installation
329identity. An alarm hint only causes celld to read SQLite. The hint does not
330authorize an alarm handler to run.
331
332Each committed alarm installation has a separate object under `wake/entries/`.
333The ownership epoch and a persistent SQLite sequence identify the installation.
334An alarm response waits for its publication PUT and the existing output proof.
335Updates within the same minute also require a new publication PUT.
336
337Cleanup uses conditional writes to publish a retirement record under
338`wake/retired/`. The record requires a durability proof, a current owner,
339and a confirmed replacement publication when an alarm remains armed.
340Cleanup deletes only older identities or a proven consumed identity.
341An old DELETE therefore cannot delete a later installation.
342This protocol requires no conditional DELETE operation from the bucket.
343After the proof, celld attempts to delete known obsolete publications directly.
344These deletions do not block an alarm response.
345
346Each cleanup pass lists at most 128 objects and processes at most eight cells
347concurrently. The next pass continues the listing, and the final page restarts
348the scan. A failed DELETE or a late PUT can require another complete scan.
349The default interval is 60 seconds. `CELLD_WAKER_TICK_MS` changes this interval
350and the due-scan interval. A large inventory can require many intervals, and
351an additional node can repeat the same reads and deletes.
352The collector requests a SQLite refresh for a remaining installation only when
353its minute is due. Future installations stay dormant until that minute.
354The timer still controls delivery, so the refresh cannot run the handler early.
355
356The `wake/format.json` object selects format 2. The `wake/waker.json` object
357holds the advisory lease for the fleet's waker role. A new node or deployment
358initializes an empty fleet or upgrades a stopped v0.4.1 fleet automatically.
359An unsupported format prevents startup. Existing application objects outside
360the reserved namespaces do not prevent initialization.
361The previous `wake-format.json`, `wake-v2/`, and `wake-retired-v2/` names
362also identify unsupported engine data and prevent fresh initialization.
363
364### Start a fleet with this format
365
366An empty fleet initializes this format automatically. An upgrade from v0.4.1
367uses the same bucket and preserves the existing node data directories.
368The upgrade requires a stopped fleet. Mixed versions cannot share a serving
369fleet because v0.4.1 cannot read the new alarm discovery entries.
370
3711. Stop application traffic and deployment writers. Stop every old node and
372 its supervisor, then wait for every node lease to expire.
3732. Back up the stopped fleet's bucket and node data. Keep the node names,
374 peer addresses, and data directories for the restart. A follower disk can
375 contain acknowledged writes that the bucket does not yet contain.
3763. Prevent the old binaries from restarting or writing to the bucket.
377 Revoke their credentials or remove their access through the deployment
378 system. The format marker cannot stop an old binary from writing.
3794. Start the new binary on every node with the same configuration, data, and
380 addresses. Wait for the fleet to become healthy before resuming traffic.
381
382Startup upgrades the format automatically. The nodes can start together and
383complete the same migration. A live node lease prevents the migration, so
384stop every old writer and wait for its lease to expire before the restart.
385The operator must prevent old writers from returning after the upgrade.
386
387The migration inventories stored cells and creates a discovery seed for each
388cell. It preserves the databases, node logs, ownership records, deployments,
389and application objects. Existing legacy wake entries remain unused.
390The inventory uses pages of at most 128 entries and includes cells whose
391old alarm discovery entry is missing.
392
393A discovery seed makes recovery read the cell's SQLite alarm state. Recovery
394creates the installation identity without changing the deadline or retry
395state. A seed does not authorize an alarm handler to run. The normal
396retirement proof removes the seed after durable recovery and replacement
397discovery, so an interrupted migration cannot silently discard an alarm.
398Recovery can load cells with future alarms while it processes these seeds.
399
400If a node stops during the migration, another starting node resumes it.
401The nodes cannot serve until the complete inventory succeeds. Later starts
402use the completed format without another migration. Alarms can run late
403during the outage.
404
405Do not roll back by starting an old binary against the upgraded fleet.
406Restore the complete stopped-fleet backup for a rollback. That restoration
407loses writes made after the backup, so preserve those writes separately.
408
409Normal restarts and ownership transfers within this format preserve the
410alarm history. Each restored writer takes a new ownership epoch and keeps
411the stored sequence and consumed state, so an old mutation cannot target
412a later installation.