Skip to content

DurabilityScheduler

Share one physical alarm and concurrency budget across capabilities.

Import DurabilityScheduler from durability. A Durable Object has one scheduler: every DurabilityScheduler and every helper configured with context for the same object join it. A scheduler coordinates that object’s work, not work across different objects.

How it uses your Durable Object

The scheduler owns the parts of the Durable Object that its helpers would otherwise compete for:

  • The object’s one alarm (ctx.storage.setAlarm). Helper writes that add or reschedule pending work run in a storage transaction that also sets the alarm to the earliest pending row of every attached table. Your class’s alarm() must delegate to scheduler.alarm() or to any helper’s alarm(); each runs the due work of every helper on the object.
  • ctx.storage.sql stays with the helpers: each creates its own tables and registers its channel of them, and the scheduler only reads their pending times.
  • The live instance holds the merged scheduler options, the concurrency permits and their FIFO waiters, the early timer for work due before the alarm, the one native timer behind every in-memory timeout, and the attached tables and channels. Eviction discards them; the alarm and the rows bring the work back.

How DurabilityScheduler uses your Durable Object

Shows Worker, Subscription, Object alarm, Scheduler, Early timer, Durability, DurabilityAlarms, durability_calls, durability_alarms. Focuses on Scheduler.One Durable ObjectWorkerfetch handlerSubscriptionyour Durable ObjectObject alarmstorage.setAlarmSchedulerin memoryEarly timershared timerDurabilitychargeInvoiceDurabilityAlarmsrenewdurability_callsctx.storage.sqldurability_alarmsctx.storage.sqlsubscribe()alarm(info)alarm()runDuerunDuerowsrows

1/9 · One scheduler per object instance

The class constructs a DurabilityScheduler from this.ctx. The first attachment for an object creates its engine, the only code that sets the object’s alarm and its early timer. It starts with alarmConcurrency permits, here 5, which every attached helper shares.

Durable state
Nothing is written yet.
In memory
The engine, its permits, and its timer are rebuilt after every eviction.
Open source ↗
/**
 * `DurabilityAlarms` + `DurabilityScheduler` example: recurring named alarms
 * that trigger durable operations, sharing one physical Durable Object alarm.
 *
 * Each subscription object schedules a `renew` alarm. When it fires, the
 * handler registers a durable `chargeInvoice` operation (retried on failure
 * independently of the alarm) and schedules the next occurrence. Scheduling a
 * name again replaces its pending occurrence, so `subscribe` is repeatable.
 */
import { DurableObject } from 'cloudflare:workers';
import {
  Durability,
  DurabilityAlarms,
  DurabilityScheduler,
  type DurableCall,
} from 'durability';

type Env = {
  SUBSCRIPTIONS: DurableObjectNamespace<Subscription>;
  PAYMENTS: Fetcher;
};

const renewalPeriodMs = 30 * 24 * 60 * 60 * 1000;

type ChargeInput = { customerId: string; periodStart: number };

export class Subscription extends DurableObject<Env> {
  // One object uses both helpers, so they must share a scheduler: it owns the
  // single physical alarm and one concurrency pool for all handlers.
  private readonly scheduler = new DurabilityScheduler({
    context: this.ctx,
    alarmConcurrency: 5,
  });

  private readonly durability = new Durability({
    scheduler: this.scheduler,
    handlers: {
      chargeInvoice: async ({ id, payload }: DurableCall<ChargeInput>) => {
        const response = await this.env.PAYMENTS.fetch(
          'https://payments/charge',
          {
            method: 'POST',
            headers: { 'Idempotency-Key': id },
            body: JSON.stringify(payload),
          }
        );

        if (!response.ok) {
          throw new Error(`Charge failed with ${response.status}`);
        }
      },
    },
    retries: { maxAttempts: 8 },
  });

  private readonly alarms = new DurabilityAlarms({
    scheduler: this.scheduler,
    handlers: {
      renew: async ({ idempotencyKey, scheduledTime }) => {
        const customerId = await this.ctx.storage.get<string>('customerId');

        if (customerId === undefined) {
          return; // Cancelled; let this occurrence complete without renewing.
        }

        // The alarm's idempotencyKey is stable across its retries, so a
        // retried occurrence cannot double-charge.
        await this.durability.chargeInvoice({
          id: idempotencyKey,
          payload: { customerId, periodStart: scheduledTime },
        });
        await this.alarms.renew(scheduledTime + renewalPeriodMs);
      },
    },
  });

  async subscribe(customerId: string) {
    await this.ctx.storage.put('customerId', customerId);
    await this.alarms.renew(Date.now() + renewalPeriodMs);
  }

  async cancel() {
    await this.ctx.storage.delete('customerId');
  }

