Skip to content

DurabilityOutbox

DurabilityOutbox API, behavior, and recovery contract.

Import DurabilityOutbox from durability/experimental and pin an exact version. Use it for latest-state convergence, not delivery of every intermediate change.

How it uses your Durable Object

DurabilityOutbox adds no storage or infrastructure of its own. It uses the Durable Object it is constructed in:

  • ctx.storage.sql holds durability_outbox and durability_outbox_seq next to your own tables, so stage() inside ctx.storage.transaction() commits or rolls back with the state it describes.
  • The object’s one alarm is owned by the object’s one scheduler, which the outbox joins along with every other helper on the object. Every stage, claim, and retry reconciles it in the same storage transaction. The target is the earliest pending time, but no sooner than alarmMinDelayMs from now; an existing alarm between the earliest pending time and the target is kept, otherwise the alarm is set to the target, and it is deleted when nothing is pending. Your class’s alarm() must delegate to scheduler.alarm().
  • The live instance holds the channel’s running batch, the retry() marks and timeout of a publish in flight, and the scheduler’s permits and timer. Eviction loses them; the claimed row becomes due again at its publish deadline.
  • The sink is outside the object. It can receive a version again after an eviction, or an old timed-out write after a newer one, so it must keep the highest version per key.

How DurabilityOutbox uses your Durable Object

Shows Worker, OutboxObject, counters, DurabilityOutbox, Object alarm, durability_outbox, publish(), Sink. Focuses on DurabilityOutbox.One Durable ObjectWorkerRPC callerOutboxObjectyour Durable Objectcountersyour tableDurabilityOutboxin memoryObject alarmctx.storage.setAlarmdurability_outboxctx.storage.sqlpublish()your callbackSinkkeeps highest versionset()INSERTstage()rowssetAlarmentrieskey, versionalarm()

1/8 · The outbox belongs to the object instance

The scheduler is built from this.ctx, and the outbox attaches to it on the default channel with its publish callback. It reads and writes only this object’s storage.

Durable state
No value is staged yet. Constructing the outbox runs its migrations, shown next.
In memory
The scheduler, the outbox, and the publish callback exist for this instance only.
Open source ↗
import { DurableObject } from 'cloudflare:workers';
import { DurabilityScheduler } from '../../src';
import { DurabilityOutbox } from '../../src/experimental';

export class OutboxObject extends DurableObject {
  private readonly scheduler = new DurabilityScheduler({ context: this.ctx });
  private readonly outbox = new DurabilityOutbox<number>({
    scheduler: this.scheduler,
    publish: (entries) => {
      for (const entry of entries) {
        this.ctx.storage.sql.exec(
          'INSERT INTO published (key, version, value) VALUES (?, ?, ?)',
          entry.key,
          entry.version,
          entry.value
        );
      }
    },
  });

  constructor(ctx: DurableObjectState, env: Cloudflare.Env) {
    super(ctx, env);
    this.ctx.storage.sql.exec(`
      CREATE TABLE IF NOT EXISTS counters (key TEXT PRIMARY KEY, value INTEGER NOT NULL);
      CREATE TABLE IF NOT EXISTS published (key TEXT, version INTEGER, value INTEGER);
    `);
  }

  /** Writes application state and stages it in one transaction. */
  async set(key: string, value: number, fail = false): Promise<boolean> {
    const committed = this.ctx.storage.transaction(async () => {
      this.ctx.storage.sql.exec(
        'INSERT INTO counters (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value',
        key,
        value
      );
      await this.outbox.stage(key, value);

      if (fail) {
        throw new Error('rolled back');
      }
    });

    return committed.then(
      () => true,
      () => false
    );
  }

  counters() {
    return this.ctx.storage.sql
      .exec('SELECT key, value FROM counters')
      .toArray();
  }

  pending() {
    return this.ctx.storage.sql
      .exec('SELECT entry_key AS key, version FROM durability_outbox')
      .toArray();
  }

  published() {
    return this.ctx.storage.sql
      .exec('SELECT key, value FROM published ORDER BY version')
      .toArray();
  }

  override alarm(info?: AlarmInvocationInfo) {
    return this.scheduler.alarm(info);
  }
}

Behind the scenes

Durable state: durability_outbox holds the newest value per channel/key, with version, generation, attempt, and retry time. durability_outbox_seq allocates object-wide monotonically increasing versions, even after acknowledged entries are deleted.

