KatInterview

Build a Reliable Cron-as-a-Service Platform

System DesignModel answer
MediumModel answerSystem Designsystem-designjob-schedulerdistributed-systems

Last updated

Design a cloud service that lets clients register recurring or one-off tasks, runs them on a worker fleet without skipping any run, enforces timeouts, and exposes run status and logs.

Problem

Design a managed task-scheduling platform that runs as a set of cloud microservices. Clients use an API to register work that should run at specific times, and the platform executes it on a pool of worker machines.

What a client submits

Each job definition contains:

  • Job ID: optional; the platform generates one if it is missing.
  • Schedule: a cron expression, a single run at a given instant, or a fixed interval ("every X minutes").
  • Task: what to execute, such as a script name, a container image, or another executable description.
  • Resource needs: coarse, such as a CPU/memory tier or machine class.
  • Timeout: the maximum allowed run duration.
  • Timeout action: what happens when a run overruns (kill it, mark it failed, retry, trigger another job, and so on).

What the platform must do

  1. Run every job at, or close to, its scheduled time across many workers, even when machines fail or restart.
  2. Detect runs that exceed their timeout and apply the configured action.
  3. Let clients query run state (PENDING, RUNNING, SUCCESS, FAILED, TIMED_OUT, ...) for a single run, and the history of a job (for example, the last N runs with timestamps and outcomes).
  4. Let clients retrieve logs (stdout/stderr or structured) for a past run.

Constraints and assumptions

  • Hundreds of thousands of active jobs.
  • Minute-level scheduling at minimum; finer granularity is fine if your design supports it.
  • Reliability beats precision: a few seconds of lateness is acceptable, but skipping a run is a defect. Let this drive your delivery-semantics decision.
  • If any scheduler instance or worker crashes or restarts, every due job must still eventually run.
  • Components must be containerized/VM-hosted and scale independently.
  • State any unspecified choices as explicit assumptions: time zones and DST, log retention, exactly-once vs at-least-once, catch-up policy after an outage, and depth of security or multi-tenancy.

What to cover

Walk through the architecture, data model, scheduling logic, execution path, failure handling, and query APIs. Justify the major choices and call out trade-offs. Good clarifying questions include the execution semantics required, the policy for missed windows, timing accuracy, task duration and peak concurrency, retention needs, and how detailed resource requirements are.

Hints

Hint 1

Pick the delivery guarantee first. If skipping is worse than running twice, choose at-least-once and make each run have a unique, deterministic identity (job ID plus scheduled time) so duplicates can be detected.

Hint 2

Keep the scheduler stateless and let the database be the source of truth: index jobs by next due time, and in one transaction create the run record and advance the job's next due time. Partition jobs across scheduler instances with row locks (such as SKIP LOCKED) or hash ranges.

Hint 3

Treat every worker as something that can vanish. Use leases with heartbeats, local timeout enforcement, and a background reaper that requeues expired leases and force-completes runs that exceeded their deadline. Store logs in object storage rather than the database.