KatInterview

Design a Recurring Task Scheduler That Never Skips a Run

System DesignModel answer
MediumModel answerSystem Designsystem-designschedulercron

Last updated

Design a service that stores 100K recurring cron-style tasks, keeps up to 10K runs in flight, and guarantees each scheduled fire executes at least once despite crashes.

Problem

Design a backend service that lets users register recurring tasks. Each task carries a cron expression, and the service must start a run of that task every time the expression fires.

Requirements and assumptions

  • Roughly 100,000 task definitions are stored.
  • Up to 10,000 runs may be executing at the same moment.
  • Every task is schedule-driven. There is no "start immediately" request; a run begins only when its cron expression fires.
  • Delivery is at least once. Every scheduled fire must eventually produce an execution. Occasionally running the same fire twice is acceptable, but silently dropping a fire is not.
  • The following are left open. Ask about them or state your own assumptions:
    • What a "run" actually does (HTTP call, container launch, message publish) and how long it lasts.
    • How close to the scheduled instant a run must begin.
    • The finest schedule resolution (per minute, or per few seconds).
    • What should happen to fires that were due while the system was down.
    • What to do when a task's previous run is still going as its next fire arrives.

What to produce

Part 1: Data model and API

Define the entities, their keys, and where each task's next fire time lives. Provide endpoints to create, update, pause and delete tasks, and to inspect past runs. Decide whether the schedule should sit on the task record or in a separate entity, and defend the choice. Explain what happens to already-planned runs when a task is edited, paused or deleted.

Part 2: Finding and executing due work

Explain how the system detects which tasks are due among 100,000, hands them to workers, and keeps concurrency within the limit. Cover running several scheduler instances for availability without dispatching twice, and describe what happens when thousands of tasks share one fire time (for example, the top of the hour).

Part 3: At-least-once guarantee

Walk through each place a scheduled run could be lost: a scheduler crashing mid-plan, a dispatch message that never arrives, a worker dying mid-run. Show how the design ensures each fire is eventually executed, where duplicates can appear, and how fires missed during downtime are treated.

Hints

Hint 1

Estimate the load first. 100K tasks with minute-level schedules means at most about 100K fires per minute in the worst case (roughly 1,700 per second on average, far higher in a burst). That is small enough for a single relational database to handle as the source of truth.

Hint 2

Think about what a schedule really is. If each task has exactly one cron expression and nothing else shares it, a separate schedule entity adds a join and a consistency problem without adding capability. What requirement would justify splitting it out?

Hint 3

Make 'plan a run' a single atomic step: insert the run row and advance the task's next_fire_at in one transaction. Then treat the run row as a durable work item that is claimed with a lease, so crashes at any later point leave something a sweeper can find and retry.

Hint 4

Separate 'finding due work' from 'executing it'. Use a queue or a claim query with SKIP LOCKED between them, and enforce the 10K bound with a worker pool size or a semaphore rather than by throttling the scheduler.