KatInterview

Build a Timed and Recurring Task Execution Platform

System DesignModel answer
MediumModel answerSystem Designsystem-designschedulerdistributed-systems

Last updated

Design a service that accepts tasks, fires them immediately, at a future time or on a cron schedule, runs them on a worker fleet, and tracks each execution reliably.

Design a platform that lets clients register units of work and have them executed on a fleet of worker machines at the correct moment. The platform must record the progress of every execution until it ends.

Required behavior

  • A task can be launched right away, launched once at a chosen future timestamp, or launched repeatedly according to a recurrence rule such as a cron expression.
  • Every individual execution (an attempt window for one trigger, which we call a run) has a status that clients can query, plus its result and logs.
  • A failed run is retried according to a retry policy attached to the task. Clients can pause, modify or remove a task, and can cancel a run that is in flight.

Scope notes

  • Treat the task payload as opaque, for example a container image or a command with arguments. The platform decides when and where it runs, not what it does.
  • Volume, timing precision, task duration and delivery semantics are intentionally unspecified. Ask about them or state your own assumptions explicitly.

What to discuss

  1. Clarifying questions and the assumptions you settle on.
  2. Data model and the run lifecycle.
  3. How due work is discovered efficiently at scale and how multiple scheduler instances divide it.
  4. How each run is dispatched exactly once in effect, and how crashed or unresponsive workers are detected.
  5. Retries, timeouts, cancellation, and what happens to recurring runs missed during an outage.
  6. Behavior under bursts (for example thousands of tasks due at midnight), including priorities, tenant limits and back-pressure.
  7. Failure of each component and the metrics you would monitor. </hints_placeholder>

Hints

Hint 1

Separate the long-lived task definition from the short-lived run record, and write down the run state machine (for example scheduled, queued, running, succeeded, failed, canceled) before drawing boxes.

Hint 2

Think about how to query 'what is due in the next few seconds' without scanning everything: index or partition by next fire time, and let each scheduler instance own a shard of the keyspace via a lease or consistent hashing.

Hint 3

You cannot get true exactly-once execution over an unreliable network. Aim for at-least-once dispatch combined with leases, heartbeats and a unique run ID so owners can make their work idempotent.