  /** Destructive retention cleanup for settled records older than 90 days. */
  async purgeHistory() {
    const cutoff = Date.now() - 90 * 24 * 60 * 60 * 1000;

    return {
      operations: await this.durability.purgeBefore(cutoff),
      alarms: await this.alarms.purgeBefore(cutoff),
    };
  }

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

export default {
  async fetch(request, env): Promise<Response> {
    const url = new URL(request.url);
    const [, customerId, action] = url.pathname.split('/');

    if (!customerId || request.method !== 'POST') {
      return new Response('Expected POST /:customerId/(subscribe|cancel)', {
        status: 404,
      });
    }

    const subscription = env.SUBSCRIPTIONS.getByName(customerId);

    if (action === 'subscribe') {
      await subscription.subscribe(customerId);

      return new Response('subscribed', { status: 201 });
    }

    if (action === 'cancel') {
      await subscription.cancel();

      return new Response('cancelled');
    }

    return new Response('Not found', { status: 404 });
  },
} satisfies ExportedHandler<Env>;

Behind the scenes

Durable state: the scheduler creates no job table of its own. Attached helpers provide their tables and due-work callbacks; the engine consults them to reconcile the object’s one physical alarm. Helpers of one kind share a table, partitioned by channel: the first instance registers the table, each instance attaches its own channel, and a second instance on a channel already attached throws.

Engine transactions reconcile that alarm alongside their storage changes. The next physical alarm is bounded by the earliest due work and now + alarmMinDelayMs. Near-due work can also start from an in-memory timer; experimental liveTimers extends timer scheduling further into the future.

In memory: timers, active execution locks, promises, and the FIFO concurrency pool. Eager operations and ordinary scheduled work share the default ten permits. Some capabilities, such as recurring schedule runs, explicitly maintain a separate execution limit; a shared scheduler is not a universal limit on all application work.

Recovery: a physical alarm reconstructs progress from durable participant state after eviction. Long alarm invocations hand off before the wall-time limit. If a pass itself fails, the scheduler arms a fallback at alarmMinDelayMs and retries the pass from memory sooner; only failure to arm that fallback propagates to platform retries.

Because every helper on the object joins the same engine, no helper can move or delete the physical alarm another one set. Not every helper needs alarm work: the retained log uses the engine for storage operations but does not automatically schedule retention.

Implementation: scheduler/index.ts (engineFor, Engine.attach, registerChannel, Engine.transaction, reconcile, runAlarm, startExecution).

Using both in one Durable Object

A Durable Object has one physical alarm. When one object uses both helpers, they share the alarm, the storage, and the concurrency pool. Creating a DurabilityScheduler and passing it to each makes that sharing explicit and keeps the scheduler options in one place:

import { Durability, DurabilityAlarms, DurabilityScheduler } from 'durability';

export class ImageJobs extends DurableObject<Env> {
  private readonly scheduler = new DurabilityScheduler({
    context: this.ctx,
    alarmConcurrency: 10,
    alarmMinDelayMs: 15_000,
  });

  private readonly durability = new Durability({
    scheduler: this.scheduler,
    handlers: {
      resizeImage: async ({ payload }) =>
        this.env.IMAGES.resize(payload.imageId),
    },
  });

  private readonly alarms = new DurabilityAlarms({
    scheduler: this.scheduler,
    handlers: { cleanup: async () => this.env.IMAGES.cleanup() },
  });

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

One scheduler per Durable Object

The engine is looked up by the object’s ctx.storage. The first DurabilityScheduler or helper built from that context creates it, and every later one joins it, so a helper configured with context instead of scheduler ends up on the same alarm, storage, and concurrency pool as the example above. Passing scheduler explicitly is optional.

