Skip to content

Facets

A facet is a child object inside a Durable Object, with a SQLite database of its own. A root object starts a facet from a Durable Object class, so code that the root runs gets durable storage without its own namespace. Facets work in a cluster. Read the Cloudflare Durable Object Facets documentation for the API.

Example

The facets example starts one facet for each name in the request path. Each facet counts its requests in its own database. The facet class comes from ctx.exports.App, an exported class with startup properties.

index.jsjs
import { DurableObject } from "cloudflare:workers";

// A facet. Each one counts requests in its own SQLite database.
export class App extends DurableObject {
  fetch() {
    const n = (this.ctx.storage.kv.get("n") ?? 0) + 1;
    this.ctx.storage.kv.put("n", n);
    return new Response(`${this.ctx.props.name}: ${n}\n`);
  }
}

// The root object starts one facet for each name in the path.
export class Supervisor extends DurableObject {
  fetch(request) {
    const name = new URL(request.url).pathname.slice(1) || "a";
    const facet = this.ctx.facets.get(name, () => ({
      class: this.ctx.exports.App({ props: { name } }),
    }));
    return facet.fetch(request);
  }
}

export default {
  fetch(request, env, ctx) {
    return ctx.exports.Supervisor.getByName("root").fetch(request);
  },
};
wrangler.jsoncjsonc
{
  "name": "facets",
  "main": "index.js",
  "compatibility_date": "2026-01-01",
  "no_bundle": true,
  "exports": {
    "Supervisor": { "type": "durable-object", "storage": "sqlite" }
  }
}
npx workerd serve ./facets/wrangler.jsonc
curl http://localhost:8787/a
curl http://localhost:8787/b

Facet classes

The class that the ctx.facets.get() callback returns can come from ctx.exports or from a Worker Loader stub’s getDurableObjectClass(). ctx.exports supplies a class whether or not exports declares it. A durable_objects.bindings namespace in env cannot supply one.

A facet runs on the leader of its root object. In a cluster, the facet’s database is part of the root object’s log in the bucket, so it moves with the root. The databases are separate, so a root transaction does not include the writes of a facet.

Differences from Cloudflare

  • A Worker Loader class can supply a facet only from a config.capnp with --experimental.

See also Cloudflare compatibility.

Type to search…

↑↓ navigate↵ selectEsc close