Diffinery

homeflare/celldpublic

celld/docs/services/durable-objects.md
139 lines · 7.0 KBRaw History
1# Durable Objects / Cells
2
3A Durable Object is a single-threaded actor with its own SQLite database, and
4celld calls each running Durable Object a cell. celld replicates the database
5to the fleet. Read the
6[Cloudflare Durable Objects documentation](https://developers.cloudflare.com/durable-objects/)
7for the standard API.
8
9## Example
10
11The [counter example](../../examples/counter) keeps a counter in the key-value
12storage of a Durable Object, and it uses `idFromName()` to give each name a
13separate counter.
14
15<!-- celld-example: counter -->
16
17## API
18
19- `env.COUNTER.idFromName(name)` returns an id, and `env.COUNTER.get(id)`
20 returns a stub. `getByName(name)` performs both steps.
21- `newUniqueId()` and `idFromString()` create and parse a random id.
22- `ctx.storage.get()`, `put()`, `delete()`, `list()`, and `deleteAll()` are
23 the key-value methods.
24- `ctx.storage.sql.exec()` runs SQL on the database of the object.
25- `transaction()` and `transactionSync()` group several writes.
26- `storage.setAlarm()`, `getAlarm()`, and `deleteAlarm()` manage one alarm per
27 object, and celld calls the `alarm(alarmInfo)` handler.
28- `ctx.acceptWebSocket()` accepts a hibernatable WebSocket, and celld calls
29 `webSocketMessage()` for each frame.
30- `blockConcurrencyWhile()` closes the input gate.
31- `storage.sync()` waits for the durability of the earlier committed writes.
32
33## Identity and addressing
34
35celld creates the object at the first call on a stub, not at `get()`.
36
37`idFromName()` derives a 64-digit hexadecimal id with HMAC-SHA-256 over the
38name, under a key that celld builds from the script name and the class name.
39One name therefore reaches one object from every node. `ctx.id.name` holds the
40name when it is 1024 UTF-8 bytes or less, as on Cloudflare; a longer name still
41routes correctly.
42
43`newUniqueId()` draws random bytes, so keep its `toString()` form to reach the
44object again. `idFromString()` verifies the HMAC, so celld refuses an id from a
45different namespace.
46
47A rename of the Worker script therefore reaches new, empty objects, and the old
48objects keep their storage under the old ids.
49
50## Ownership and the single-threaded model
51
52Exactly one node serves a cell at a time. A node claims a cell with a
53conditional write of an ownership record to the fleet bucket. Each activation
54advances a fencing epoch that appears in the storage prefix, so a node that
55lost the cell writes only into a superseded prefix. The
56[guarantees](../guarantees.md) page gives the full mechanism. celld forwards
57each stub call to the owner node.
58
59Inside the cell, one synchronous turn runs at a time. An event that awaits can
60overlap another event unless `blockConcurrencyWhile()` closes the input gate.
61celld holds a response until a durability proof covers every write that the
62response can reveal, as the Cloudflare
63[output gate](https://developers.cloudflare.com/durable-objects/best-practices/rules-of-durable-objects/)
64does, so an application does not have to `await` a `put()`.
65
66The output gate holds each WebSocket frame only for its own proof, so a
67`webSocketMessage()` handler that sends and then awaits delivers that frame
68while it runs. On one socket, celld starts message handlers in arrival order
69but does not wait for one to finish before the next starts, so an incoming
70message can cancel work that an earlier handler awaits. A hibernatable socket
71delivers frames in send order across WebSocket handlers and RPC methods.
72
73A cell keeps no in-memory state across an eviction, so the constructor runs
74again at the next event. Ownership can also move when a node stops or drains,
75or when idle rebalancing moves a hibernated cell. A hibernatable WebSocket
76survives hibernation on the same node, and it closes when the cell moves, so
77the client must reconnect.
78
79## Durable storage and alarms
80
81A class that a `new_sqlite_classes` migration declares can use the key-value
82methods and `ctx.storage.sql.exec()`. The `transactionSync()` callback receives
83no argument, as in workerd, and a throw rolls it back. A transaction can start
84a nested transaction; a failed nested transaction discards only its own
85writes, and the enclosing transaction can still commit.
86
87The synchronous `ctx.storage.kv.list()` iterator reads one entry per step and
88does not block later writes. Each step resumes after the last returned key, so
89it can observe a change to an entry that it has not returned yet. A new call to
90`kv.list()` invalidates the previous iterator for that object.
91
92celld captures each write as an LTX segment and replicates it to
93`cells/<cell>/ltx/e<epoch>/` in the fleet bucket. In a fleet of two or more
94nodes, the owner answers when each of its one or two followers holds the write
95on disk, and the bucket upload follows. On a single node, every write waits for the object
96store.
97
98celld does not answer a successful `setAlarm()` until a durable wake entry in
99the bucket covers the alarm. A hibernated cell fires its alarm on its owner
100node. One node holds the waker role, and it wakes only a cell whose owner
101stopped.
102
103## Differences from Cloudflare
104
105- celld makes no placement, migration, or jurisdiction promise. A cell runs on
106 a node of your fleet, and `newUniqueId({ jurisdiction })` and
107 `namespace.jurisdiction()` throw.
108- The key of a namespace contains the script name, so a rename of the script
109 changes every id that `idFromName()` derives.
110- A `migrations` entry accepts `tag` and `new_sqlite_classes` only. A class
111 rename, a class delete, and a class transfer stop the deployment.
112- A Durable Object event keeps pending I/O active after the handler returns, so
113 a timer or a subrequest does not require `ctx.waitUntil()`.
114- The imported `waitUntil()` and `ctx.waitUntil()` can add work while previously
115 registered background work remains active.
116- An RPC stub cannot cross an isolate boundary. See
117 [RPC](../cloudflare-compat.md#rpc).
118- An outbound WebSocket does not continue after the object moves to another
119 node.
120- `SqlStorage.Cursor.toArray()` gives a celld-specific error near the V8 heap
121 limit.
122- `storage.sync()` waits for the object store or the fleet ensemble to hold all
123 earlier committed writes. The operation uses the shorter of the 10-second
124 `CELLD_LTX_DURABILITY_TIMEOUT_SECS` default and the 15-second
125 `CELLD_OPERATION_DEADLINE_MS` default.
126- `storage.sync()` rejects during an open transaction and after an object abort.
127 Without an object store, it resolves after the local commit.
128- A transaction and `blockConcurrencyWhile()` have a 30-second limit. A timeout
129 resets the object and rolls back an open transaction.
130- A failed handler still waits for the durability of any value or write that it
131 can expose. A celld-generated failure that exposes no object value returns
132 immediately.
133- Outside an explicit transaction, a SQL write cursor must finish before a
134 response, an outbound effect, or `storage.sync()`. An unfinished `RETURNING`
135 cursor holds uncommitted writes, so celld rejects that output with an error.
136 A read cursor can remain open.
137
138The [Cloudflare compatibility](../cloudflare-compat.md#services) page
139lists the runtime APIs and the unsupported services.