| 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 database of a facet replicates beside |
| 8 | the database of its root object, and the facet shares the ownership of the root |
| 9 | object. Read the |
| 10 | [Cloudflare Durable Object Facets documentation](https://developers.cloudflare.com/dynamic-workers/usage/durable-object-facets/) |
| 11 | for the standard API behavior. |
| 12 | |
| 13 | ## Example |
| 14 | |
| 15 | The [facets example](../../examples/facets) creates a facet from a class that a |
| 16 | Worker Loader provides. |
| 17 | |
| 18 | <!-- celld-example: facets --> |
| 19 | |
| 20 | ## A facet is a part of its root object |
| 21 | |
| 22 | A facet is not a cell. celld keeps the SQLite database of a facet in a file of |
| 23 | its own, and it replicates that file as a stream of its own under the bucket |
| 24 | prefix of the root cell. A facet owns no ownership record and no fencing epoch. |
| 25 | It inherits both from the root cell, and its stream uses the epoch of the root |
| 26 | cell. The |
| 27 | [Durable Objects page](durable-objects.md#ownership-and-the-single-threaded-model) |
| 28 | describes that record and that epoch. |
| 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, so a move proves |
| 35 | the stream of each facet before the new owner opens the root cell. |
| 36 | |
| 37 | The storage of a facet is independent of the storage of its root object, as in |
| 38 | workerd. A write of a facet commits in the database of the facet, so a rollback |
| 39 | of a root storage transaction does not undo a facet call inside it. celld holds |
| 40 | an outbound effect of a facet, and the reply of a facet call, until the stream |
| 41 | of the facet proves the writes of that call. The effect also passes the output |
| 42 | gate of the root cell, because it can reveal what the root object showed the |
| 43 | facet. |
| 44 | |
| 45 |  |
| 46 | |
| 47 | ## Starting and addressing a facet |
| 48 | |
| 49 | `ctx.facets.get(name, callback)` returns a stub for the facet with that name, |
| 50 | and celld runs the callback only when the facet is not already running. The |
| 51 | callback takes no argument and returns a `class` and an optional `id`. `class` |
| 52 | is a `DurableObjectClass`, and two sources supply one. The first source is |
| 53 | `worker.getDurableObjectClass("App")` on a Worker Loader binding. The second is |
| 54 | `ctx.exports.App` for a `DurableObject` class that the Worker exports without a |
| 55 | storage migration, and that facet runs in the isolate of its root. |
| 56 | |
| 57 | `id` sets the value of `ctx.id` in the facet. A `DurableObjectId` keeps its |
| 58 | name, so an ID from `idFromName("child")` exposes `ctx.id.name === "child"`. |
| 59 | A string ID stays a string, and an omitted ID inherits the parent ID and its |
| 60 | name. An unmigrated class handle has no `idFromName()` method, so use a |
| 61 | Durable Object namespace to create a named ID. |
| 62 | |
| 63 | `ctx.exports.App({ props })` creates a class handle with startup properties. |
| 64 | The call copies the properties, so later changes to the original object do |
| 65 | not change `ctx.props` in the facet. The properties must be an object that |
| 66 | supports structured cloning. A loopback class without properties supplies an |
| 67 | empty object, and the facet constructor can read the properties. |
| 68 | |
| 69 | The name alone selects the database, so a later call that keeps the name and |
| 70 | changes the `id` still reaches the stored data. A name has a limit of 256 bytes. |
| 71 | A facet can start a facet of its own, to a total depth of 4 that counts the root |
| 72 | object. `ctx.facets.abort(name, reason)` stops a running facet and keeps its |
| 73 | database, and a call on the stopped stub then throws the given reason. |
| 74 | `ctx.facets.delete(name)` stops the facet and removes its database and the |
| 75 | database of every facet below it. The stub answers `fetch()` and the RPC methods |
| 76 | of the class. The stub itself is not a promise, and a pipelined property path is |
| 77 | unavailable, so a call must name one method. |
| 78 | |
| 79 | ## Choose a facet or a second Durable Object |
| 80 | |
| 81 | Use a facet when the child state belongs to the parent. The supervisor sits on |
| 82 | the path of every call, so it can count a call, apply a quota, and refuse a |
| 83 | request before the child code runs. The facet stays on the owner node of the |
| 84 | root cell, therefore a call from the supervisor crosses no node boundary and |
| 85 | needs no second ownership record. A facet and its root object do not commit |
| 86 | atomically, because each database proves its own writes. An application that |
| 87 | needs one atomic commit must keep that state in one database. |
| 88 | |
| 89 | Use a separate Durable Object when the children must scale apart. Each object |
| 90 | then takes its own id, its own ownership record, and its own owner node, so the |
| 91 | fleet can place two of them on two machines and the stop of one node affects one |
| 92 | of them. A caller can also address such an object directly with |
| 93 | [`idFromName()`](durable-objects.md#identity-and-addressing). No name reaches a |
| 94 | facet from outside its root object, and a facet that must serve external traffic |
| 95 | therefore needs a supervisor that forwards the request. |
| 96 | |
| 97 | ## Differences from Cloudflare |
| 98 | |
| 99 | - A Durable Object binding cannot supply a facet class. A class with a storage |
| 100 | migration cannot supply one either, because `ctx.exports` holds that class as |
| 101 | a namespace. |
| 102 | - Each facet has a separate SQLite database, which celld replicates as a |
| 103 | separate stream under the bucket prefix of the root Durable Object. |
| 104 | - A facet cannot set an alarm. `storage.setAlarm()` throws inside a facet, so |
| 105 | the root object must hold the schedule. |
| 106 | - A facet stub is not awaitable, and a pipelined property path is unavailable. |
| 107 | - The `clone()` method is unavailable. |
| 108 | |
| 109 | The [Cloudflare compatibility](../cloudflare-compat.md#services) |
| 110 | page lists the runtime APIs and the unsupported services. |