stage() replaces the key’s pending row. Inside an application storage transaction it commits atomically with the state being described. A new generation fences settlement from an older in-flight publication. The scheduler claims due batches, then calls publish outside the transaction.

Recovery: returning acknowledges entries not marked retry(). Throw/timeout retries the whole batch without an attempt limit. A crash can repeat an already-successful external write, and a slow old write can arrive after a new one. Sinks must deduplicate (key, version) and reject version regressions; a plain unconditional KV write does not provide that guarantee.

Lifetime: superseded pending versions may never publish. Acknowledged rows disappear; detached channels retain/postpone their work. Use fanout when every message matters. The semaphore queue composes an internal outbox for cumulative accounting.

Implementation: outbox/index.ts (staging, batch claims, and publication).

Outbox (experimental)

DurabilityOutbox is exported from durability/experimental and may change in a minor release; pin an exact version when depending on it.

DurabilityOutbox publishes the newest state of a key to something outside the object — a KV namespace, another Durable Object, an API — and keeps retrying until the sink accepts it. Use it when consumers need the latest value, not every change: restaging a key before it is published replaces the pending version, so superseded versions may never be sent. For message delivery where every message matters, use DurabilityFanout.

import { DurableObject } from 'cloudflare:workers';
import { DurabilityScheduler } from 'durability';
import { DurabilityOutbox } from 'durability/experimental';

type Topology = { workflowId: string; steps: string[] };

export class Account extends DurableObject<Env> {
  private readonly scheduler = new DurabilityScheduler({ context: this.ctx });
  private readonly topology = new DurabilityOutbox<Topology>({
    scheduler: this.scheduler,
    channel: 'topology',
    // The sink keeps the highest version it has seen per key, so a slow
    // publication of an older version can never overwrite a newer one.
    publish: async (entries) => {
      const results = await this.env.TOPOLOGY_INDEX.replace(
        entries.map(({ key, version, value }) => ({ key, version, value }))
      );
      // Returning acknowledges every entry not marked for retry.
      for (const [index, result] of results.entries()) {
        if (!result.ok) {
          entries[index]!.retry();
        }
      }
    },
  });

  async saveTopology(topology: Topology) {
    await this.ctx.storage.transaction(async () => {
      this.ctx.storage.sql.exec(
        'INSERT OR REPLACE INTO topologies (id, body) VALUES (?, ?)',
        topology.workflowId,
        JSON.stringify(topology)
      );
      await this.topology.stage(topology.workflowId, topology);
    });
  }

  override alarm(info?: AlarmInvocationInfo) {
    return this.scheduler.alarm(info);
  }
}
  • Atomic with application state. stage() inside ctx.storage.transaction() commits or rolls back with the application’s writes.
  • Versions. Every stage() returns a version that is monotonic across the whole object, including after acknowledged entries are deleted. Sinks must be idempotent on (key, version) and keep the highest version they have seen: a slow publication of an older version can land after a newer one. A plain KV.put cannot do this; compare versions in a Durable Object, a transactional store, or a conditional write.
  • Acknowledgement. publish returning acknowledges every entry not marked with entry.retry(delayMs?); throwing or exceeding attemptTimeoutMs (default 60 seconds) retries the whole batch. Acknowledging a version deletes the entry only if it is still the newest, so an in-flight publication can never discard a later stage.
  • Retries never give up. Dropping the newest state would leave the sink stale forever, so there is no attempt limit or dead-letter queue; retries.delay(attempt) controls backoff (default: jittered exponential capped at 5 minutes).
  • Channels. Several outboxes share one scheduler and one set of tables, partitioned by channel (default default); each owns its own publish, and the same key in different channels is independent. An outbox attaches its channel to the scheduler through the same mechanism as every other helper’s channels, so attaching two outboxes with the same channel to one scheduler throws An outbox for channel "<channel>" is already attached to this scheduler. Due entries of a channel no outbox attaches, such as one a deploy removed, are kept and postponed 60 seconds at a time, with a durability.channel_missing report, until that channel’s outbox attaches again. Topology and the semaphore queue publish through outbox channels of their own on the same table, named durability.topology… and durability.semaphore-queue or JSON arrays starting with those names; do not give an application outbox one of those channels.
  • Batching. maxBatchSize (default 50) bounds one publish call. batchDelayMs rounds first publication up to the next multiple of the delay, so stages within one window coalesce into a batch; a stage landing exactly on a boundary is due immediately.

DurabilityOutbox.migrate(this.ctx, null) removes the outbox schema.

Type to search…

↑↓ navigate↵ selectEsc close