How a Durable Objects alarm loop runs up your Cloudflare bill
A Durable Object alarm that keeps rescheduling itself racks up requests, storage writes and duration. What that costs at list price, and how to stop it.
A Durable Object alarm runs your alarm() handler at a time you pick. Pretty common pattern: set the next alarm inside the handler. If a bug sets that time to now, or a few ms from now, the handler just runs again and again. The handler keeps succeeding, so you might not notice for a while.
async alarm() {
await this.flush();
// Bug: delayMs never got set from config, so it's 0.
await this.ctx.storage.setAlarm(Date.now() + (this.delayMs ?? 0));
}What it costs
Every alarm run is a request ($0.15 per million) and every setAlarm is a storage write. On top of that, an object that's busy back to back doesn't get to go idle, so it bills duration too: $12.50 per million GB-s, at 128 MB per object.
Using Cloudflare's own pricing math, an object that's active all day is 0.128 GB × 86,400 s = 11,059 GB-s, about $0.14 a day. Tiny for one object. But if the bug is in the shared class, every object that hits this alarm path can loop. 1,000 of them is ~$138 a day in duration alone, at list price before the monthly allowance.
Why you may not notice
Budget alerts send an email and keep everything running. Cloudflare's newer Durable Objects spending limit does block, but it's monthly and account-wide, so it blocks new DO requests and alarms across the whole account, not just the looping object. A loop that starts on a Friday night can run a long way before either kicks in.
How to stop it
- Validate the delay before you schedule. Missing, zero or negative means a bug, so throw instead of looping.
- Set a floor: never schedule an alarm less than a few seconds out.
- Stop the loop when there is no work. Call
deleteAlarm()instead of setting a new one. - Watch duration per namespace in the Workers analytics, not only the monthly bill.
workerfuse checks Durable Object usage every five minutes. When a Worker's estimated spend passes your limit, it pauses that Worker instead of the whole account. The stub stops the alarm loop once the object picks it up, which took about five minutes in our test. Start a free trial.