| 1 | # Durable Objects / Cells |
| 2 | |
| 3 | A Durable Object is a single-threaded actor with durable storage, and celld |
| 4 | calls each running Durable Object a cell. An application uses one when a piece |
| 5 | of state needs a single consistent owner, such as a chat room, a game match, a |
| 6 | shopping cart, an agent session, or a rate limiter. The state lives in a SQLite |
| 7 | database that belongs to that object alone, on the node that serves it, and |
| 8 | celld replicates the database to the fleet. Read the |
| 9 | [Cloudflare Durable Objects documentation](https://developers.cloudflare.com/durable-objects/) |
| 10 | for the standard API behavior. |
| 11 | |
| 12 | ## Example |
| 13 | |
| 14 | The [counter example](../../examples/counter) keeps a counter in the key-value |
| 15 | storage of a Durable Object, and it uses `idFromName()` to give each name a |
| 16 | separate counter. |
| 17 | |
| 18 | <!-- celld-example: counter --> |
| 19 | |
| 20 | ## Identity and addressing |
| 21 | |
| 22 | A `durable_objects` binding exposes one class as a namespace, and an |
| 23 | application reaches an object in two steps. `env.COUNTER.idFromName("room-7")` |
| 24 | returns a `DurableObjectId`, and `env.COUNTER.get(id)` returns a |
| 25 | `DurableObjectStub`. `getByName("room-7")` performs both steps together. The |
| 26 | stub is only a handle, so celld creates the object at the first call and not at |
| 27 | `get()`. Read the |
| 28 | [Durable Object ID documentation](https://developers.cloudflare.com/durable-objects/api/id/) |
| 29 | for the complete method list. |
| 30 | |
| 31 | `idFromName()` is deterministic. celld derives the id with HMAC-SHA-256 over |
| 32 | the name, under a key that belongs to the namespace, so one name always gives |
| 33 | the same 64-digit hexadecimal id. That id always reaches the same object, and |
| 34 | two Workers on two different nodes that use one name therefore reach one |
| 35 | object. The name also survives in `ctx.id.name` when it is 1024 UTF-8 bytes or |
| 36 | less, which is the Cloudflare rule, and a longer name still routes to the |
| 37 | correct object. |
| 38 | |
| 39 | `newUniqueId()` instead draws random bytes, so its object carries no name and |
| 40 | no later caller can address it again. Keep the `toString()` form of such an id |
| 41 | if the application must reach the object a second time. `idFromString()` parses |
| 42 | that hexadecimal form and verifies the HMAC, therefore celld refuses an id that |
| 43 | belongs to a different namespace instead of creating an unrelated object. |
| 44 | |
| 45 | celld builds the key of a namespace from the script name and the class name, so |
| 46 | a rename of the Worker script changes every id that the namespace derives. The |
| 47 | objects of the old name keep their storage under the old ids, and the renamed |
| 48 | script reaches new and empty objects. Keep the script name stable, or migrate |
| 49 | the data before the rename. |
| 50 | |
| 51 | celld implements no jurisdiction, because a celld fleet has only the machines |
| 52 | that you run. `newUniqueId({ jurisdiction })` and `namespace.jurisdiction()` |
| 53 | throw an error, so an application learns about the gap at the call site. |
| 54 | |
| 55 | ## Ownership and the single-threaded model |
| 56 | |
| 57 | Cloudflare creates an object near the first request that uses it, and |
| 58 | Cloudflare can migrate the object afterwards. celld makes no placement, |
| 59 | migration, or jurisdiction promise, and it makes one narrower promise instead: |
| 60 | exactly one node serves a cell at a time. |
| 61 | |
| 62 | A node claims a cell by writing an ownership record into the fleet bucket with |
| 63 | a conditional write. The object store accepts only one of two competing |
| 64 | writers, so two nodes cannot own one cell. Every activation advances a fencing |
| 65 | epoch, and the epoch appears in the storage prefix, therefore a node that lost |
| 66 | the cell writes only into a superseded prefix. The |
| 67 | [guarantees](../guarantees.md) page gives the full mechanism. |
| 68 | |
| 69 | A Worker holds its stub on whichever node served the request. celld resolves |
| 70 | the owner of the cell and forwards the call to that node, so the object code |
| 71 | runs on the owner and nowhere else. Inside the cell, one event runs at a time: |
| 72 | a `fetch`, an RPC method, an `alarm()`, or a WebSocket message. celld also |
| 73 | holds a response until a durability proof covers every write that the response |
| 74 | can reveal, which is the behavior Cloudflare calls the |
| 75 | [output gate](https://developers.cloudflare.com/durable-objects/best-practices/rules-of-durable-objects/). |
| 76 | An application therefore does not have to `await` a `put()`, because a client |
| 77 | cannot receive the response before the write survives a failure. The same gate |
| 78 | holds a WebSocket frame, and it holds each frame only for its own proof. |
| 79 | Therefore a `webSocketMessage()` handler that sends a frame and then awaits |
| 80 | delivers that frame while it still runs, and an application can stream an |
| 81 | answer through one handler. |
| 82 | |
| 83 |  |
| 84 | |
| 85 | A cell keeps no in-memory state across an eviction. celld removes an idle cell |
| 86 | from memory, and the cell then hibernates on its node or becomes inactive in |
| 87 | the bucket, so the constructor runs again at the next event. Ownership can move |
| 88 | as well, because a node can stop, a node can drain, and idle rebalancing can |
| 89 | move a hibernated cell to another node. A reader must therefore treat only |
| 90 | durable storage as persistent. A hibernatable WebSocket survives a hibernation |
| 91 | on the same node, and it closes when the cell moves to a new owner, so a client |
| 92 | must reconnect. Cloudflare gives the same rule for |
| 93 | [in-memory state](https://developers.cloudflare.com/durable-objects/reference/in-memory-state/). |
| 94 | |
| 95 | ## Durable storage and alarms |
| 96 | |
| 97 | `ctx.storage` gives each object its own SQLite database. A class that a |
| 98 | `new_sqlite_classes` migration declares can use the key-value methods `get()`, |
| 99 | `put()`, `delete()`, `list()`, and `deleteAll()`, and it can also run SQL |
| 100 | through `ctx.storage.sql.exec()`. `transaction()` and `transactionSync()` group |
| 101 | several writes, and `storage.sync()` waits for the durability of the earlier |
| 102 | committed writes. The `transactionSync()` callback receives no argument, as in |
| 103 | workerd. Therefore, it writes through `ctx.storage`, and a throw rolls the |
| 104 | transaction back. A callback can start another transaction, through the |
| 105 | transaction object or through `ctx.storage` itself, and celld nests the new |
| 106 | transaction inside the open one. A nested transaction that fails discards only |
| 107 | its own writes, therefore the enclosing transaction can continue and commit. |
| 108 | Read the |
| 109 | [storage documentation](https://developers.cloudflare.com/durable-objects/best-practices/access-durable-objects-storage/) |
| 110 | for the method signatures. |
| 111 | |
| 112 | The durability of that database comes from the fleet, not from a managed |
| 113 | service. celld captures each write as an LTX segment and replicates the segment |
| 114 | to `cells/<cell>/ltx/e<epoch>/` in the fleet bucket. A fleet of two or more |
| 115 | nodes is faster, because the owner also sends each write to one or two follower |
| 116 | nodes and answers as soon as a follower holds the write on its own disk. The |
| 117 | bucket upload follows afterwards. A single node has no follower, so every write |
| 118 | on it waits for the object store instead. |
| 119 | |
| 120 | `storage.setAlarm()` schedules one alarm per object, `getAlarm()` reads it, and |
| 121 | `deleteAlarm()` removes it. celld calls the `alarm(alarmInfo)` handler at the |
| 122 | scheduled time and passes `retryCount` and `isRetry`, as the |
| 123 | [Alarms API](https://developers.cloudflare.com/durable-objects/api/alarms/) |
| 124 | describes. celld does not answer a successful response until a durable wake |
| 125 | entry in the bucket covers the alarm, so a crash cannot lose a schedule that a |
| 126 | client already saw. A hibernated cell fires its alarm on the node that owns it. |
| 127 | One node in the fleet holds the waker role, and it wakes only a cell whose |
| 128 | owner node stopped, so the alarm work of the fleet does not queue on one |
| 129 | machine. |
| 130 | |
| 131 | ## Differences from Cloudflare |
| 132 | |
| 133 | - celld makes no placement, migration, or jurisdiction promise. A cell runs on |
| 134 | a node of your fleet, and `newUniqueId({ jurisdiction })` and |
| 135 | `namespace.jurisdiction()` throw. |
| 136 | - The key of a namespace contains the script name, so a rename of the script |
| 137 | changes every id that `idFromName()` derives. |
| 138 | - A `migrations` entry accepts `tag` and `new_sqlite_classes` only. A class |
| 139 | rename, a class delete, and a class transfer stop the deployment. |
| 140 | - A Durable Object event keeps pending I/O active after the handler returns, so |
| 141 | a timer or a subrequest does not require `ctx.waitUntil()`. |
| 142 | - The imported `waitUntil()` and `ctx.waitUntil()` can add work while previously |
| 143 | registered background work remains active. |
| 144 | - An RPC stub cannot cross an isolate boundary. See |
| 145 | [RPC](../cloudflare-compat.md#rpc). |
| 146 | - An outbound WebSocket does not continue after the object moves to another |
| 147 | node. |
| 148 | - `SqlStorage.Cursor.toArray()` gives a celld-specific error near the V8 heap |
| 149 | limit. |
| 150 | - `storage.sync()` waits for the object store or the fleet ensemble to hold all |
| 151 | earlier committed writes. The operation uses the shorter of the 10-second |
| 152 | `CELLD_LTX_DURABILITY_TIMEOUT_SECS` default and the 15-second |
| 153 | `CELLD_OPERATION_DEADLINE_MS` default. |
| 154 | - `storage.sync()` rejects during an open transaction and after an object abort. |
| 155 | Without an object store, it resolves after the local commit. |
| 156 | - A transaction and `blockConcurrencyWhile()` have a 30-second limit. A timeout |
| 157 | resets the object and rolls back an open transaction. |
| 158 | - A failed handler still waits for the durability of any value or write that it |
| 159 | can expose. A celld-generated failure that exposes no object value returns |
| 160 | immediately. |
| 161 | - Outside an explicit transaction, a SQL write cursor must finish before a |
| 162 | response, an outbound effect, or `storage.sync()`. An unfinished `RETURNING` |
| 163 | cursor holds uncommitted writes, so celld rejects that output with an error. |
| 164 | A read cursor can remain open. |
| 165 | |
| 166 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page |
| 167 | lists the runtime APIs and the unsupported services. |