# Cloudflare Cron Trigger runs twice, late or never

> One minute can be delivered twice, a little early, or never. How to write a scheduled() handler so that each job still runs once and on the minute it was due.

Published 2026-11-22 · https://aiskills402.com/blog/cloudflare-cron-trigger-runs-twice-late-or-never

A Cloudflare Cron Trigger runs twice, starts a job a minute late or skips it because nothing in the platform promises exactly one event per minute, delivered on the minute. You recognise the problem by its outputs, since the logs stay calm: two identical emails or posts that belong to one minute, an hourly job that always starts about a minute after the hour, a daily task that is simply absent on some days. The cure is in the `scheduled()` handler, not in the cron expression. Claim each (minute, job) pair with one atomic insert before anything leaves the Worker, compute the minute by rounding `scheduledTime`, catch up the minutes that never arrived, and set the watchdog above the real gaps. We learned each of these on a Worker of ours that dispatches about twenty jobs from a single trigger firing every minute. Each minute arrived twice on 12 October 2026, and on 23 October as many as 13 minutes in one hour never arrived at all.

## What the documentation does and does not say

The Cron Triggers page explains where these invocations run: on machines that have spare capacity at the moment, chosen to use the network efficiently. It says the schedule is read in UTC, and that a change to a trigger may take as long as 15 minutes to spread across the network, with invocations possibly lost while it spreads ([Cloudflare Cron Triggers](https://developers.cloudflare.com/workers/configuration/cron-triggers/)). We read the page on 22 November 2026 looking for a delivery promise and found none: no exactly-once, no on-the-second, no statement about copies.

The Scheduled Handler page adds the one field that matters here. `controller.scheduledTime` is the moment the event was meant for, in epoch milliseconds ([Scheduled Handler](https://developers.cloudflare.com/workers/runtime-apis/handlers/scheduled/)). In our logs, both copies of a doubled minute pointed at that one minute. The wall clock at the moment your code runs is a different number, and the rest of this guide depends on keeping the two apart.

Read together, the safe reading is that a minute may arrive once, twice, early, late or not at all, and the code must give the right result in each of those cases. Our measurements below are one Worker's history, not a forecast for yours.

## Step 1: claim the minute before anything leaves the Worker

On 12 October our Worker received two events for every minute. A second copy followed each original by a minute or two, and its `scheduledTime` pointed at the same minute as the first. Everything due in those minutes went out twice, one public post included.

The fix is a small claim table whose primary key is the pair (minute, job), and an insert that quietly gives way when that pair is already stored:

```sql
CREATE TABLE job_runs (
  minute INTEGER NOT NULL,
  job    TEXT    NOT NULL,
  PRIMARY KEY (minute, job)
);
```

```ts
async function claimAndRun(env, minute, job) {
  const sql = "INSERT INTO job_runs (minute, job) VALUES (?, ?) ON CONFLICT (minute, job) DO NOTHING";
  const result = await env.DB.prepare(sql).bind(minute, job.name).run();
  if (result.meta.changes !== 1) return; // some other delivery got here first
  await job.run(env, minute);
}
```

SQLite turns such an insert into a no-op when it would break a uniqueness rule, and it only acts on a primary key, a `UNIQUE` constraint or a unique index ([SQLite UPSERT](https://sqlite.org/lang_upsert.html)). The columns after `ON CONFLICT` must name that key; without it there is nothing to collide with, and the guard protects nothing. D1 reports how many rows the statement changed in `meta.changes`, so the losing copy sees a zero and leaves ([D1 return object](https://developers.cloudflare.com/d1/worker-api/return-object/)).

Two shortcuts look equivalent and are not. Reading first and inserting afterwards lets both copies pass the read before either writes. A flag in a module variable lives in one isolate, and the second event may be served by another one. The insert has to be the decision itself, and it has to come before the email, the post, the payment call or the webhook. The same reasoning protects a pipeline that announces articles, where a doubled run is visible to everyone; our guide to [duplicate and flooded social posts](/blog/social-auto-posting-duplicates-floods) treats that case from the side of the article row.

The claim table grows by one row per job per minute, so give it its own cleanup. On each tick, remove a small batch of rows older than a few days and log how many went. Because the minute comes first in the primary key, that delete can use the index.

## Step 2: take the key from the event, not from the clock

Because the duplicate shows up a minute or two behind the original, a key derived from `Date.now()` names a later minute for it. Its insert finds nothing in the way, and the job fires again although a claim is in place and looks correct in review. Build the key from `scheduledTime`, which after rounding gives both copies one and the same minute. When a second clock joins later, it must arrive at an identical integer for any given minute, or each clock wins a claim of its own.

## Step 3: round the minute, do not floor it

On our Worker, `scheduledTime` came about two seconds before the minute it stood for. Dividing by sixty thousand and flooring then returns the minute before. A matcher fed that value decides the top-of-the-hour job is not due yet, the following event matches it, and the job starts almost a minute late. Every job in the hub ran about 60 seconds behind until we changed one function call.

```ts
const minute = Math.round(controller.scheduledTime / 60_000); // nearest minute
```

Adding a margin of a few seconds before a floor gives the same result. What matters is that an event stamped slightly early lands on the minute it represents.

## Step 4: assume minutes go missing

On 23 October the trigger skipped minutes while Workers reported normal operation: as many as 13 in one hour, quiet stretches as long as eight minutes, and a few events that turned up 20 to 47 minutes behind their minute. A job whose only chance was a skipped minute never started, and nothing logged its absence.

So no job may depend on one exact minute. Store a pulse, the latest minute the handler has handled, and on every wake replay the minutes since it:

```ts
const MAX_BACK = 30; // how many minutes a replay may reach into the past

async function handle(env, minute) {
  const previous = await readPulse(env);         // latest minute handled so far
  await writePulse(env, minute);                 // stored as max(old, new)
  const first = Math.max(previous + 1, minute - MAX_BACK);
  for (let m = first; m <= minute; m++) {
    for (const job of jobsDueAt(m)) await claimAndRun(env, m, job);
  }
}
```

Three details decide whether this helps or hurts. The replay has a fixed limit, so a long outage does not release a day of minutes at once. Each replayed minute goes through the same claim as Step 1, so a late copy that arrives during the catch-up finds its minute taken. And the pulse only moves forward, which keeps an event that is forty minutes late from dragging it back.

A daily job can also sit on the every-minute schedule if the code gives it a bounded window, say the hour after three in the morning, and claims it on (day, job) rather than (minute, job). Whichever minute reaches the handler first within that hour does the work, and the later ones see the claim and stop. The window needs its end, or a deploy in the afternoon starts the nightly job hours after its time.

## Step 5: set the watchdog above the real gaps

A watchdog that reads the pulse measures how long the handler has been quiet. With quiet stretches of up to eight minutes on a working scheduler, a threshold of three minutes pages people for nothing, while a threshold measured in hours lets a dead scheduler go unnoticed. Put the threshold above the longest gap in your own log plus one tick, or demand that several consecutive checks fail. The late events do not change the number: an event that shows up forty minutes late is extra work for the claim, not a longer quiet stretch. Our piece on [watchdogs that stay quiet or mail too much](/blog/watchdog-alerts-silent-and-noisy) covers the rest of that design.

Write the pulse when the handler wakes, before the jobs. A pulse that is stored only once an hour-long job finishes makes a healthy scheduler look dead.

## Step 6: a second clock from a Durable Object alarm

A Durable Object can hold one alarm at a time. Alarms are executed at least once and retried with growing delays when the handler throws, and since those retries are limited, the page advises catching errors inside the handler and booking a new alarm before it returns ([Durable Objects alarms](https://developers.cloudflare.com/durable-objects/api/alarms/)). At least once means an alarm can also deliver a minute twice, so it needs the claim just as much as the cron does.

```ts
export class Clock extends DurableObject {
  async alarm() {
    const rangAt = Date.now();
    const nextRing = (Math.floor(rangAt / 60_000) + 1) * 60_000 + 1_000;
    await this.ctx.storage.setAlarm(nextRing);                         // book the next ring first
    this.ctx.waitUntil(handle(this.env, Math.round(rangAt / 60_000))); // then hand off the work
  }
}
```

Booking the next ring comes first, because a throw or a long job placed before it means the clock silently stops. The work goes to `ctx.waitUntil` rather than being awaited, so the next ring does not wait for the slowest job; our notes record that inside a Durable Object it keeps the object alive until the promise settles. The cron arms the alarm only when `getAlarm()` returns null. The alarms page warns that this call returns null while an alarm is running unless the next one is already booked, one more reason to book first. In our measurements an alarm booked for one second after the minute fired roughly 1.4 seconds behind that, and rounding still places it on the minute the cron event names. Both clocks call the same `handle()`, share one claim table and one pulse, and the minute runs once.

The alarm is no substitute for the replay of Step 4. A clock only serves the minute in which it rang, so a minute lost by both of them comes back through the catch-up alone.

## A check that can fail

Each step above has a test that turns red when the step is undone. Run each test once against the broken version before trusting it.

- **Double delivery.** Call the handler twice at once with the same `scheduledTime`. One row in the claim table and one outward call must result. Replace the insert with a `SELECT` and a separate `INSERT`, and the test must fail.
- **Early stamp.** Give it a `scheduledTime` two seconds before the top of an hour. The computed minute must be the first minute of that hour. Change the rounding to a floor and the test must fail.
- **Skipped minutes.** Leave the pulse ten minutes behind with a job due inside the gap. That job must run once on this wake, and a later copy of its minute must change nothing.
- **Long outage.** Leave the pulse two days behind. Only the bounded window may replay.
- **Throwing alarm.** Make a job throw inside the alarm. The next ring must still be booked afterwards.

## When this does not apply

- **Work that is safe to repeat.** Rebuilding a cache or overwriting a file with the same content costs only time when it runs twice. A claim table may be more than such a job needs, though the catch-up still matters if a skipped run does.
- **One Worker, one month.** Our numbers come from a single scheduler Worker on one account in October 2026. Another account may never see a doubled minute, or may see longer gaps. Count your own invocations per hour and compare `scheduledTime` with the clock before you choose a threshold.
- **Not measured.** We did not test schedules attached to Workflows, which the Cron Triggers page also mentions, nor behaviour after Cloudflare changes its scheduler.
- **Wrong day, not wrong minute.** If a job runs on the wrong weekday, the cause is usually the day numbering, which Cloudflare counts from Sunday; our [comparison of how models write those numbers](/blog/llm-cron-cloudflare-day-numbers) shows the trap.
- **A store without an atomic insert.** Everything here relies on a unique key and a single conditional write. A store that only offers read and write as two steps cannot hold the claim.
- **Exact seconds.** Neither clock promises timing to the second. A job that must happen at one exact moment needs a design that accepts a window.

## Our skills for this topic

- **Scheduler code.** [Our scheduler review](/skills/cloudflare-cron-trigger-review) takes a pasted `scheduled()` handler together with its catch-up, its watchdog and any alarm beside it, and points to each place where a job could fire twice, late or never, giving a short code, the cause and corrected code, then a verdict.
- **Cron expressions.** [The schedule writer for Workers](/skills/cron-schedule) writes Cron Trigger expressions from a plain description of when a job should run, counting Sunday as day one and keeping every time in UTC.
- **Watchdogs.** [Our liveness alarm review](/skills/watchdog-alert-review) goes through the code of a staleness check or heartbeat alarm for both ways it can fail: keeping quiet about a dead source, or sending a stream of alerts about a healthy one.
