# Durable Objects

URL: https://eoxlabs.dev/stack/durable-objects
Last updated: 2026-11-26
Publisher: EOXLABS (https://eoxlabs.dev)

Section: [Stack](https://eoxlabs.dev/stack)

Durable Objects are a special kind of Cloudflare Worker: each object is a single, globally addressable instance of a class with its own strongly consistent storage. EOXLABS uses a Durable Object wherever several clients must agree on one piece of state: an upload session, a conversation, a rate-limit window or a live WebSocket room.

## What does a Durable Object guarantee?

Cloudflare's [storage options guide](https://developers.cloudflare.com/workers/platform/storage-options/) states the core property: only one instance of a Durable Object with a given ID runs at once, anywhere in the world, and requests for that ID are routed to it. The instance runs one event at a time. Its storage is private; no other object can read it.

Three runtime mechanisms make that model safe to program against, and Cloudflare's [rules of Durable Objects](https://developers.cloudflare.com/durable-objects/best-practices/rules-of-durable-objects/) describes each:

- **Input gates** stop new events from being delivered while a storage operation is in flight.
- **Output gates** hold a response until the writes made before it are persisted, so a client is never told a write succeeded before it is safely stored.
- **Write coalescing** commits several writes made without an intervening `await` as one atomic transaction.

The gap matters as much as the guarantee. Input gates protect storage operations only. A `fetch()` to another service or a write to R2 lets other requests interleave, so EOXLABS re-reads state after any external `await` before acting on it.

## How does EOXLABS model Durable Objects?

The design question is always the same: what is the atom of coordination? EOXLABS gives each tenant, conversation, upload or device its own object, and never routes all traffic through one global object, which Cloudflare lists as an anti-pattern. CONDUIT, the EOXLABS edge file-transfer product, coordinates resumable, chunked uploads with Durable Objects. The sketch below shows the shape of that pattern, not product code:

```ts
import { DurableObject } from "cloudflare:workers";

export class UploadSession extends DurableObject<Env> {
  constructor(ctx: DurableObjectState, env: Env) {
    super(ctx, env);
    ctx.blockConcurrencyWhile(async () => {
      this.ctx.storage.sql.exec(
        "CREATE TABLE IF NOT EXISTS parts (n INTEGER PRIMARY KEY, etag TEXT NOT NULL)",
      );
    });
  }

  async recordPart(n: number, etag: string): Promise<number> {
    this.ctx.storage.sql.exec("INSERT OR REPLACE INTO parts (n, etag) VALUES (?, ?)", n, etag);
    // One alarm per object: each new part pushes the expiry an hour out.
    await this.ctx.storage.setAlarm(Date.now() + 60 * 60 * 1000);
    return this.ctx.storage.sql
      .exec<{ total: number }>("SELECT count(*) AS total FROM parts")
      .one().total;
  }

  async alarm(): Promise<void> {
    // Alarms run at least once, so cleanup must be safe to repeat.
    await this.ctx.storage.deleteAll();
  }
}

// In the calling Worker: const total = await env.UPLOADS.getByName(uploadId).recordPart(3, etag);
```

The conventions behind it:

- **Names versus unique IDs.** `getByName()` suits entities whose ID the caller already knows. For a brand-new entity, `newUniqueId()` is faster on first use, because a name-derived ID needs a global uniqueness check that Cloudflare's [namespace reference](https://developers.cloudflare.com/durable-objects/api/namespace/) says can take up to a few hundred milliseconds.
- **RPC methods, not a `fetch()` handler**, so calls are typed and the object exposes a narrow interface.
- **SQLite storage with migrations in the constructor**, wrapped in `blockConcurrencyWhile()` so no request sees a half-migrated schema.
- **Alarms for deadlines.** Each object holds one alarm; the [Alarms API](https://developers.cloudflare.com/durable-objects/api/alarms/) guarantees at-least-once execution and retries a failed handler with exponential backoff from 2 seconds, up to 6 times. [Durable Object alarms in practice](https://eoxlabs.dev/engineering/durable-object-alarms) covers idempotent handlers in depth.
- **Hibernating WebSockets.** Connections accepted with `ctx.acceptWebSocket()` stay open while the object leaves memory, and per-connection state travels in `serializeAttachment()`. Cloudflare's [WebSocket guide](https://developers.cloudflare.com/durable-objects/best-practices/websockets/) notes that billable duration does not accrue during hibernation. The [real-time inbox note](https://eoxlabs.dev/engineering/websocket-hibernation-realtime-inbox) and [real-time voice](https://eoxlabs.dev/capabilities/real-time-voice) build on this.

## What limits and trade-offs come with Durable Objects?

| Property | Value | Design consequence |
|---|---|---|
| [Storage per object](https://developers.cloudflare.com/durable-objects/platform/limits/#sqlite-backed-durable-objects-general-limits) | 10 GB on Workers Paid, 1 GB on Free (SQLite backend); 8 MB per row or key-value pair | Large files go to R2; the object keeps references |
| [Throughput per object](https://developers.cloudflare.com/durable-objects/platform/limits/#how-much-work-can-a-single-durable-object-do) | Soft limit of 1,000 requests per second; excess requests queue, then fail as overloaded | Hot entities are sharded across several objects |
| [Point-in-time recovery](https://developers.cloudflare.com/durable-objects/api/sqlite-storage-api/) | Any point in the past 30 days, per object | A bad write can be rolled back without a separate backup system |
| [Location](https://developers.cloudflare.com/durable-objects/reference/data-location/) | Created near the first request and does not currently move; location hints are best effort | Objects are created from real traffic, or with a hint |
| Jurisdiction | `eu`, `us` or `fedramp` namespaces constrain where an object runs and stores data | Used where residency rules apply; the same name in another jurisdiction is a different object |

## Where should coordination state live?

| | Durable Object | D1 | Workers KV |
|---|---|---|---|
| Concurrent writers | Serialized by the object's single thread | Serialized by a [single-threaded primary database](https://developers.cloudflare.com/d1/platform/limits/) | Last write wins; [1 write per second per key](https://developers.cloudflare.com/kv/platform/limits/) |
| Read freshness | Strongly consistent | Sequentially consistent within a session | [Eventually consistent](https://developers.cloudflare.com/kv/concepts/how-kv-works/); changes can take 60 seconds or more to appear elsewhere |
| Scale unit | Many small objects | Many databases of up to 10 GB | Unlimited keys per namespace |
| Best fit | Per-entity coordination, timers, WebSockets | Relational queries across entities | Read-mostly configuration |

Each object's storage is visible only to that object, so cross-entity reporting needs a second store. EOXLABS writes per-entity state in the object and projects what must be queried across entities into [D1](https://eoxlabs.dev/stack/d1). Read-heavy settings that tolerate delay are better served from the edge cache of [Workers KV](https://eoxlabs.dev/stack/workers-kv).

## When not to use Durable Objects

- **Stateless request handling.** A plain Worker avoids the routing hop to wherever the object lives.
- **One global object for everything.** A single object that every request touches becomes the bottleneck at its throughput limit.
- **Queries across many entities.** Reporting, search and analytics need D1 or Postgres; there is no SQL across objects.
- **One entity with more than 10 GB of data.** That data belongs in R2 or a dedicated database.
- **Residency enforced by location hints.** Hints are best effort. Only a jurisdiction-restricted namespace constrains where data is stored.

Durable Objects sit beside [Cloudflare Workers](https://eoxlabs.dev/stack/cloudflare-workers) in the [EOXLABS stack](https://eoxlabs.dev/stack). In [telecommunications](https://eoxlabs.dev/industries/telecommunications) systems built around live sessions, EOXLABS starts the design from the session object.

## Related

- [Durable Object alarms instead of background jobs](https://eoxlabs.dev/engineering/durable-object-alarms): Replacing process-model background jobs with Durable Object alarms, with verified semantics, a TypeScript scheduler that runs many jobs on one alarm, idempotency rules and a decision list.
- [WebSocket hibernation for a real-time inbox](https://eoxlabs.dev/engineering/websocket-hibernation-realtime-inbox): One Durable Object per inbox, hibernating WebSockets, messages written to SQLite before fan-out, and a sequence cursor that lets a reconnecting device replay exactly what it missed.
- [Real-Time and Voice Systems](https://eoxlabs.dev/capabilities/real-time-voice): EOXLABS designs live features (chat, presence, live dashboards, calls and voice agents) with WebSockets held by Durable Objects for state and Cloudflare Realtime for WebRTC media.
- [D1](https://eoxlabs.dev/stack/d1): D1 is the relational store EOXLABS reaches for first on Cloudflare, and its single-threaded design shapes schemas, replica sessions and the point where Postgres is the better choice.
- [Cloudflare Workers](https://eoxlabs.dev/stack/cloudflare-workers): Cloudflare Workers is the request layer of EOXLABS systems, and this page covers how EOXLABS structures a Worker, the limits behind each design choice, and where a Worker is the wrong tool.

## More stack

- [R2](https://eoxlabs.dev/stack/r2)
- [Workers KV](https://eoxlabs.dev/stack/workers-kv)
