Alarms
Durable Objects alarms allow you to schedule the Durable Object to be woken up at a time in the future. When the alarm's scheduled time comes, the alarm()
handler method will be called. Alarms are modified using the Storage API, and alarm operations follow the same rules as other storage operations.
Notably:
- Each Durable Object is able to schedule a single alarm at a time by calling
setAlarm()
. - Alarms have guaranteed at-least-once execution and are retried automatically when the
alarm()
handler throws. - Retries are performed using exponential backoff starting at a 2 second delay from the first failure with up to 6 retries allowed.
Alarms can be used to build distributed primitives, like queues or batching of work atop Durable Objects. Alarms also provide a mechanism to guarantee that operations within a Durable Object will complete without relying on incoming requests to keep the Durable Object alive. For a complete example, refer to Use the Alarms API.
-
getAlarm()
: number | null- If there is an alarm set, then return the currently set alarm time as the number of milliseconds elapsed since the UNIX epoch. Otherwise, return
null
.
- If there is an alarm set, then return the currently set alarm time as the number of milliseconds elapsed since the UNIX epoch. Otherwise, return
-
setAlarm(scheduledTimeMs number )
: void- Set the time for the alarm to run. Specify the time as the number of milliseconds elapsed since the UNIX epoch.
-
deleteAlarm()
: void-
Unset the alarm if there is a currently set alarm.
-
Calling
deleteAlarm()
inside thealarm()
handler may prevent retries on a best-effort basis, but is not guaranteed.
-
-
alarm()
: void-
Called by the system when a scheduled alarm time is reached.
-
The
alarm()
handler has guaranteed at-least-once execution and will be retried upon failure using exponential backoff, starting at 2 second delays for up to 6 retries. Retries will be performed if the method fails with an uncaught exception. -
This method can be
async
.
-
This example shows how to both set alarms with the setAlarm(timestamp)
method and handle alarms with the alarm()
handler within your Durable Object.
- The
alarm()
handler will be called once every time an alarm fires. - If an unexpected error terminates the Durable Object, the
alarm()
handler may be re-instantiated on another machine. - Following a short delay, the
alarm()
handler will run from the beginning on the other machine.
- Understand how to use the Alarms API in an end-to-end example.
- Read the Durable Objects alarms announcement blog post ↗.
- Review the Storage API documentation for Durable Objects.