---
title: "DurabilityScheduler"
description: "Share one physical alarm and concurrency budget across capabilities."
---

> Documentation Index
> Fetch the complete documentation index at: https://durability.shirei.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# DurabilityScheduler

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.

Click the diagram to open a full-screen explorer with the complete source files. Select a component to jump to its code, or select a linked code line to highlight the affected architecture. Use the ← / → keys or on-screen arrows to move through the flow, and the scenario menu to switch to a failure scenario. Each step says what is durable and what is only in memory. Escape or the close button returns to the guide. These are explanatory traces of the real source, not executable simulations or guarantees about an external service.

## 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`](https://github.com/avenceslau/durability/blob/main/packages/durability/src/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:

```ts
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](/reference/guarantees/#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.

```ts
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](/primitives/alarms/).

## `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](/primitives/scheduler/#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.

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

Source: https://durability.shirei.dev/primitives/scheduler/index.mdx
