Srijan Guchhait

One Durable Object per object

Single-writer ordering, an op log with a bounded tail, and idempotent action calls: how a workspace product gets its consistency on Cloudflare.

  • cloudflare
  • architecture
  • distributed-systems

I am building a workspace product that holds messages, tables, files, documents and code in one place. It runs entirely on Cloudflare. The hard part of such a product is the question of where state lives and who may change it. My answer has three parts: one Durable Object per object, one writer for each object, and every change as an idempotent action.

Layers and the unit of state

The product has three layers.

  1. The kernel handles identity, workspaces, objects, permissions, sync, actions, the outbox and metering.
  2. Primitives are a small set of object types: message stream, record table, rich document, file and repository. Notes, task lists and calendars are primitives too. A primitive is a set of actions plus the SQLite tables that hold its state.
  3. Apps are manifests. A manifest names objects over the primitives and adds views and automations. Chat, file storage and documents are apps. They use the same path a third-party app would use.

The unit of state is the object. Every piece of user data lives in one Durable Object with its own SQLite database. The address is the workspace, the type and the id. A channel is an object. A table is an object. A document is an object.

Why one writer

A Durable Object handles one request at a time, and only it writes its data. That gives each object a total order of changes. I need no locks, no cross-machine transactions and no clock comparison. Two people who edit the same table at the same moment reach the same writer, and it applies their changes one after the other.

The price is that a change cannot span two objects in one transaction. I accepted this on purpose. An object writes only to its own database. Anything that touches more than one object is derived later from the outbox.

Actions are the only door

Nothing reads or changes an object except through its actions. The web client, bots, the HTTP API and a customer's own AI call the same actions. Each action declares its input schema, whether it writes, and the role it needs. Permissions and validation live in one place. The action catalog is exported as JSON Schema, so an AI assistant can use the actions as tools without a second integration.

The write path

An action call arrives with an opId that the client chose. The object then does four things in one transaction:

  1. It assigns the next sequence number.
  2. It applies the change.
  3. It appends the change to the op log.
  4. It records a row in the outbox.

If the same opId arrives again, the object returns the stored result and applies nothing. A client that lost its connection can retry without risk. The order comes from the sequence number of the object. A wall clock never decides the order, so a device with a wrong clock cannot break it.

The op log has a limit. An object keeps its most recent operations for catch-up. A client that fell behind the log receives a snapshot instead. One device that stays offline for months therefore cannot make the log grow without bound.

Sync with one socket per object

A client opens one WebSocket for each object it has open. After it subscribes, it receives everything it missed, and then every operation in sequence order with no gaps. It never polls. Calls over the socket use the same actions as HTTP calls. Presence, such as cursors and typing, goes to the other subscribers and is never stored. The sockets hibernate with the object, so an idle connection costs nothing.

Permissions without a global lock

A workspace is a Durable Object as well. It holds the members, the roles, the directory of objects, the grants for single objects and the usage counters. It decides who may do what. Every HTTP call is authorized against it.

A long-lived socket cannot ask the workspace for every message. Each connection carries a 60-second authorization lease instead. The object checks an expired lease before it accepts a write or sends a broadcast. A revoked permission therefore takes effect within a minute.

The platform adds one more constraint. A Durable Object has a fixed location from the moment it is created. So the region is part of the workspace ID, and no lookup is needed to find where the data lives.

What it costs

  • A change across two objects has no transaction. It becomes eventually consistent through the outbox.
  • One very busy object is limited by one writer. Splitting state by object spreads most of the load.
  • Bounded history means a snapshot must always be available.

If I started again, I would defend one rule first: every mutation goes through an action, and an object writes only to its own database. Exact retries, consistent permissions and AI access all follow from that rule. They stop holding the day a second code path writes state in another way.

← All posts