  • Options merge. Each attachment may pass scheduler options (alarmConcurrency, alarmMinDelayMs, alarmHandoffMs, liveTimers, onAlarmRetry). An option one attachment sets applies to every helper on the object, an attachment that leaves it out never conflicts, and an attachment that sets a different value throws Scheduler option "…" is already set to a different value for this Durable Object. Values are compared with ===, so two different onAlarmRetry functions conflict.
  • Any alarm() runs everything. scheduler.alarm() and every helper’s alarm() call the same engine, so delegating the object’s alarm to any one of them runs the due work of every helper. A library can build its helpers from the context it is given, and the application only forwards alarm.
  • One copy of the library. The engines are kept in a registry shared by every copy of durability in the isolate. A second copy reaching an object that already has an engine throws Another copy of durability is already attached to this Durable Object; install durability once, for example as a peer dependency of libraries that use it.

Channels

Every helper that attaches to the scheduler takes a channel option, default unless given one, so several instances of one helper kind can share the scheduler: an application’s Durability on default and a library’s on its own channel, for example. Each channel of a helper kind holds one instance. Instances share the table, partitioned by a channel column, and each alarm pass runs the due rows of every attached channel through that channel’s instance.

For operations, named alarms, fanout deliveries, and outbox entries, reconciliation reads the earliest pending row across all channels of the table, including channels no instance attached. A pass keeps the due rows of an unattached channel, for example one a deploy removed, moves their next attempt one minute later, and reports durability.channel_missing. They run once an instance attaches that channel again. DurabilitySchedule, DurabilitySemaphore, and DurabilitySemaphoreQueue compute their own pending times: an unattached channel’s records stay unchanged, do not arm the alarm, and are reported once; attaching the channel again restores its alarm.

Delivery semantics and concurrency

Work is delivered at least once, as the delivery and safety contract describes. A process can stop after a side effect succeeds but before its completion record commits, so handlers should be idempotent and pass the call ID or alarm idempotencyKey to external services.

alarmConcurrency defaults to 10 and is one FIFO limit shared by eager operation handlers, timer-driven work, and physical-alarm work attached to the same scheduler. Newly registered operations start eagerly only when a permit is immediately available. Work due before the physical-alarm minimum enters the same queue when its in-memory timer fires.

Operation completion records are retained indefinitely so their IDs remain deduplicated. The scheduler owns the Durable Object’s alarm; compose unrelated scheduled work as named alarms instead of replacing the physical alarm.

When a run cannot finish

A Durable Object alarm whose handler throws is retried with backoff a limited number of times and then dropped. Every helper’s alarm() ends by reconciling the physical alarm, and a storage error there would otherwise propagate, so enough of them in a row would leave pending work with nothing armed. Instead, a run that fails arms the physical alarm directly at alarmMinDelayMs from now and returns, reporting durability.alarm.failed with the error; the next run reconciles properly. Only when even that setAlarm fails does the failure reach the platform and its retries.

The fallback alarm can be up to 15 seconds away, so the scheduler also retries the run from its in-memory timer: 1 second after the first failure, doubling with each failure in a row up to alarmMinDelayMs. A run that succeeds resets the delay. The fallback alarm stays armed, so the work still runs if the object is evicted before the timer fires.

Long-running alarm calls

Alarm invocations have a 15-minute wall-time limit. If work is still pending after alarmHandoffMs (default 14 minutes), the scheduler retains its promise in memory, arms a fallback after alarmMinDelayMs, and returns from the current invocation. The next alarm attaches to the same promise instead of starting the handler again. If the object is evicted during a handoff, the next alarm reconstructs and executes the persisted pending work.

setTimeout (scheduler)

scheduler.setTimeout(callback, delayMs) runs callback once after delayMs and returns a function that cancels it. Use it instead of the global setTimeout for in-memory timeouts whose number grows with the object’s workload.

The runtime makes the global setTimeout throw QuotaExceededError once 10,000 timers are pending in one Durable Object. The scheduler keeps every pending timeout of the object, its helpers’ and yours, in a min-heap ordered by due time and sets one native timer for the earliest, so the number pending does not count against that limit.

const cancel = this.scheduler.setTimeout(() => {
  this.sockets.get(id)?.close(4000, 'Idle');
}, 30_000);

// Later, if the socket sends something first:
cancel();

It differs from the global setTimeout in three ways:

  • It returns a cancel function, not an id for clearTimeout.
  • Callbacks due at the same time run in one task, in the order they were scheduled, so promise reactions queued by one do not run before the next. A callback scheduled from inside another runs in a later task, even with no delay.
  • A callback that throws is reported as durability.timer.callback_failed instead of becoming an uncaught exception, and the callbacks after it still run.

The timeout exists only in memory, so an eviction drops it. Schedule work that must still run after one as a named alarm.

liveTimers (scheduler)

liveTimers?: boolean. Due work starts from an in-memory timer at its exact scheduled time, however far beyond alarmMinDelayMs, and an alarm pass skips records that are already running instead of waiting for them, so a long handler no longer delays newly due work. The physical alarm stays armed at the usual bounded delay as the eviction fallback, and the handoff before the alarm wall-time limit is unchanged.

Gives up: timers do not survive eviction, so after one the work runs only when the physical alarm fires. Attempts interrupted by the eviction are counted as usual, so an operation with maxAttempts: 1 fails instead of running again.

onAlarmRetry (scheduler)

onAlarmRetry?: (info: AlarmRetryInfo) => void | Promise<void>, where info is { error: { name, message }, retryInMs, retryCount?, isRetry?, objectId? }. It is called each time an alarm run fails and the scheduler re-arms its own fallback alarm (see When a run cannot finish), so the error can go to an external service. retryInMs is alarmMinDelayMs; retryCount and isRetry come from the platform’s AlarmInvocationInfo when the invocation carried one, and objectId from the context.

The durability.alarm.failed report still happens. The hook is best effort: it runs after the fallback is armed, a returned promise is passed to waitUntil, and a throw or rejection is only reported as durability.alarm.retry_hook_failed, so it cannot change scheduling. It is not called when even the fallback cannot be armed, because the failure then reaches the platform.

const scheduler = new DurabilityScheduler({
  context: this.ctx,
  onAlarmRetry: (info) => this.env.ERRORS.send(info),
});

Type to search…

↑↓ navigate↵ selectEsc close