KatInterview

Build a Crash-Resilient Delayed and Recurring Task Runner

System DesignModel answer
MediumModel answerSystem Designsystem-designdistributed-systemsscheduling

Last updated

Design a distributed service that runs one-off and cron-style tasks at scale with no lost work, safe retries, duplicate-submission protection, and cancellation.

Design a Crash-Resilient Task Runner

Your team needs a platform that executes background tasks at a requested time. Some tasks run once, others repeat on a cron-style schedule. Both scheduler processes and worker processes can crash at any moment, and the platform must keep working without a human stepping in.

What the system must do

  • Submit: A client sends a task with a payload and a time to run it.
  • Timing: A task runs at or after its requested time, never before.
  • No lost work: If a worker dies mid-task, the task is retried.
  • Duplicate protection: Submitting the same job ID concurrently must not result in the job running more than once.
  • Cancel: A client can cancel a task that has not finished.

Non-functional targets

  • About 10,000 submissions per second.
  • About 1 million pending tasks at any time.
  • Task state survives restarts of any component.
  • Node failures are handled automatically.

What to cover in your answer

  1. API: endpoints to submit, cancel, and query status.
  2. Data model: schema or storage strategy for tasks and their executions.
  3. Architecture: the scheduler, queue or dispatch path, workers, and storage, and how they talk to each other.
  4. Failure handling: what happens when a worker crashes mid-task, and when a scheduler crashes while dispatching.

State your delivery guarantee explicitly and justify it. Be prepared to discuss trade-offs and follow-ups such as retries, poison tasks, and idempotency of the work itself.

Hints

Hint 1

Pick the delivery guarantee first. Exactly-once across a network is not realistic, so think about at-least-once delivery combined with idempotent handling and a way to reject stale workers.

Hint 2

Separate the job definition from each individual run. A deterministic run ID (job ID plus scheduled time) makes duplicate creation of a recurring occurrence impossible via a unique key.

Hint 3

Use leases with heartbeats for workers and for scheduler shard ownership, and make the state change plus the intent to dispatch commit atomically (transactional outbox). Add an attempt counter that works as a fencing token.