| 1 | # Durable Object Facets |
| 2 | |
| 3 | A facet is a child object inside a Durable Object. A supervisor class starts a |
| 4 | facet from a class that a [Worker Loader](dynamic-workers.md) provides, and the |
| 5 | supervisor and the facet then keep separate SQLite databases. An application |
| 6 | uses a facet to give generated or untrusted code durable storage without giving |
| 7 | that code a Durable Object namespace. The state of a facet lives inside the |
| 8 | storage of its root object, so celld replicates the two together. Read the |
| 9 | [Cloudflare Durable Object Facets documentation](https://developers.cloudflare.com/dynamic-workers/usage/durable-object-facets/) |
| 10 | for the standard API behavior. |
| 11 | |
| 12 | ## Example |
| 13 | |
| 14 | The [facets example](../../examples/facets) creates a facet from a class that a |
| 15 | Worker Loader provides. |
| 16 | |
| 17 | <!-- celld-example: facets --> |
| 18 | |
| 19 | ## A facet is a part of its root object |
| 20 | |
| 21 | A facet is not a cell. celld holds the SQLite database of a facet as an image in |
| 22 | a row of the root cell's own database, and it copies that image into the row |
| 23 | before an effect of the facet leaves the node. A facet therefore owns no |
| 24 | ownership record, no fencing epoch, and no replication stream. It inherits all |
| 25 | three from the root cell, so one upload covers the writes of the supervisor and |
| 26 | the writes of every facet together. The |
| 27 | [Durable Objects page](durable-objects.md#ownership-and-the-single-threaded-model) |
| 28 | describes that record, that epoch, and that stream. |
| 29 | |
| 30 | Containment also sets the lifecycle. A facet starts inside an event of the root |
| 31 | object, therefore the code of the facet runs on the node that owns the root |
| 32 | cell. celld releases every running facet when it releases the root cell, and the |
| 33 | startup callback runs again at the next call. An eviction, a reset, and a move |
| 34 | of ownership take the root object and its facets as one group. |
| 35 | |
| 36 | celld holds an outbound effect of a facet against the output gate of the root |
| 37 | cell, because that effect can reveal a write that only the root cell can prove. |
| 38 | A root storage transaction keeps an uncommitted facet image out of the root |
| 39 | database, so celld rejects an outbound effect while that transaction is open. |
| 40 | The error states that the root transaction has not committed the facet image. |
| 41 | celld applies the check to a read-only call as well, because an earlier call in |
| 42 | the same turn can leave an image behind and the read can reveal it. |
| 43 | |
| 44 |  |
| 45 | |
| 46 | ## Starting and addressing a facet |
| 47 | |
| 48 | `ctx.facets.get(name, callback)` returns a stub for the facet with that name, |
| 49 | and celld runs the callback only when the facet is not already running. The |
| 50 | callback takes no argument and returns a `class` and an optional `id`. `class` |
| 51 | must come from `worker.getDurableObjectClass("App")` on a Worker Loader binding, |
| 52 | because that method is the only source of a `DurableObjectClass`. `id` sets what |
| 53 | the facet reads at `ctx.id`, and the facet inherits the id of its root object |
| 54 | when the callback gives no `id`. |
| 55 | |
| 56 | The name alone selects the database, so a later call that keeps the name and |
| 57 | changes the `id` still reaches the stored data. A name has a limit of 256 bytes. |
| 58 | A facet can start a facet of its own, to a total depth of 4 that counts the root |
| 59 | object. `ctx.facets.abort(name, reason)` stops a running facet and keeps its |
| 60 | database, and a call on the stopped stub then throws the given reason. |
| 61 | `ctx.facets.delete(name)` stops the facet and removes its database and the |
| 62 | database of every facet below it. The stub answers `fetch()` and the RPC methods |
| 63 | of the class. The stub itself is not a promise, and a pipelined property path is |
| 64 | unavailable, so a call must name one method. |
| 65 | |
| 66 | ## Choose a facet or a second Durable Object |
| 67 | |
| 68 | Use a facet when the child state belongs to the parent. The supervisor sits on |
| 69 | the path of every call, so it can count a call, apply a quota, and refuse a |
| 70 | request before the child code runs. The facet stays on the owner node of the |
| 71 | root cell, therefore a call from the supervisor crosses no node boundary and |
| 72 | waits for no second output gate. A write of the facet also becomes durable with |
| 73 | the next upload of the root cell, and a facet write that a root storage |
| 74 | transaction encloses commits with that transaction or rolls back with it. Two |
| 75 | separate Durable Objects give no such atomicity, because each one proves its own |
| 76 | writes. |
| 77 | |
| 78 | A facet has a cost as well. celld copies the complete SQLite image of a facet |
| 79 | into the root database after a turn that changed it, so a large facet database |
| 80 | makes each changed turn more expensive. |
| 81 | |
| 82 | Use a separate Durable Object when the children must scale apart. Each object |
| 83 | then takes its own id, its own ownership record, and its own owner node, so the |
| 84 | fleet can place two of them on two machines and the stop of one node affects one |
| 85 | of them. A caller can also address such an object directly with |
| 86 | [`idFromName()`](durable-objects.md#identity-and-addressing). No name reaches a |
| 87 | facet from outside its root object, and a facet that must serve external traffic |
| 88 | therefore needs a supervisor that forwards the request. |
| 89 | |
| 90 | ## Differences from Cloudflare |
| 91 | |
| 92 | - Facets require Dynamic Workers and a class from a Worker Loader binding. A |
| 93 | class from `ctx.exports` or a Durable Object binding is unavailable. |
| 94 | - Each facet has a separate SQLite database, which celld replicates with the |
| 95 | root Durable Object. celld copies the complete image of that database into the |
| 96 | root database after a turn that changed it, so the cost of a changed turn |
| 97 | grows with the size of the facet database. |
| 98 | - celld rejects an outbound effect from a facet while a root storage transaction |
| 99 | holds an uncommitted facet image. |
| 100 | - An explicit transaction keeps its uncommitted writes in the facet. A rollback |
| 101 | discards those writes, and a commit makes them available for root replication. |
| 102 | - A facet cannot set an alarm. `storage.setAlarm()` throws inside a facet, so |
| 103 | the root object must hold the schedule. |
| 104 | - A facet stub is not awaitable, and a pipelined property path is unavailable. |
| 105 | - The `clone()` method is unavailable. |
| 106 | |
| 107 | The [Cloudflare compatibility](../cloudflare-compat.md#services) |
| 108 | page lists the runtime APIs and the unsupported services. |