Reliable HTTP cron and delayed-job scheduling with automatic retries and full run logs — delay.run

This is a hypothetical case study. The company and numbers are invented; the bug is a classic one: a "local time" schedule that meets a day with two 09:00s, or none, and a job that is not safe to run twice.

The setup

A product-management tool sends a weekday digest: "here's what is due today". Users pick a local time, most leave it at 09:00. The app stores timezone (an IANA name, usually) and digest_hour. A single cron wakes up every 15 minutes, in UTC, and selects users whose local time has just rolled past the chosen hour:

# runs at minute 0, 15, 30, 45 now = datetime.now(timezone.utc) for user in users_with_digests_enabled(): local = now.astimezone(ZoneInfo(user.timezone)) if local.weekday() < 5 and local.hour == user.digest_hour and local.minute < 15: send_digest(user) 

There is no record of "digest already sent for this local date". The 15-minute window is supposed to make the job idempotent enough: it only sends when minute < 15, and the cron is not supposed to run twice in that window.

The incident

Sunday, 2 November, 02:00 in America/New_York. Clocks fall back. 01:30 happens, then 01:30 happens again. The digest hour for most of these users is 09:00, so the fallback itself is not the send window. The more interesting failure is the identity of "today" and the jobs for zones where the chosen hour is in the repeated or skipped range — and, in this system, a second bug that Sunday night's UTC cron behaviour exposed on Monday.

Monday, 09:00 Eastern. The digest sends as usual at 14:00 UTC. The cron also has a second path, added months ago for users who want 09:00 in zones that do not observe DST, written with a fixed offset:

local = now + timedelta(hours=user.offset_hours) 

About 800 users still have offset_hours populated from an older signup form, and their timezone is empty. For them, "Eastern" is -5 or -4 depending on who last saved the profile, not a zone that can name the fold.

The worse cut is the users who do have a proper zone, and who had picked 01:00 as their digest time (a small night-shift group, about 200 people). On the night of the fallback, 01:00 happened twice. The job ran in both UTC windows that mapped to 01:00 local, and there was no "already sent for date D" key, so they got two digests an hour apart.

The large blast was a cousin of the same mistake, a week later in a different zone, and at 09:00. A deploy that week moved the cron from */15 to a worker that could retry a batch if the process was killed mid-loop. On the first Monday after the move, the worker was killed after sending ~4,000 digests (a deploy), retried the whole batch, and sent them again, because the only debounce was "are we currently in the first 15 minutes of the hour?". They still were.

Two different triggers, one root idea: "this window of the clock" was used as the idempotency key, and the clock is not a unique key. DST is what made the team look; the retry is what made 4,000 people write in.

Fixing the immediate damage

They stopped the worker, sent a short apology to the 4,000 who got two Monday digests, and marked those users as sent in a new ledger before restarting. The night-shift double send was apologised for as well. No data was corrupted; it was "only" email, which is still a lot of trust to spend in one morning.

A job that might run twice — because of a retry, a deploy, or a clock that repeated an hour — needs an idempotency key that is not 'the current time'. delay.run retries a failed HTTP job and logs every attempt, which is the right behaviour only if the endpoint can see it has already done the work for that user and that local date.

What actually went wrong

The debounce was a property of the clock. hour == 9 and minute < 15 is true for every run in that window, including retries, overlapping processes, and a DST fold that produces the same local hour twice.

Some users still had a fixed offset. Offsets cannot represent DST. They are a permanent source of "this job ran at the wrong time, or twice, or not at all" around the two transition weekends, and they quietly drift for the rest of the year if anyone updates them by hand.

The retry was added without revisiting uniqueness. Retrying a batch is a good idea only after "send digest" is unique per (user_id, local_date).

The fixes

1. A ledger table, unique on user and local date.

CREATE TABLE digest_sends ( user_id bigint NOT NULL, local_date date NOT NULL, sent_at timestamptz NOT NULL, PRIMARY KEY (user_id, local_date) ); 

The job computes local_date in the user's IANA zone, inserts, and sends only if the insert succeeds. A retry, a second worker, or a second 09:00 in the same local night is a no-op.

2. IANA time zones only. The leftover offset_hours path was migrated: every user got a real timezone, or a default and an email asking them to confirm. Offsets were dropped.

3. Treat the fold and the gap as test fixtures. The test suite now runs the job through America/New_York on a spring-forward weekend and a fall-back weekend, for digest hours inside and outside the affected range, and asserts one send per local date.

4. Retries re-enter the same insert. The worker can be as aggressive as it likes; the unique key is the guard, not the cron window.

What I'd take from it

"Once a day at local 09:00" is two requirements: a zone that can name 09:00, and a record that it already happened for that local date. The clock will repeat itself, a deploy will retry the batch, and either one is enough to double-send if the ledger isn't there. DST is just the week you notice.

Related reading

Have you been bitten by a DST fold, a skipped hour, or a retry that turned into a double send? Which of the three was it?