Run one-time and recurring jobs across many workers, with retries and cancellation.
Separate schedules from executions, claim work safely, and fence workers that have lost ownership.
01. Model the state explicitly
A schedule describes when to create work; an execution records one concrete occurrence. Store schedule version, timezone, next due time, and policy for missed runs. Each execution has an ID, scheduled timestamp, attempt count, state, and ownership token.
For recurring work, define whether a late execution is skipped, caught up, or coalesced. Daylight-saving transitions and user edits make “every day at 09:00” different from “every 24 hours.” Preserve the intended timezone and make ambiguous-time behavior explicit.
02. Claim due work atomically
Partition schedules and scan an indexed due-time field. Claim a bounded batch with a database-supported locking or conditional-update protocol. Creating the execution and advancing the schedule should be transactional, with a uniqueness constraint on the schedule occurrence to prevent duplicate creation.
Workers receive execution IDs from a durable queue. The scheduler should not need to hold an open database transaction for the full runtime of a job. Track dispatch and execution separately so a queue outage does not make the schedule disappear.
03. Fence stale workers
Suppose worker A claims execution E with token 41 and pauses. Its lease expires; worker B receives token 42. When A resumes, a completion or external write carrying token 41 must be rejected by the protected destination if strict single-owner writes are required.
A lease alone does not stop the old process from running. A monotonically increasing token only helps if the destination actually enforces it. Where that is unavailable, make effects idempotent or reconcile duplicates. Cancellation likewise needs a defined boundary: requested cancellation cannot undo an already completed external action.
04. Plan for bursts and recovery
One million daily jobs evenly distributed would average about 11.6 jobs per second, but scheduling all of them at midnight creates a million-job burst. Allow jitter where semantics permit, bound dispatch rates, and reserve capacity for urgent work.
Track scheduling lag, execution lag, duration, missed occurrences, and retry age. Follow-up: after a two-hour outage, apply the declared catch-up policy rather than blindly creating every missed occurrence. A successful recovery restores intended business behavior, not merely an empty queue.
Practice the follow-up
What must a storage service check for a fencing token to protect against a paused worker?
Connect this to durable queue semantics.
For distributed training, continue with GPU scheduling: gang placement, topology and checkpoint recovery.
Sources & further reading
PostgreSQL: explicit locking. The interview scenario, calculations, and proposed design choices are illustrative.
Continue your preparation
Choose a connected learning path or browse more System design questions. Read our editorial method and attribution policy.