KatInterview

Scheduler Design Where Each Run Starts At Most Once

System DesignModel answer
HardModel answerSystem Designsystem-designdistributed-schedulerat-most-once

Last updated

Design a distributed job scheduler for one-time and recurring jobs where a scheduled run must never start twice, covering crash ambiguity, deadlines, logs, cancellation and notifications.

Design a distributed service that runs background jobs on a schedule. It must support:

  • One-time jobs and recurring jobs (for example, cron-style schedules).
  • An execution deadline (SLA) for each run.
  • Log capture for every run.
  • Cancellation of pending or upcoming runs.
  • Notifications to subscribers about run outcomes.

The core guarantee is at-most-once execution per scheduled run: for any single scheduled occurrence, the job's code must never begin more than once.

What to cover

  • The public API (create, status, cancel, history) and the identifiers you use for jobs and for individual runs.
  • How a worker durably claims a run before starting it.
  • What happens when a worker, queue, or database node crashes at different moments, especially when you cannot tell whether the run actually started.
  • Why the guarantee limits availability and completion, and how you present that trade-off.
  • How you report missed deadlines.
  • Where logs live and how they are retrieved.
  • How subscribers get notified without losing events.
  • Whether your chosen queue (for example Redis) is durable enough, or whether you need something else.

Points to clarify with the interviewer

  • Does "execution" mean the application code starts, or that an external side effect commits?
  • Is it acceptable for a run to end up lost or in an unknown state?
  • May a retry be modeled as a new scheduled run?
  • Does the SLA measure start time or completion time?
  • Must subscribers be able to recover notifications they missed?

Hints

Hint 1

Separate the identity of a job (the definition) from the identity of a run (one occurrence, for example job_id plus scheduled time). Make the run ID deterministic so that creating it twice is idempotent.

Hint 2

Treat the claim as a conditional write in a strongly consistent store, moving the run from PENDING to STARTING with a fencing token. Ask what a new owner can safely conclude when the old owner vanished after STARTING.

Hint 3

Pick a rule for ambiguous runs: mark them UNKNOWN or FAILED for human or policy review rather than silently re-running them. Then discuss the alternative of at-least-once delivery plus idempotent effects.

Hint 4

For notifications, write the event to an outbox table in the same transaction as the state change, and let a relay publish it. Do not rely on a cache-style queue as the source of truth.