Skip to content

Lesson 7 of 9 · 23m

Cron, Delayed Jobs, and Scheduler Design

Design recurring schedules and delayed execution with overlap protection, time-zone clarity, catch-up rules, and leader election.

Schedulers look harmless because the happy path is just a clock. Then daylight saving time hits, a deploy happens at midnight, two scheduler replicas run the same job, and the billing system sends duplicate invoices with impressive punctuality. Scheduling is queue design plus time, and time is hostile.

Separate triggering from execution

The scheduler should enqueue work, not do the work inline. A cron process that performs the job couples scheduler failure to worker failure and makes scaling awkward.

scheduler tick -> enqueue job with deterministic key -> worker executes

This also gives you the queue's visibility, retry, monitoring, and dead-letter machinery for free.

Cron syntax is not a reliability model

A system cron entry like this only says when a process starts:

*/10 * * * * /opt/app/bin/enqueue-usage-rollup

It does not answer the important questions: what happens if it runs twice, misses a tick, overlaps the previous run, or executes in the wrong time zone? Those are application-level semantics.

Use explicit time-zone behavior. Prefer UTC internally and document local-time semantics for user-facing schedules.

Overlap protection is mandatory

A ten-minute job triggered every five minutes needs a lock or a skip rule:

insert into scheduler_locks (name, expires_at)
values ('usage-rollup', now() + interval '15 minutes')
on conflict (name) do nothing;

If the insert did not happen, another scheduler owns the tick. Locks need expiry, because crashed processes do not clean up. For database-backed systems, select ... for update skip locked can coordinate workers but is not a magical distributed scheduler by itself.

Deterministic keys prevent duplicate ticks

If two schedulers enqueue the same logical occurrence, the job key should collapse them:

usage-rollup:tenant_8:2026-11-30T18:00Z
report-generate:report_42:attempt_window_20261130

The enqueue operation becomes idempotent. This is better than hoping leader election is perfect. Leader election reduces duplicate triggers; deterministic keys make duplicates survivable.

Delayed jobs are scheduled jobs too

"Send reminder in 24 hours" can be implemented through delayed queue delivery, a due-at index, or a workflow timer. Each has trade-offs:

queue delay       simple, limited maximum delay, hard to cancel
due_at table      queryable, cancellable, needs scanner
workflow timer    durable state machine, more platform complexity

Prefer a due-at table or workflow state when users can cancel, reschedule, or inspect pending work. Hidden queue timers are miserable operationally.

Define catch-up behavior before the outage

If the scheduler is down for six hours, should it run six missed reports, one catch-up report, or skip to now? There is no universal answer. Billing may need exact windows; cache refreshes usually need only latest state. Write it in the job definition:

usage_rollup:
  schedule: "*/10 * * * *"
  misfire_policy: catch_up_windows
report_snapshot:
  schedule: "0 2 * * *"
  misfire_policy: run_latest_only

Field notes

  • Cron runs in the host's configured environment, not necessarily the application's environment. Secrets, PATH, and time zone surprises are common.
  • Never rely on exactly-once scheduler execution. Make enqueueing idempotent first.
  • Keep scheduled time in the payload. Workers should process the intended window, not whatever time they happen to start.
  • Monitor missed ticks as well as job failures. A silent dead scheduler produces no failed jobs and therefore no obvious signal.

Was this lesson helpful?

Sign in to rate